dfd
This commit is contained in:
@@ -118,6 +118,16 @@ class ResultRow:
|
||||
def add_column(self, name: str, value: Any) -> None:
|
||||
"""Add a column to this row."""
|
||||
str_value = str(value) if value is not None else ""
|
||||
|
||||
# Normalize extension columns globally and cap to 5 characters
|
||||
if str(name).strip().lower() == "ext":
|
||||
str_value = str_value.strip().lstrip(".")
|
||||
for idx, ch in enumerate(str_value):
|
||||
if not ch.isalnum():
|
||||
str_value = str_value[:idx]
|
||||
break
|
||||
str_value = str_value[:5]
|
||||
|
||||
self.columns.append(ResultColumn(name, str_value))
|
||||
|
||||
def get_column(self, name: str) -> Optional[str]:
|
||||
@@ -618,48 +628,78 @@ class ResultTable:
|
||||
for row in self.rows:
|
||||
for col in row.columns:
|
||||
col_name = col.name
|
||||
value_width = len(col.value)
|
||||
if col_name.lower() == "ext":
|
||||
value_width = min(value_width, 5)
|
||||
col_widths[col_name] = max(
|
||||
col_widths.get(col_name, 0),
|
||||
len(col.name),
|
||||
len(col.value)
|
||||
value_width
|
||||
)
|
||||
|
||||
# Calculate row number column width
|
||||
num_width = len(str(len(self.rows))) + 1 # +1 for padding
|
||||
|
||||
lines = []
|
||||
|
||||
# Add title if present
|
||||
if self.title:
|
||||
lines.append("=" * self.title_width)
|
||||
lines.append(self.title.center(self.title_width))
|
||||
lines.append("=" * self.title_width)
|
||||
|
||||
# Preserve column order
|
||||
column_names = list(col_widths.keys())
|
||||
|
||||
def capped_width(name: str) -> int:
|
||||
cap = 5 if name.lower() == "ext" else 90
|
||||
return min(col_widths[name], cap)
|
||||
|
||||
widths = [num_width] + [capped_width(name) for name in column_names]
|
||||
base_inner_width = sum(widths) + (len(widths) - 1) * 3 # account for " | " separators
|
||||
|
||||
# Compute final table width (with side walls) to accommodate headers/titles
|
||||
table_width = base_inner_width + 2 # side walls
|
||||
if self.title:
|
||||
table_width = max(table_width, len(self.title) + 2)
|
||||
if self.header_lines:
|
||||
lines.extend(self.header_lines)
|
||||
|
||||
table_width = max(table_width, max(len(line) for line in self.header_lines) + 2)
|
||||
|
||||
def wrap(text: str) -> str:
|
||||
"""Wrap content with side walls and pad to table width."""
|
||||
if len(text) > table_width - 2:
|
||||
text = text[: table_width - 5] + "..." # keep walls intact
|
||||
return "|" + text.ljust(table_width - 2) + "|"
|
||||
|
||||
lines = []
|
||||
|
||||
# Title block
|
||||
if self.title:
|
||||
lines.append("|" + "=" * (table_width - 2) + "|")
|
||||
lines.append(wrap(self.title.center(table_width - 2)))
|
||||
lines.append("|" + "=" * (table_width - 2) + "|")
|
||||
|
||||
# Optional header metadata lines
|
||||
for meta in self.header_lines:
|
||||
lines.append(wrap(meta))
|
||||
|
||||
# Add header with # column
|
||||
header_parts = ["#".ljust(num_width)]
|
||||
separator_parts = ["-" * num_width]
|
||||
for col_name in col_widths:
|
||||
width = min(col_widths[col_name], 90) # Cap column width (increased for expanded titles)
|
||||
for col_name in column_names:
|
||||
width = capped_width(col_name)
|
||||
header_parts.append(col_name.ljust(width))
|
||||
separator_parts.append("-" * width)
|
||||
|
||||
lines.append(" | ".join(header_parts))
|
||||
lines.append("-+-".join(separator_parts))
|
||||
|
||||
|
||||
lines.append(wrap(" | ".join(header_parts)))
|
||||
lines.append(wrap("-+-".join(separator_parts)))
|
||||
|
||||
# Add rows with row numbers
|
||||
for row_num, row in enumerate(self.rows, 1):
|
||||
row_parts = [str(row_num).ljust(num_width)]
|
||||
for col_name in col_widths:
|
||||
width = min(col_widths[col_name], 90) # Increased cap for expanded titles
|
||||
for col_name in column_names:
|
||||
width = capped_width(col_name)
|
||||
col_value = row.get_column(col_name) or ""
|
||||
if len(col_value) > width:
|
||||
col_value = col_value[:width - 3] + "..."
|
||||
col_value = col_value[: width - 3] + "..."
|
||||
row_parts.append(col_value.ljust(width))
|
||||
lines.append(" | ".join(row_parts))
|
||||
|
||||
lines.append(wrap(" | ".join(row_parts)))
|
||||
|
||||
# Bottom border to close the rectangle
|
||||
lines.append("|" + "=" * (table_width - 2) + "|")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def format_compact(self) -> str:
|
||||
|
||||
Reference in New Issue
Block a user