-
Notifications
You must be signed in to change notification settings - Fork 48
/
tabview
executable file
·100 lines (86 loc) · 3.81 KB
/
tabview
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" tabview -- View a tab-delimited file in a spreadsheet-like display.
Scott Hansen <firecat four one five three at gmail dot com>
Based on code contributed by A.M. Kuchling <amk at amk dot ca>
Usage:
From command line: tabview <filename>
From python command line to view an object:
import tabview.tabview as t
a = [["a","b","c"], ["d","e","f"]]
t.view(a)
From python command line to view a file:
import tabview.tabview as t
t.view(fn=<filename>[, enc=<encoding>])
"""
from __future__ import print_function, unicode_literals
import argparse
import os
import sys
from tabview.tabview import view
def arg_parse():
"""Parse filename and show help."""
parser = argparse.ArgumentParser(description="View a tab-delimited file "
"in a spreadsheet-like display. "
"Press F1 or '?' while running for a "
"list of available keybindings.")
parser.add_argument('filename', help="File to read. Use '-' to read from "
"the standard input instead.")
parser.add_argument('--encoding', '-e', help="Encoding, if required. "
"If the file is UTF-8, Latin-1(iso8859-1) or a few "
"other common encodings, it should be detected "
"automatically. If not, you can pass "
"'CP720', or 'iso8859-2', for example.")
parser.add_argument('--start_pos', '-s',
help="Initial cursor display position. "
"Single number for just y (row) position, or two "
"comma-separated numbers (--start_pos 2,3) for both. "
"Alternatively, you can pass the numbers in the more "
"classic +y:[x] format without the --start_pos label. "
"Like 'tabview <fn> +5:10'")
parser.add_argument('--width', '-w', default=20,
help="Specify column width. 'max' or 'mode' (default) "
"for variable widths, or an integer value for "
"fixed column width.")
parser.add_argument('--double_width', action='store_true', default=False,
help="Force full handling of double-width characters "
"for large files (with a performance penalty)")
return parser.parse_known_args()
def start_position(start_norm, start_classic):
"""Given a string "[y, x, ...]" or a string "+[y]:[x]", return a tuple (y, x)
for the start position
Args: start_norm - string [y,x, ...]
start_classic - string "+[y]:[x]"
Returns: tuple (y, x)
"""
if start_norm is not None:
start_pos = start_norm.split(',')[:2]
if not start_pos[0]:
start_pos[0] = 0
start_pos = [int(i) for i in start_pos]
elif start_classic:
sp = start_classic[0].strip('+').split(':')
if not sp[0]:
sp[0] = 0
try:
start_pos = (int(sp[0]), int(sp[1]))
except IndexError:
start_pos = (int(sp[0]), 0)
else:
start_pos = (0, 0)
return start_pos
def fixup_stdin():
print("tabview: Reading from stdin...", file=sys.stderr)
data = os.fdopen(os.dup(0), 'rb')
os.dup2(os.open("/dev/tty", os.O_RDONLY), 0)
return data
if __name__ == '__main__':
args, extra = arg_parse()
pos_plus = [i for i in extra if i.startswith('+')]
start_pos = start_position(args.start_pos, pos_plus)
if args.filename != '-':
data = args.filename
else:
data = fixup_stdin()
view(data, enc=args.encoding, start_pos=start_pos,
column_width=args.width, double_width=args.double_width)