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

Add a new parameter max_width to MyPrettyTable #2426

Merged
merged 10 commits into from
Jul 18, 2024
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
python_requires=">=3.8",
install_requires=[
"packaging",
"prettytable>=3.3.0",
"prettytable>=3.10.2",
"pycryptodome>=3.4.6",
"crytic-compile>=0.3.7,<0.4.0",
# "crytic-compile@git+https://github.com/crytic/crytic-compile.git@master#egg=crytic-compile",
Expand Down
4 changes: 2 additions & 2 deletions slither/printers/summary/loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

class LocPrinter(AbstractPrinter):
ARGUMENT = "loc"
HELP = """Count the total number lines of code (LOC), source lines of code (SLOC), \
and comment lines of code (CLOC) found in source files (SRC), dependencies (DEP), \
HELP = """Count the total number lines of code (LOC), source lines of code (SLOC),
and comment lines of code (CLOC) found in source files (SRC), dependencies (DEP),
and test files (TEST)."""

WIKI = "https://github.com/trailofbits/slither/wiki/Printer-documentation#loc"
Expand Down
4 changes: 3 additions & 1 deletion slither/utils/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,10 @@ def output_printers(printer_classes: List[Type[AbstractPrinter]]) -> None:
printers_list = sorted(printers_list, key=lambda element: (element[0]))
idx = 1
for (argument, help_info) in printers_list:
table.add_row([str(idx), argument, help_info])
# Clean multi line HELP info
table.add_row([str(idx), argument, " ".join(x.strip() for x in help_info.splitlines())])
idx = idx + 1

print(table)


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

Check warning on line 2 in slither/utils/myprettytable.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

W0611: Unused Literal imported from typing (unused-import)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused import.

The Literal import from typing is unused and should be removed to clean up the code.

- from typing import List, Dict, Union, Literal
+ from typing import List, Dict, Union
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
from typing import List, Dict, Union, Literal
from typing import List, Dict, Union
Tools
Ruff

2-2: typing.Literal imported but unused

Remove unused import: typing.Literal

(F401)

GitHub Check: Lint Code Base

[warning] 2-2:
W0611: Unused Literal imported from typing (unused-import)


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, None] = "max", # Default value is "max"
):
self._field_names = field_names
self._rows: List = []
self._options: Dict = {}
Expand All @@ -19,6 +25,16 @@
else:
self._options["set_alignment"] = []

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:
# We reduce slightly the max-width to take into account inconsistencies in terminals
self.max_width = terminal_column - 3
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 @@
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 @@
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
Loading