Skip to content

Commit c25d37f

Browse files
feat: option to display output in ASCII tables (#485)
## 📝 Description This PR adds a new option, `--ascii-table`, to display output like this: ![Screenshot 2023-06-28 at 12 14 54 PM](https://github.com/linode/linode-cli/assets/114753608/f24fccb5-f29c-4fb1-a167-e6f434ea5dbb) This is better than the default (unicode) tables in some CI runners. It may be more visually appealing to some users as well. **Questions for consideration:** - Should this be an output option, or a global config/setting? - An advantage of a global setting is users may prefer to see ALL tables (including "Available Commands, etc") in this style, as opposed to just output. - What do you think of the option name? - `--ascii` is cleaner and more consistent with existing `json` and `markdown` options, but I thought it'd be better to explicitly call out that this applies to the tables only (e.g. not any data coming back from the API). ## ✔️ How to Test - Try out the new `--ascii-table` option - Run the new unit test: `pytest tests/unit/test_output.py`
1 parent ada85d9 commit c25d37f

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

linodecli/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
6868
cli.output_handler.columns = "*"
6969
elif parsed.markdown:
7070
cli.output_handler.mode = OutputMode.markdown
71+
elif parsed.ascii_table:
72+
cli.output_handler.mode = OutputMode.ascii_table
7173

7274
if parsed.delimiter:
7375
cli.output_handler.delimiter = parsed.delimiter

linodecli/arg_helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def register_args(parser):
6161
action="store_true",
6262
help="Display output in Markdown format.",
6363
)
64+
parser.add_argument(
65+
"--ascii-table",
66+
action="store_true",
67+
help="Display output in an ASCII table.",
68+
)
6469
parser.add_argument(
6570
"--pretty",
6671
action="store_true",

linodecli/output.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class OutputMode(Enum):
2424
delimited = 2
2525
json = 3
2626
markdown = 4
27+
ascii_table = 5
2728

2829

2930
class OutputHandler: # pylint: disable=too-few-public-methods,too-many-instance-attributes
@@ -80,6 +81,9 @@ def print(
8081
OutputMode.table: lambda: self._table_output(
8182
header, data, columns, title, to
8283
),
84+
OutputMode.ascii_table: lambda: self._table_output(
85+
header, data, columns, title, to, box.ASCII
86+
),
8387
OutputMode.delimited: lambda: self._delimited_output(
8488
header, data, columns, to
8589
),
@@ -131,7 +135,7 @@ def _get_columns(self, response_model):
131135
return columns
132136

133137
def _table_output(
134-
self, header, data, columns, title, to
138+
self, header, data, columns, title, to, box_style=box.SQUARE
135139
): # pylint: disable=too-many-arguments
136140
"""
137141
Pretty-prints data in a table
@@ -145,7 +149,7 @@ def _table_output(
145149
)
146150

147151
tab = Table(
148-
*header, header_style="", box=box.SQUARE, show_header=self.headers
152+
*header, header_style="", box=box_style, show_header=self.headers
149153
)
150154
for row in content:
151155
row = [Text.from_ansi(item) for item in row]

tests/unit/test_output.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,35 @@ def test_table_output_models_no_headers(self, mock_cli):
188188

189189
assert output.getvalue() == mock_table.getvalue()
190190

191+
def test_ascii_table_output(self, mock_cli):
192+
output = io.StringIO()
193+
header = ["h1"]
194+
data = [
195+
{
196+
"cool": "foo",
197+
},
198+
{"cool": "bar"},
199+
]
200+
columns = [ModelAttr("cool", True, True, "string")]
201+
202+
output_handler = mock_cli.output_handler
203+
output_handler._table_output(
204+
header, data, columns, "cool table", output, box.ASCII
205+
)
206+
207+
print(output.getvalue())
208+
209+
assert (
210+
output.getvalue() == " cool \n"
211+
" table \n"
212+
"+-----+\n"
213+
"| h1 |\n"
214+
"|-----|\n"
215+
"| foo |\n"
216+
"| bar |\n"
217+
"+-----+\n"
218+
)
219+
191220
def test_get_columns_from_model(self, mock_cli):
192221
output_handler = mock_cli.output_handler
193222

0 commit comments

Comments
 (0)