Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable --format=raw and fixes table width #1689

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SoftLayer/CLI/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def __init__(self):

def out(self, output):
"""Outputs a string to the console (stdout)."""

# If we output to a | or file, need to set default width so all output is printed.
if not self.console.is_terminal:
self.console.width = 1000000
if self.format == 'json':
try:
self.console.print_json(output)
Expand Down
13 changes: 8 additions & 5 deletions SoftLayer/CLI/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912

# responds to .prettytable()
if hasattr(data, 'prettytable') and fmt in ('table', 'raw'):
return format_prettytable(data)
return format_prettytable(data, fmt)

# responds to .to_python()
if hasattr(data, 'to_python'):
Expand Down Expand Up @@ -68,12 +68,12 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
return str(data)


def format_prettytable(table):
def format_prettytable(table, fmt='table'):
"""Converts SoftLayer.CLI.formatting.Table instance to a prettytable."""
for i, row in enumerate(table.rows):
for j, item in enumerate(row):
table.rows[i][j] = format_output(item)
ptable = table.prettytable()
ptable = table.prettytable(fmt)
return ptable


Expand Down Expand Up @@ -258,9 +258,12 @@ def to_python(self):
items.append(dict(zip(self.columns, formatted_row)))
return items

def prettytable(self):
def prettytable(self, fmt='table'):
"""Returns a RICH table instance."""
table = rTable(title=self.title, box=box.SQUARE, header_style="bright_cyan")
box_style = box.SQUARE
if fmt == 'raw':
box_style = None
table = rTable(title=self.title, box=box_style, header_style="bright_cyan")
if self.sortby:
try:
# https://docs.python.org/3/howto/sorting.html#key-functions
Expand Down
6 changes: 6 additions & 0 deletions tests/CLI/helper_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ def test_format_output_table_invalid_sort(self):
formatting.format_output, t, 'table',
)

def test_format_raw_table(self):
test_table = formatting.Table(['col1', 'col2'])
test_table.add_row(['row1', 'row2'])
pretty_table = formatting.format_output(test_table, 'raw')
self.assertIsNone(pretty_table.box)


class TestTemplateArgs(testing.TestCase):

Expand Down