Skip to content

Commit

Permalink
[shell] add $curcol #989
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 5, 2024
1 parent 8fdf2c7 commit d50a1bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions visidata/sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class RecursiveExprException(Exception):

class LazyComputeRow:
'Calculate column values as needed.'
def __init__(self, sheet, row, col=None, extra={}):
def __init__(self, sheet, row, col=None, **kwargs):
self.row = row
self.col = col
self.sheet = sheet
self.extra = AttrDict(extra) # extra bindings
self.extra = AttrDict(kwargs) # extra bindings
self._usedcols = set()

self._lcm.clear() # reset locals on lcm
Expand Down Expand Up @@ -394,7 +394,7 @@ def evalExpr(self, expr:str, row=None, col=None, **kwargs):
'eval() expr in the context of (row, col), with extra bindings in kwargs'
if row is not None:
# contexts are cached by sheet/rowid for duration of drawcycle
contexts = vd._evalcontexts.setdefault((self, self.rowid(row), col), LazyComputeRow(self, row, col, kwargs))
contexts = vd._evalcontexts.setdefault((self, self.rowid(row), col), LazyComputeRow(self, row, col, **kwargs))
else:
contexts = dict(sheet=self)

Expand Down
16 changes: 8 additions & 8 deletions visidata/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,30 @@ def open_fdir(vd, p):
return FileListSheet(p.base_stem, source=p)

@VisiData.api
def addShellColumns(vd, cmd, sheet):
shellcol = ColumnShell(cmd, source=sheet, width=0)
def addShellColumns(vd, cmd, sheet, curcol=None):
shellcol = ColumnShell(cmd, source=sheet, width=0, curcol=curcol)
sheet.addColumnAtCursor(
Column(cmd+'_stdout', type=bytes.rstrip, srccol=shellcol, getter=lambda col,row: col.srccol.getValue(row)[0]),
Column(cmd+'_stderr', type=bytes.rstrip, srccol=shellcol, getter=lambda col,row: col.srccol.getValue(row)[1]),
shellcol)


class ColumnShell(Column):
def __init__(self, name, cmd=None, **kwargs):
def __init__(self, name, cmd=None, curcol=None, **kwargs):
super().__init__(name, **kwargs)
self.expr = cmd or name
self.curcol = curcol

@asynccache(lambda col,row: (col, col.sheet.rowid(row)))
def calcValue(self, row):
try:
import shlex
args = []
context = LazyComputeRow(self.source, row)
context = LazyComputeRow(self.source, row, curcol=self.curcol)
for arg in shlex.split(self.expr):
if arg.startswith('$'):
args.append(shlex.quote(str(context[arg[1:]])))
else:
args.append(arg)
arg = shlex.quote(str(context[arg[1:]]))
args.append(arg)

p = subprocess.Popen([os.getenv('SHELL', 'bash'), '-c', shlex.join(args)],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Expand Down Expand Up @@ -253,7 +253,7 @@ def inputShell(vd):
DirSheet.addCommand('`', 'open-dir-parent', 'vd.push(openSource(source.parent if source.resolve()!=Path(".").resolve() else os.path.dirname(source.resolve())))', 'open parent directory') #1801
BaseSheet.addCommand('', 'open-dir-current', 'vd.push(vd.currentDirSheet)', 'open Directory Sheet: browse properties of files in current directory')

Sheet.addCommand('z;', 'addcol-shell', 'cmd=inputShell(); addShellColumns(cmd, sheet)', 'create new column from bash expression, with $columnNames as variables')
Sheet.addCommand('z;', 'addcol-shell', 'cmd=inputShell(); addShellColumns(cmd, sheet, curcol=cursorCol)', 'create new column from bash expression, with $columnNames as variables')

DirSheet.addCommand(ENTER, 'open-row-file', 'vd.push(openSource(cursorRow or fail("no row"), filetype="dir" if cursorRow.is_dir() else LazyComputeRow(sheet, cursorRow).ext))', 'open current file as a new sheet')
DirSheet.addCommand('g'+ENTER, 'open-rows', 'for r in selectedRows: vd.push(openSource(r))', 'open selected files as new sheets')
Expand Down

0 comments on commit d50a1bf

Please sign in to comment.