Skip to content

Commit

Permalink
[choose] add type for join/aggregators history #2075
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 24, 2023
1 parent 4f200b9 commit 0e9d0c9
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
10 changes: 7 additions & 3 deletions visidata/aggregators.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,13 @@ def aggregator_choices(vd):
]


Sheet.addCommand('+', 'aggregate-col', 'addAggregators([cursorCol], chooseMany(aggregator_choices))', 'Add aggregator to current column')
Sheet.addCommand('z+', 'memo-aggregate', 'for agg in chooseMany(aggregator_choices): cursorCol.memo_aggregate(aggregators[agg], selectedRows or rows)', 'memo result of aggregator over values in selected rows for current column')
ColumnsSheet.addCommand('g+', 'aggregate-cols', 'addAggregators(selectedRows or source[0].nonKeyVisibleCols, chooseMany(aggregator_choices))', 'add aggregators to selected source columns')
@VisiData.api
def chooseAggregators():
return vd.chooseMany(vd.aggregator_choices, type="aggregators")

Sheet.addCommand('+', 'aggregate-col', 'addAggregators([cursorCol], chooseAggregators())', 'Add aggregator to current column')
Sheet.addCommand('z+', 'memo-aggregate', 'for agg in chooseAggregators(): cursorCol.memo_aggregate(aggregators[agg], selectedRows or rows)', 'memo result of aggregator over values in selected rows for current column')
ColumnsSheet.addCommand('g+', 'aggregate-cols', 'addAggregators(selectedRows or source[0].nonKeyVisibleCols, chooseAggregators())', 'add aggregators to selected source columns')

vd.addMenuItems('''
Column > Add aggregator > aggregate-col
Expand Down
12 changes: 6 additions & 6 deletions visidata/choose.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
vd.option('fancy_chooser', False, 'a nicer selection interface for aggregators and jointype')

@VisiData.api
def chooseOne(vd, choices):
def chooseOne(vd, choices, type=''):
'Return one user-selected key from *choices*.'
return vd.choose(choices, 1)
return vd.choose(choices, 1, type=type)


@VisiData.api
def choose(vd, choices, n=None):
def choose(vd, choices, n=None, type=''):
'Return a list of 1 to *n* "key" from elements of *choices* (see chooseMany).'
ret = vd.chooseMany(choices) or vd.fail('no choice made')
ret = vd.chooseMany(choices, type=type) or vd.fail('no choice made')
if n and len(ret) > n:
vd.fail('can only choose %s' % n)
return ret[0] if n==1 else ret
Expand All @@ -39,7 +39,7 @@ def chooseFancy(vd, choices):


@VisiData.api
def chooseMany(vd, choices):
def chooseMany(vd, choices, type=''):
'''Return a list of 1 or more keys from *choices*, which is a list of
dicts. Each element dict must have a unique "key", which must be typed
directly by the user in non-fancy mode (therefore no spaces). All other
Expand All @@ -65,7 +65,7 @@ def throw_fancy(v, i):
if ret:
raise ReturnValue(ret)
return v, i
chosenstr = vd.input(prompt+': ', completer=CompleteKey(choice_keys), bindings={'^X': throw_fancy})
chosenstr = vd.input(prompt+': ', completer=CompleteKey(choice_keys), bindings={'^X': throw_fancy}, type=type)
for c in chosenstr.split():
if c in choice_keys:
chosen.append(c)
Expand Down
13 changes: 9 additions & 4 deletions visidata/features/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,17 @@ def iterload(self):
keyedcols[col.name][sheet] = col


IndexSheet.addCommand('&', 'join-selected', 'left, rights = someSelectedRows[0], someSelectedRows[1:]; vd.push(left.openJoin(rights, jointype=chooseOne(jointypes)))', 'merge selected sheets with visible columns from all, keeping rows according to jointype')
@VisiData.api
def chooseJointype(vd):
return chooseOne(vd.jointypes, type="jointype")


IndexSheet.addCommand('&', 'join-selected', 'left, rights = someSelectedRows[0], someSelectedRows[1:]; vd.push(left.openJoin(rights, jointype=chooseJointype()))', 'merge selected sheets with visible columns from all, keeping rows according to jointype')
IndexSheet.bindkey('g&', 'join-selected')
Sheet.addCommand('&', 'join-sheets-top2', 'vd.push(openJoin(vd.sheets[1:2], jointype=chooseOne(jointypes)))', 'concatenate top two sheets in Sheets Stack')
Sheet.addCommand('g&', 'join-sheets-all', 'vd.push(openJoin(vd.sheets[1:], jointype=chooseOne(jointypes)))', 'concatenate all sheets in Sheets Stack')
Sheet.addCommand('&', 'join-sheets-top2', 'vd.push(openJoin(vd.sheets[1:2], jointype=chooseJointype()))', 'concatenate top two sheets in Sheets Stack')
Sheet.addCommand('g&', 'join-sheets-all', 'vd.push(openJoin(vd.sheets[1:], jointype=chooseJointype()))', 'concatenate all sheets in Sheets Stack')

ColumnsSheet.addCommand('&', 'join-sheets-cols', 'vd.push(join_sheets_cols(selectedRows, jointype=chooseOne(jointypes)))', '')
ColumnsSheet.addCommand('&', 'join-sheets-cols', 'vd.push(join_sheets_cols(selectedRows, jointype=chooseJointype()))', '')

vd.addMenuItems('''
Data > Join > selected sheets > join-selected
Expand Down
2 changes: 1 addition & 1 deletion visidata/features/procmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def processes(vd):
def chooseSignal(vd):
import signal
d = [{'key': attr[3:]} for attr in dir(signal) if attr.startswith('SIG') and not attr.startswith('SIG_')]
return getattr(signal, 'SIG'+vd.chooseOne(d))
return getattr(signal, 'SIG'+vd.chooseOne(d, type='signal'))

ProcessesSheet.addCommand('d', 'term-process', 'os.kill(cursorRow.pid, signal.SIGTERM)', 'send SIGTERM to process')
ProcessesSheet.addCommand('gd', 'term-selected', 'for r in someSelectedRows: os.kill(r.pid, signal.SIGTERM)', 'send SIGTERM to selected processes')
Expand Down
2 changes: 1 addition & 1 deletion visidata/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def afterLoad(self):
@PivotSheet.api
def addcol_aggr(sheet, col):
hasattr(col, 'origCol') or vd.fail('not an aggregation column')
for agg in vd.chooseMany(vd.aggregator_choices):
for agg in vd.chooseAggregators():
sheet.addColumnAtCursor(makeAggrColumn(col.origCol, vd.aggregators[agg]))


Expand Down
2 changes: 1 addition & 1 deletion visidata/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def set_theme(obj, theme=''):
obj.options[k] = v


BaseSheet.addCommand('', 'theme-input', 'vd.set_theme(chooseOne([dict(key=k) for k in themes.keys()]))', 'choose from available themes')
BaseSheet.addCommand('', 'theme-input', 'vd.set_theme(chooseOne([dict(key=k) for k in themes.keys()], type="theme"))', 'choose from available themes')
BaseSheet.addCommand('', 'theme-default', 'vd.set_theme()', 'reset theme to VisiData defaults')

vd.addMenuItem('View', 'Set theme', 'choose', 'theme-input')
Expand Down

0 comments on commit 0e9d0c9

Please sign in to comment.