Fix curses error in print_colored at window boundaries

- Wrap all addch calls in try/except blocks
- Return early when hitting window boundary
- Prevents crash when content exceeds window dimensions
- Gracefully truncates output instead of throwing error
This commit is contained in:
pfl
2026-01-09 15:13:52 +01:00
parent 993ed4f051
commit 530c12d9c0

17
ui.py
View File

@ -46,7 +46,10 @@ def print_colored(window, message, attributes=0):
^0 = reset, ^1 = red, ^2 = green, ^3 = yellow, ^4 = blue, ^5 = cyan, ^6 = magenta, ^7 = white, ^8 = bold, ^9 = underline ^0 = reset, ^1 = red, ^2 = green, ^3 = yellow, ^4 = blue, ^5 = cyan, ^6 = magenta, ^7 = white, ^8 = bold, ^9 = underline
""" """
if not curses.has_colors: if not curses.has_colors:
window.addstr(message) try:
window.addstr(message)
except curses.error:
pass
return return
color = 0 color = 0
@ -69,13 +72,19 @@ def print_colored(window, message, attributes=0):
elif ord('1') <= val <= ord('6'): elif ord('1') <= val <= ord('6'):
color = val - ord('0') color = val - ord('0')
else: else:
window.addch('^', curses.color_pair(color) | (curses.A_BOLD if bold else 0) | (curses.A_UNDERLINE if underline else 0) | attributes) try:
window.addch(ch, curses.color_pair(color) | (curses.A_BOLD if bold else 0) | (curses.A_UNDERLINE if underline else 0) | attributes) window.addch('^', curses.color_pair(color) | (curses.A_BOLD if bold else 0) | (curses.A_UNDERLINE if underline else 0) | attributes)
window.addch(ch, curses.color_pair(color) | (curses.A_BOLD if bold else 0) | (curses.A_UNDERLINE if underline else 0) | attributes)
except curses.error:
return
parse_color = False parse_color = False
elif ch == '^': elif ch == '^':
parse_color = True parse_color = True
else: else:
window.addch(ch, curses.color_pair(color) | (curses.A_BOLD if bold else 0) | (curses.A_UNDERLINE if underline else 0) | attributes) try:
window.addch(ch, curses.color_pair(color) | (curses.A_BOLD if bold else 0) | (curses.A_UNDERLINE if underline else 0) | attributes)
except curses.error:
return
def update_autocomplete_display(window, current_input, first_word, words, ends_with_space): def update_autocomplete_display(window, current_input, first_word, words, ends_with_space):
""" """