MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / print_table

Method print_table

script/btcli.py:119–163  ·  view source on GitHub ↗

打印表格

(headers: List[str], rows: List[List[str]], title: str = None)

Source from the content-addressed store, hash-verified

117
118 @staticmethod
119 def print_table(headers: List[str], rows: List[List[str]], title: str = None):
120 """打印表格"""
121 if title:
122 print(f"\n{Colors.header(title)}")
123
124 if not rows:
125 print(Colors.warning(" 暂无数据"))
126 return
127
128 # 计算每列的最大显示宽度(考虑中英文混合)
129 col_widths = []
130 for i, header in enumerate(headers):
131 # 表头宽度
132 header_width = TablePrinter.get_string_display_width(header)
133 max_width = header_width
134
135 # 检查数据行中该列的最大宽度
136 for row in rows:
137 cell_width = TablePrinter.get_string_display_width(str(row[i]))
138 max_width = max(max_width, cell_width)
139
140 col_widths.append(max_width)
141
142 # 打印表头
143 header_parts = []
144 for i, h in enumerate(headers):
145 # 对表头进行填充,考虑中英文混合宽度
146 padded_header = TablePrinter.pad_string_with_chinese(h, col_widths[i])
147 header_parts.append(padded_header)
148
149 header_line = " | ".join(header_parts)
150 print(f"\n {Colors.BOLD}{header_line}{Colors.ENDC}")
151 print(" " + "-" * len(header_line.replace('\033', ''))) # 移除ANSI转义字符后计算长度
152
153 # 打印数据行
154 for row in rows:
155 row_parts = []
156 for i, cell in enumerate(row):
157 padded_cell = TablePrinter.pad_string_with_chinese(str(cell), col_widths[i])
158 row_parts.append(padded_cell)
159
160 row_line = " | ".join(row_parts)
161 print(f" {row_line}")
162
163 print()
164
165
166# ============================================

Callers 4

list_sitesMethod · 0.80
list_databasesMethod · 0.80
list_ftpMethod · 0.80
show_scan_resultsMethod · 0.80

Calls 7

headerMethod · 0.80
warningMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected