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:
@@ -148,6 +148,12 @@ class StatusDisplay:
|
|||||||
color, label = phases.get(phase, (Colors.WHITE, " ? "))
|
color, label = phases.get(phase, (Colors.WHITE, " ? "))
|
||||||
return "{}{}{}".format(color, label, Colors.RESET)
|
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):
|
def draw_nail_panel_compact(self, nail_id, status):
|
||||||
"""Draw compact status panel for a single nail (for side-by-side layout)."""
|
"""Draw compact status panel for a single nail (for side-by-side layout)."""
|
||||||
if not status:
|
if not status:
|
||||||
@@ -267,31 +273,43 @@ class StatusDisplay:
|
|||||||
nail2_lines.append("")
|
nail2_lines.append("")
|
||||||
|
|
||||||
# Draw separate boxes for each nail side-by-side
|
# Draw separate boxes for each nail side-by-side
|
||||||
|
# Top border
|
||||||
lines.append("{}┌──────────────────────────┐ ┌──────────────────────────┐{}".format(
|
lines.append("{}┌──────────────────────────┐ ┌──────────────────────────┐{}".format(
|
||||||
Colors.CYAN, Colors.RESET))
|
Colors.CYAN, Colors.RESET))
|
||||||
lines.append("{}│ {}NAIL 1{:<17}│ │ {}NAIL 2{:<17}│{}".format(
|
|
||||||
Colors.CYAN,
|
# Header line with nail names
|
||||||
Colors.BRIGHT_RED + Colors.BOLD,
|
n1_header = "{}NAIL 1{}".format(Colors.BRIGHT_RED + Colors.BOLD, Colors.CYAN)
|
||||||
Colors.CYAN,
|
n2_header = "{}NAIL 2{}".format(Colors.BRIGHT_BLUE + Colors.BOLD, Colors.CYAN)
|
||||||
Colors.BRIGHT_BLUE + Colors.BOLD,
|
# Manually pad with spaces after stripping ANSI codes
|
||||||
Colors.CYAN,
|
n1_spaces = 22 - len(self.strip_ansi(n1_header))
|
||||||
Colors.RESET
|
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(
|
lines.append("{}├──────────────────────────┤ ├──────────────────────────┤{}".format(
|
||||||
Colors.CYAN, Colors.RESET))
|
Colors.CYAN, Colors.RESET))
|
||||||
|
|
||||||
|
# Content lines
|
||||||
for n1_line, n2_line in zip(nail1_lines, nail2_lines):
|
for n1_line, n2_line in zip(nail1_lines, nail2_lines):
|
||||||
# Pad lines to 24 chars for each box with 5 spaces between
|
# Calculate visible length without ANSI codes
|
||||||
n1_padded = "{:<24}".format(n1_line[:24])
|
n1_visible = self.strip_ansi(n1_line)[:22]
|
||||||
n2_padded = "{:<24}".format(n2_line[:24])
|
n2_visible = self.strip_ansi(n2_line)[:22]
|
||||||
lines.append("{}│ {} │ │ {} │{}".format(
|
n1_spaces = 22 - len(n1_visible)
|
||||||
|
n2_spaces = 22 - len(n2_visible)
|
||||||
|
|
||||||
|
lines.append("{}│ {}{:<{}}│ │ {}{:<{}}│{}".format(
|
||||||
Colors.CYAN,
|
Colors.CYAN,
|
||||||
n1_padded,
|
n1_line,
|
||||||
n2_padded,
|
"",
|
||||||
|
n1_spaces,
|
||||||
|
n2_line,
|
||||||
|
"",
|
||||||
|
n2_spaces,
|
||||||
Colors.RESET
|
Colors.RESET
|
||||||
))
|
))
|
||||||
|
|
||||||
# Footer
|
# Bottom border
|
||||||
lines.append("{}└──────────────────────────┘ └──────────────────────────┘{}".format(
|
lines.append("{}└──────────────────────────┘ └──────────────────────────┘{}".format(
|
||||||
Colors.CYAN, Colors.RESET))
|
Colors.CYAN, Colors.RESET))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user