@@ -2055,13 +2055,25 @@ def repl(s):
20552055 )
20562056
20572057 # break headers and cells into lines
2058- def wrap (text : str , width : int ) -> list [str ]:
2059- return list (apsw .unicode .text_wrap (text , width )) or ["" ]
2058+ def wrap (text : str , width : int , justify : apsw .unicode .Justify , hyphen : str ) -> list [str ]:
2059+ return list (apsw .unicode .text_wrap (text , width , justify = justify , hyphen = hyphen )) or ["" ]
2060+
2061+ # special formatting
2062+ formats = {
2063+ int : {"justify" : apsw .unicode .Justify .RIGHT , "hyphen" : "" },
2064+ float : {"justify" : apsw .unicode .Justify .LEFT , "hyphen" : "" },
2065+ "header" : {"justify" : apsw .unicode .Justify .CENTER , "hyphen" : "-" },
2066+ "_" : {"justify" : apsw .unicode .Justify .LEFT , "hyphen" : "-" },
2067+ }
2068+
2069+ colnames = [wrap (colnames [i ], colwidths [i ], ** formats ["header" ]) for i in range (len (colwidths ))] # type: ignore
20602070
2061- colnames = [wrap (colnames [i ], colwidths [i ]) for i in range (len (colwidths ))] # type: ignore
20622071 for row in rows :
20632072 for i , (text , t ) in enumerate (row ): # type: ignore[misc]
2064- row [i ] = (wrap (text , colwidths [i ]), t ) # type: ignore
2073+ row [i ] = (
2074+ wrap (text , colwidths [i ], ** formats .get (t , formats ["_" ])),
2075+ t ,
2076+ ) # type: ignore
20652077
20662078 ## output
20672079 # are any cells more than one line?
@@ -2085,29 +2097,22 @@ def do_bar(chars: str) -> None:
20852097 line += chars [2 ]
20862098 out_lines .append (line )
20872099
2088- def do_row (row , sep : str , * , centre : bool = False , header : bool = False ) -> None :
2100+ def do_row (row , sep : str , * , header : bool = False ) -> None :
20892101 for n in range (max (len (cell [0 ]) for cell in row )):
20902102 line = sep
20912103 for i , (cell , t ) in enumerate (row ):
2092- text = cell [n ] if n < len (cell ) else ""
2093- text = " " + text .rstrip () + " "
2094- lt = apsw .unicode .text_width (text )
2095- extra = " " * max (colwidths [i ] + 2 - lt , 0 )
2096- if centre :
2097- lpad = extra [: len (extra ) // 2 ]
2098- rpad = extra [len (extra ) // 2 :]
2099- else :
2100- lpad = ""
2101- rpad = extra
2104+ text = cell [n ] if n < len (cell ) else " " * colwidths [i ]
2105+ # we do a space on each side
2106+ text = " " + text + " "
21022107 if header :
2103- text = colour_wrap (lpad + text + rpad , None , header = True )
2108+ text = colour_wrap (text , None , header = True )
21042109 else :
2105- text = lpad + colour_wrap (text , t ) + rpad
2110+ text = colour_wrap (text , t )
21062111 line += text + sep
21072112 out_lines .append (line )
21082113
21092114 do_bar ("╭─┬╮" if use_unicode else "+-++" )
2110- do_row ([(c , None ) for c in colnames ], "│" if use_unicode else "|" , centre = True , header = True )
2115+ do_row ([(c , None ) for c in colnames ], "│" if use_unicode else "|" , header = True )
21112116
21122117 # rows
21132118 which = 0
0 commit comments