Skip to content

Commit cb51274

Browse files
author
James McKinney
committed
csvlook: Add --max-rows, --max-columns and --max-column-width options
1 parent c1fc048 commit cb51274

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Improvements:
2222
* csvjoin supports --no-header-row.
2323
* csvjson streams input and output if the --stream and --no-inference flags are set.
2424
* csvjson supports --snifflimit and --no-inference.
25+
* csvlook adds --max-rows, --max-columns and --max-column-width options.
2526
* csvlook supports --snifflimit and --no-inference.
2627
* csvsql supports custom SQLAlchemy dialects.
2728
* csvstat supports --names.

csvkit/utilities/csvlook.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ class CSVLook(CSVKitUtility):
99
description = 'Render a CSV file in the console as a fixed-width table.'
1010

1111
def add_arguments(self):
12+
self.argparser.add_argument('--max-rows', dest='max_rows', type=int,
13+
help='The maximum number of rows to display before truncating the data.')
14+
self.argparser.add_argument('--max-columns', dest='max_columns', type=int,
15+
help='The maximum number of columns to display before truncating the data.')
16+
self.argparser.add_argument('--max-column-width', dest='max_column_width', type=int,
17+
help='Truncate all columns to at most this width. The remainder will be replaced with ellipsis.')
1218
self.argparser.add_argument('-y', '--snifflimit', dest='sniff_limit', type=int,
1319
help='Limit CSV dialect sniffing to the specified number of bytes. Specify "0" to disable sniffing entirely.')
1420
self.argparser.add_argument('--no-inference', dest='no_inference', action='store_true',
1521
help='Disable type inference when parsing the input.')
1622

1723
def main(self):
1824
table = agate.Table.from_csv(self.input_file, sniff_limit=self.args.sniff_limit, header=not self.args.no_header_row, column_types=self.get_column_types(), line_numbers=self.args.line_numbers, **self.reader_kwargs)
19-
table.print_table(output=self.output_file, max_rows=None, max_columns=None, max_column_width=None)
25+
table.print_table(output=self.output_file, max_rows=self.args.max_rows, max_columns=self.args.max_columns, max_column_width=self.args.max_column_width)
2026

2127

2228
def launch_new_instance():

docs/scripts/csvlook.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ Renders a CSV to the command line in a readable, fixed-width format::
2020

2121
optional arguments:
2222
-h, --help show this help message and exit
23+
--max-rows MAX_ROWS The maximum number of rows to display before
24+
truncating the data.
25+
--max-columns MAX_COLUMNS
26+
The maximum number of columns to display before
27+
truncating the data.
28+
--max-column-width MAX_COLUMN_WIDTH
29+
Truncate all columns to at most this width. The
30+
remainder will be replaced with ellipsis.
31+
-y SNIFF_LIMIT, --snifflimit SNIFF_LIMIT
32+
Limit CSV dialect sniffing to the specified number of
33+
bytes. Specify "0" to disable sniffing entirely.
34+
--no-inference Disable type inference when parsing the input.
2335

2436
If a table is too wide to display properly try piping the output to ``less -S`` or truncating it using :doc:`csvcut`.
2537

tests/test_utilities/test_csvlook.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,30 @@ def test_sniff_limit_zero_limit(self):
8686
'| 1;2;3 |',
8787
'|---------|',
8888
])
89+
90+
def test_max_rows(self):
91+
self.assertLines(['--max-rows', '0', 'examples/dummy.csv'], [
92+
'|----+---+----|',
93+
'| a | b | c |',
94+
'|----+---+----|',
95+
'| ... | ... | ... |',
96+
'|----+---+----|',
97+
])
98+
99+
def test_max_columns(self):
100+
self.assertLines(['--max-columns', '1', 'examples/dummy.csv'], [
101+
'|-------+------|',
102+
'| a | ... |',
103+
'|-------+------|',
104+
'| True | ... |',
105+
'|-------+------|',
106+
])
107+
108+
def test_max_column_width(self):
109+
self.assertLines(['--max-column-width', '1', 'examples/dummy.csv'], [
110+
'|--------+---+----|',
111+
'| a | b | c |',
112+
'|--------+---+----|',
113+
'| Tr... | 2 | 3 |',
114+
'|--------+---+----|',
115+
])

0 commit comments

Comments
 (0)