@ -1,3 +1,4 @@
from shutil import get_terminal_size
from typing import List , Dict , Union
from typing import List , Dict , Union
from prettytable import PrettyTable
from prettytable import PrettyTable
@ -7,7 +8,12 @@ from slither.utils.colors import Colors
class MyPrettyTable :
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 . _field_names = field_names
self . _rows : List = [ ]
self . _rows : List = [ ]
self . _options : Dict = { }
self . _options : Dict = { }
@ -19,6 +25,17 @@ class MyPrettyTable:
else :
else :
self . _options [ " set_alignment " ] = [ ]
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 :
# 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 :
def add_row ( self , row : List [ Union [ str , List [ str ] ] ] ) - > None :
self . _rows . append ( row )
self . _rows . append ( row )
@ -28,6 +45,9 @@ class MyPrettyTable:
else :
else :
table = PrettyTable ( self . _field_names )
table = PrettyTable ( self . _field_names )
if self . max_width is not None :
table . max_table_width = self . max_width
for row in self . _rows :
for row in self . _rows :
table . add_row ( row )
table . add_row ( row )
if len ( self . _options [ " set_alignment " ] ) :
if len ( self . _options [ " set_alignment " ] ) :
@ -63,7 +83,5 @@ def make_pretty_table(
table_row = [ row ] + [ body [ row ] [ key ] for key in headers [ 1 : ] ]
table_row = [ row ] + [ body [ row ] [ key ] for key in headers [ 1 : ] ]
table . add_row ( table_row )
table . add_row ( table_row )
if totals :
if totals :
table . add_row (
table . add_row ( [ total_header ] + [ sum ( body [ row ] [ key ] for row in body ) for key in headers [ 1 : ] ] )
[ total_header ] + [ sum ( [ body [ row ] [ key ] for row in body ] ) for key in headers [ 1 : ] ]
)
return table
return table