Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
vd._stdin, vd._stdout = duptty() # always dup stdin/stdout
stdinSource = Path('-', fp=vd._stdin)
# parse args, including +sheetname:subsheet:4:3 starting at row:col on sheetname:subsheet[:...]
start_positions = [] # (list_of_sheetstr, str, str) # empty sheetstr means all sheets
startsheets, startrow, startcol = [], None, None
fmtargs = []
fmtkwargs = {}
inputs = []
for arg in args.inputs:
if arg.startswith('+'): # position cursor at start
if ':' in arg:
pos = arg[1:].split(':')
if len(pos) == 1:
startsheet = [Path(inputs[-1]).name] if inputs else None
start_positions.append((startsheet, pos[0], None))
elif len(pos) == 2:
startsheet = [Path(inputs[-1]).name] if inputs else None
startrow, startcol = pos
start_positions.append((None, startrow, startcol))
elif len(pos) >= 3:
startsheets = pos[:-2]
startrow, startcol = pos[-2:]
start_positions.append((startsheets, startrow, startcol))
else:
start_positions.append((None, arg[1:], None))
elif args.play and '=' in arg:
# parse 'key=value' pairs for formatting cmdlog template in replay mode
k, v = arg.split('=')
fmtkwargs[k] = v
def eval_vd(logpath, *args, **kwargs):
'Instantiate logpath with args/kwargs replaced and replay all commands.'
log = logpath.read_text()
if args or kwargs:
log = log.format(*args, **kwargs)
src = Path(logpath.given, fp=io.StringIO(log), filesize=len(log))
vs = openSource(src, filetype='vd')
vs.name += '_vd'
vd.push(vs)
vs.vd = vd
return vs
if vs:
vs.moveToRow(startrow) or vd.warning(f'{vs} has no row "{startrow}"')
if startcol:
for vs in sheets:
if vs:
vs.moveToCol(startcol) or vd.warning(f'{vs} has no column "{startcol}"')
if not args.batch:
run(vd.sheets[0])
else:
if args.play == '-':
vdfile = stdinSource
vdfile.name = 'stdin.vd'
else:
vdfile = Path(args.play)
vs = eval_vd(vdfile, *fmtargs, **fmtkwargs)
vd.sync(vs.reload())
if args.batch:
if vs.replay_sync(): # error
return 1
else:
vs.replay()
run()
if vd.sheets and (flPipedOutput or args.output):
outpath = Path(args.output or '-')
saveSheets(outpath, vd.sheets[0], confirm_overwrite=False)
vd.sync()
vd._stdout.flush()
vdfile = stdinSource
vdfile.name = 'stdin.vd'
else:
vdfile = Path(args.play)
vs = eval_vd(vdfile, *fmtargs, **fmtkwargs)
vd.sync(vs.reload())
if args.batch:
if vs.replay_sync(): # error
return 1
else:
vs.replay()
run()
if vd.sheets and (flPipedOutput or args.output):
outpath = Path(args.output or '-')
saveSheets(outpath, vd.sheets[0], confirm_overwrite=False)
vd.sync()
vd._stdout.flush()
return 0
def launchExternalEditor(v, linenum=0):
import tempfile
with tempfile.NamedTemporaryFile() as temp:
with open(temp.name, 'w') as fp:
fp.write(v)
return launchExternalEditorPath(visidata.Path(temp.name))
def loadConfigFile(fnrc, _globals=None):
p = visidata.Path(fnrc)
if _globals is None:
_globals = globals()
if p.exists():
try:
code = compile(open(p).read(), str(p), 'exec')
exec(code, _globals)
except Exception as e:
vd.exceptionCaught(e)
addGlobals(_globals)
def reload(self):
self.toBeDeleted = []
self.rows = []
basepath = self.source.resolve()
for folder, subdirs, files in os.walk(basepath):
subfolder = folder[len(basepath)+1:]
if subfolder.startswith('.'): continue
for fn in files:
if fn.startswith('.'): continue
p = Path(os.path.join(folder, fn))
self.rows.append(p)
# sort by modtime initially
self.rows.sort(key=lambda row: row.stat().st_mtime, reverse=True)
def reload(self):
from pkg_resources import resource_filename
cmdlist = TsvSheet('cmdlist', source=Path(resource_filename(__name__, 'commands.tsv')))
cmdlist.reload_sync()
self.cmddict = {}
for cmdrow in cmdlist.rows:
self.cmddict[(cmdrow.sheet, cmdrow.longname)] = cmdrow
self.revbinds = {
longname:keystrokes
for (keystrokes, _), longname in bindkeys.iter(self.source)
if keystrokes not in self.revbinds
}
self.rows = []
for (k, o), v in commands.iter(self.source):
self.addRow(v)
v.sheet = o