Skip to content

Commit

Permalink
[column] add setcol-precision-more and -less #1609 #1650
Browse files Browse the repository at this point in the history
- works on float, floatsi, currency, date columns

Thanks to @andycraig for contributing the original plugin!
  • Loading branch information
saulpw committed Oct 17, 2023
1 parent cfb95a6 commit 6e21c1d
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions visidata/features/change_precision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
__author__ = 'Andy Craig, andycraig (https://github.com/andycraig)'


import re

from visidata import vd, Sheet, Column, floatsi, currency, date

date_fmtstrs = [
'%Y',
'%Y-%m',
# '%Y-W%U',
'%Y-%m-%d',
'%Y-%m-%d %H',
'%Y-%m-%d %H:%M',
'%Y-%m-%d %H:%M:%S',
'%Y-%m-%d %H:%M:%S.%f',
]

@Column.api
def setcol_precision(col, amount:int):
if col.type is date:
try:
i = date_fmtstrs.index(col.fmtstr)
except ValueError:
i = 2
col.fmtstr = date_fmtstrs[(i+amount)%len(date_fmtstrs)]
elif col.type in (float, floatsi, currency):
if col.fmtstr == '':
col.fmtstr = f'%.{2 + amount}f'
else:
precision_str = re.match(r'%.([0-9]+)f', col.fmtstr)
if not precision_str is None:
col.fmtstr = f'%.{max(0, int(precision_str[1]) + amount)}f'
else:
col.type = float
if col.fmtstr == '':
col.fmtstr = '%.2f'


vd.addMenuItems('''
Column > Set precision > more > setcol-precision-more
Column > Set precision > less > setcol-precision-less
''')

Sheet.addCommand('Alt+-', 'setcol-precision-less', 'cursorCol.setcol_precision(-1)', 'show less precision in current column')
Sheet.addCommand('Alt++', 'setcol-precision-more', 'cursorCol.setcol_precision(1)', 'show more precision in current column')

0 comments on commit 6e21c1d

Please sign in to comment.