-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
vd
executable file
·122 lines (99 loc) · 3.78 KB
/
vd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
#
# Usage: $0 [-d] [-r] [-f <filetype>] [<visidata options>] [<input_source> ...]
# $0 [-d] [-r] [-f <filetype>] [<visidata options>] [--piano] --play <cmdlog.vd> [--foo=value ...]
__version__ = 'saul.pw/VisiData v0.97pre'
import os
from visidata import *
def eval_vd(logpath, *args, **kwargs):
'Instantiate logpath with args/kwargs replaced and replay all commands.'
log = logpath.read_text().format(*args, **kwargs)
src = PathFd(logpath.fqpn, io.StringIO(log))
vs = openSource(src)
vd().commandlog.openHook(vs, src)
vd().push(vs)
vs.vd = vd()
return vs
def main():
'Open the given sources using the VisiData interface.'
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('inputs', nargs='*', help='initial sources')
parser.add_argument('-d', dest='debug', action='store_true', default=False, help='abort on error')
parser.add_argument('-r', dest='readonly', action='store_true', default=False, help='disable editing')
parser.add_argument('-f', dest='filetype', default='', help='specify file type')
parser.add_argument('-y', dest='confirm_overwrite', default=True, action='store_false', help='sets confirm_overwrite option to false')
parser.add_argument('-p', '--play', dest='play', default=None, help='.vd file to replay')
for optname, v in vdtui.baseOptions.items():
name, optval, defaultval, helpstr = v
if name.startswith('color_') or name.startswith('disp_'):
continue
action = 'store_true' if optval is False else 'store'
parser.add_argument('--' + optname.replace('_', '-'), action=action, dest=optname, default=None, help=helpstr)
args = parser.parse_args()
# user customisations in config file in standard location
for fnrc in ('.visidatarc', '$XDG_CONFIG_HOME/visidata/config', '~/.visidatarc'):
p = Path(fnrc)
if p.exists():
exec(open(p.resolve()).read())
break
# command-line overrides
for optname, optval in vars(args).items():
if optval is not None and optname not in ['inputs', 'play']:
vdtui.options[optname] = optval
inputs = list(args.inputs)
if not sys.stdin.isatty():
# duplicate stdin for input and reopen tty as stdin
src = PathFd('stdin', open(os.dup(0)))
inputs.append(src)
f = open('/dev/tty')
os.dup2(f.fileno(), 0)
if not args.play:
if not inputs:
inputs = ['.']
sources = []
for src in inputs:
vs = openSource(src)
vdtui.vd().commandlog.openHook(vs, src)
sources.append(vs)
vdtui.run(sources)
return
# replay mode, gather additional args
fmtargs = []
fmtkwargs = {}
args = parser.parse_args()
i=0
while i < len(args.inputs):
arg = args.inputs[i]
# parse either '--key value' or '--key=value'
if arg.startswith('-'):
arg = arg.lstrip('-')
else:
fmtargs.append(arg)
# goes into args
continue
if '=' in arg:
k, v = arg.split('=')
fmtkwargs[k] = v
else: # either goes into args or key+nextarg
i += 1
fmtkwargs[arg] = args.inputs[i]
i += 1
if args.play == '-':
vdfile = inputs[0]
assert isinstance(vdfile, PathFd)
vdfile.name = 'stdin.vd'
else:
vdfile = Path(args.play)
vs = eval_vd(vdfile, *fmtargs, **fmtkwargs)
if args.piano:
vs.replay()
run([])
else:
vd().status = print
vs.replay_sync()
if __name__ == '__main__':
vdtui.status(__version__)
vdtui.addGlobals(globals())
main()
os._exit(0) # cleanup can be expensive with large datasets