Skip to content

Commit

Permalink
Add a new parameter max_width to MyPrettyTable to enhance its displ…
Browse files Browse the repository at this point in the history
…ay for CLI usage

This PR adds a `max_width` parameter to MyPrettyTable to restrict the maximum width of its underlying table.

The value can be an integer, "max" (detect automatically the correct width) or None (no width limit)
  • Loading branch information
DarkaMaul committed Apr 15, 2024
1 parent 57743a4 commit e11d4f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
6 changes: 4 additions & 2 deletions slither/utils/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ def output_detectors(detector_classes: List[Type[AbstractDetector]]) -> None:
impact = detector.IMPACT
confidence = classification_txt[detector.CONFIDENCE]
detectors_list.append((argument, help_info, impact, confidence))
table = MyPrettyTable(["Num", "Check", "What it Detects", "Impact", "Confidence"])
table = MyPrettyTable(
["Num", "Check", "What it Detects", "Impact", "Confidence"], max_width="max"
)

# Sort by impact, confidence, and name
detectors_list = sorted(
Expand Down Expand Up @@ -388,7 +390,7 @@ def output_printers(printer_classes: List[Type[AbstractPrinter]]) -> None:
argument = printer.ARGUMENT
help_info = printer.HELP
printers_list.append((argument, help_info))
table = MyPrettyTable(["Num", "Printer", "What it Does"])
table = MyPrettyTable(["Num", "Printer", "What it Does"], max_width="max")

# Sort by impact, confidence, and name
printers_list = sorted(printers_list, key=lambda element: (element[0]))
Expand Down
27 changes: 22 additions & 5 deletions slither/utils/myprettytable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Dict, Union
from shutil import get_terminal_size
from typing import List, Dict, Union, Literal

from prettytable import PrettyTable
from prettytable.colortable import ColorTable, Themes
Expand All @@ -7,7 +8,12 @@


class MyPrettyTable:
def __init__(self, field_names: List[str], pretty_align: bool = True): # TODO: True by default?
def __init__(
self,
field_names: List[str],
pretty_align: bool = True,
max_width: Union[int, Literal["max"], None] = None,
):
self._field_names = field_names
self._rows: List = []
self._options: Dict = {}
Expand All @@ -19,6 +25,16 @@ def __init__(self, field_names: List[str], pretty_align: bool = True): # TODO:
else:
self._options["set_alignment"] = []

self.max_width = None
if max_width == "max":
# We use (0,0) as a fallback to detect if we are not attached to a terminal
# In this case, we fall back to the default behavior (i.e. printing as much as possible)
terminal_column = get_terminal_size((0, 0)).columns
if terminal_column != 0:
self.max_width = terminal_column - 2
else:
self.max_width = max_width

def add_row(self, row: List[Union[str, List[str]]]) -> None:
self._rows.append(row)

Expand All @@ -28,6 +44,9 @@ def to_pretty_table(self) -> PrettyTable:
else:
table = PrettyTable(self._field_names)

if self.max_width is not None:
table.max_table_width = self.max_width

for row in self._rows:
table.add_row(row)
if len(self._options["set_alignment"]):
Expand Down Expand Up @@ -63,7 +82,5 @@ def make_pretty_table(
table_row = [row] + [body[row][key] for key in headers[1:]]
table.add_row(table_row)
if totals:
table.add_row(
[total_header] + [sum([body[row][key] for row in body]) for key in headers[1:]]
)
table.add_row([total_header] + [sum(body[row][key] for row in body) for key in headers[1:]])
return table

0 comments on commit e11d4f2

Please sign in to comment.