Fix spacing calculation to account for ANSI color codes

- Added strip_ansi() helper function to remove color codes before length calc
- Fixed padding logic to calculate visible character length correctly
- ANSI codes no longer mess up the box alignment
- Both boxes should now align properly regardless of color content
- Deployed and verified on Pi
This commit is contained in:
2026-03-12 04:28:48 +00:00
parent 12964fa3ab
commit 2853fa3f8a
+33 -15
View File
@@ -148,6 +148,12 @@ class StatusDisplay:
color, label = phases.get(phase, (Colors.WHITE, " ? "))
return "{}{}{}".format(color, label, Colors.RESET)
def strip_ansi(self, text):
"""Remove ANSI color codes from text for length calculation."""
import re
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
return ansi_escape.sub('', text)
def draw_nail_panel_compact(self, nail_id, status):
"""Draw compact status panel for a single nail (for side-by-side layout)."""
if not status:
@@ -267,31 +273,43 @@ class StatusDisplay:
nail2_lines.append("")
# Draw separate boxes for each nail side-by-side
# Top border
lines.append("{}┌──────────────────────────┐ ┌──────────────────────────┐{}".format(
Colors.CYAN, Colors.RESET))
lines.append("{}{}NAIL 1{:<17}│ │ {}NAIL 2{:<17}{}".format(
Colors.CYAN,
Colors.BRIGHT_RED + Colors.BOLD,
Colors.CYAN,
Colors.BRIGHT_BLUE + Colors.BOLD,
Colors.CYAN,
Colors.RESET
))
# Header line with nail names
n1_header = "{}NAIL 1{}".format(Colors.BRIGHT_RED + Colors.BOLD, Colors.CYAN)
n2_header = "{}NAIL 2{}".format(Colors.BRIGHT_BLUE + Colors.BOLD, Colors.CYAN)
# Manually pad with spaces after stripping ANSI codes
n1_spaces = 22 - len(self.strip_ansi(n1_header))
n2_spaces = 22 - len(self.strip_ansi(n2_header))
lines.append("{}{}{:<{}}│ │ {}{:<{}}│{}".format(
Colors.CYAN, n1_header, "", n1_spaces, n2_header, "", n2_spaces, Colors.RESET))
# Middle border
lines.append("{}├──────────────────────────┤ ├──────────────────────────┤{}".format(
Colors.CYAN, Colors.RESET))
# Content lines
for n1_line, n2_line in zip(nail1_lines, nail2_lines):
# Pad lines to 24 chars for each box with 5 spaces between
n1_padded = "{:<24}".format(n1_line[:24])
n2_padded = "{:<24}".format(n2_line[:24])
lines.append("{}{} │ │ {}{}".format(
# Calculate visible length without ANSI codes
n1_visible = self.strip_ansi(n1_line)[:22]
n2_visible = self.strip_ansi(n2_line)[:22]
n1_spaces = 22 - len(n1_visible)
n2_spaces = 22 - len(n2_visible)
lines.append("{}{}{:<{}}│ │ {}{:<{}}│{}".format(
Colors.CYAN,
n1_padded,
n2_padded,
n1_line,
"",
n1_spaces,
n2_line,
"",
n2_spaces,
Colors.RESET
))
# Footer
# Bottom border
lines.append("{}└──────────────────────────┘ └──────────────────────────┘{}".format(
Colors.CYAN, Colors.RESET))