Skip to content

Commit bc72c3b

Browse files
committed
break out color definitions
1 parent 559618a commit bc72c3b

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

settings.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
from ui.menus import generate_menu_from_protobuf
66
from input_handlers import get_bool_selection, get_repeated_input, get_user_input, get_enum_input, get_fixed32_input
77
from save_to_radio import save_changes
8+
from ui.colors import setup_colors
89

910
import logging
1011

1112

1213
def display_menu(current_menu, menu_path, selected_index, show_save_option):
1314
global menu_win
15+
1416
# Calculate the dynamic height based on the number of menu items
1517
num_items = len(current_menu) + (1 if show_save_option else 0) # Add 1 for the "Save Changes" option if applicable
1618
height = min(curses.LINES - 2, num_items + 5) # Ensure the menu fits within the terminal height
@@ -34,14 +36,17 @@ def display_menu(current_menu, menu_path, selected_index, show_save_option):
3436
for idx, option in enumerate(current_menu):
3537
field_info = current_menu[option]
3638
current_value = field_info[1] if isinstance(field_info, tuple) else ""
37-
display_option = f"{option}"[:width // 2 - 2] # Truncate option name if too long
39+
display_option = f"{option}"[:width // 2 - 2] # Truncate option name if too long``
3840
display_value = f"{current_value}"[:width // 2 - 4] # Truncate value if too long
3941

4042
try:
43+
# Use red color for "Reboot" or "Shutdown"
44+
color = curses.color_pair(5) if option in ["Reboot", "Reset Node DB", "Shutdown", "Factory Reset"] else curses.color_pair(1)
45+
4146
if idx == selected_index:
42-
menu_win.addstr(idx + 3, 4, f"{display_option:<{width // 2 - 2}} {display_value}", curses.A_REVERSE)
47+
menu_win.addstr(idx + 3, 4, f"{display_option:<{width // 2 - 2}} {display_value}", curses.A_REVERSE | color)
4348
else:
44-
menu_win.addstr(idx + 3, 4, f"{display_option:<{width // 2 - 2}} {display_value}")
49+
menu_win.addstr(idx + 3, 4, f"{display_option:<{width // 2 - 2}} {display_value}", color)
4550
except curses.error:
4651
pass
4752

@@ -50,9 +55,9 @@ def display_menu(current_menu, menu_path, selected_index, show_save_option):
5055
save_option = "Save Changes"
5156
save_position = height - 2
5257
if selected_index == len(current_menu):
53-
menu_win.addstr(save_position, (width - len(save_option)) // 2, save_option, curses.A_REVERSE)
58+
menu_win.addstr(save_position, (width - len(save_option)) // 2, save_option, curses.color_pair(2) | curses.A_REVERSE)
5459
else:
55-
menu_win.addstr(save_position, (width - len(save_option)) // 2, save_option)
60+
menu_win.addstr(save_position, (width - len(save_option)) // 2, save_option, curses.color_pair(2))
5661

5762
menu_win.refresh()
5863

@@ -64,7 +69,6 @@ def settings_menu(sdscr, interface):
6469
selected_index = 0
6570
modified_settings = {}
6671

67-
6872
while True:
6973
options = list(current_menu.keys())
7074

@@ -229,7 +233,7 @@ def main(stdscr):
229233
level=logging.INFO, # DEBUG, INFO, WARNING, ERROR, CRITICAL)
230234
format="%(asctime)s - %(levelname)s - %(message)s"
231235
)
232-
236+
setup_colors()
233237
curses.curs_set(0)
234238
stdscr.keypad(True)
235239

ui/colors.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import curses
2+
3+
def setup_colors():
4+
curses.start_color()
5+
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
6+
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
7+
curses.init_pair(3, curses.COLOR_CYAN, curses.COLOR_BLACK)
8+
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
9+
curses.init_pair(5, curses.COLOR_RED, curses.COLOR_BLACK)

ui/curses_ui.py

+11-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from settings import settings_menu
66
from message_handlers.tx_handler import send_message, send_traceroute
77
import ui.dialog
8-
8+
from ui.colors import setup_colors
99

1010
def add_notification(channel_number):
1111
handle_notification(channel_number, add=True)
@@ -36,8 +36,7 @@ def draw_debug(value):
3636
function_win.refresh()
3737

3838
def draw_splash(stdscr):
39-
curses.start_color()
40-
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) # Green text on black background
39+
setup_colors()
4140
curses.curs_set(0)
4241

4342
stdscr.clear()
@@ -50,9 +49,9 @@ def draw_splash(stdscr):
5049
start_x = width // 2 - len(message_1) // 2
5150
start_x2 = width // 2 - len(message_4) // 2
5251
start_y = height // 2 - 1
53-
stdscr.addstr(start_y, start_x, message_1, curses.color_pair(1) | curses.A_BOLD)
54-
stdscr.addstr(start_y+1, start_x-1, message_2, curses.color_pair(1) | curses.A_BOLD)
55-
stdscr.addstr(start_y+2, start_x-2, message_3, curses.color_pair(1) | curses.A_BOLD)
52+
stdscr.addstr(start_y, start_x, message_1, curses.color_pair(2) | curses.A_BOLD)
53+
stdscr.addstr(start_y+1, start_x-1, message_2, curses.color_pair(2) | curses.A_BOLD)
54+
stdscr.addstr(start_y+2, start_x-2, message_3, curses.color_pair(2) | curses.A_BOLD)
5655
stdscr.addstr(start_y+4, start_x2, message_4)
5756
stdscr.box()
5857
stdscr.refresh()
@@ -78,10 +77,10 @@ def draw_channel_list():
7877
truncated_channel = channel[:win_width - 5] + '-' if len(channel) > win_width - 5 else channel
7978
if i < win_height - 2 : # Check if there is enough space in the window
8079
if start_index + i == globals.selected_channel and globals.current_window == 0:
81-
channel_win.addstr(i + 1, 1, truncated_channel + notification, curses.color_pair(3))
80+
channel_win.addstr(i + 1, 1, truncated_channel + notification, curses.color_pair(1) | curses.A_REVERSE)
8281
remove_notification(globals.selected_channel)
8382
else:
84-
channel_win.addstr(i + 1, 1, truncated_channel + notification, curses.color_pair(4))
83+
channel_win.addstr(i + 1, 1, truncated_channel + notification, curses.color_pair(1))
8584
channel_win.box()
8685
channel_win.refresh()
8786

@@ -133,9 +132,9 @@ def draw_messages_window():
133132
for line in wrapped_lines:
134133
# Highlight the row if it's the selected message
135134
if index == globals.selected_message and globals.current_window == 1:
136-
color = curses.color_pair(3) # Highlighted row color
135+
color = curses.A_REVERSE # Highlighted row color
137136
else:
138-
color = curses.color_pair(1) if prefix.startswith(globals.sent_message_prefix) else curses.color_pair(2)
137+
color = curses.color_pair(4) if prefix.startswith(globals.sent_message_prefix) else curses.color_pair(3)
139138
messages_win.addstr(row, 1, line, color)
140139
row += 1
141140

@@ -153,9 +152,9 @@ def draw_node_list():
153152
for i, node in enumerate(globals.node_list[start_index:], start=1):
154153
if i < win_height - 1 : # Check if there is enough space in the window
155154
if globals.selected_node + 1 == start_index + i and globals.current_window == 2:
156-
nodes_win.addstr(i, 1, get_name_from_number(node, "long"), curses.color_pair(3))
155+
nodes_win.addstr(i, 1, get_name_from_number(node, "long"), curses.color_pair(1) | curses.A_REVERSE)
157156
else:
158-
nodes_win.addstr(i, 1, get_name_from_number(node, "long"), curses.color_pair(4))
157+
nodes_win.addstr(i, 1, get_name_from_number(node, "long"), curses.color_pair(1))
159158

160159
nodes_win.box()
161160
nodes_win.refresh()
@@ -246,14 +245,6 @@ def main_ui(stdscr):
246245
stdscr.keypad(True)
247246
get_channels()
248247

249-
# Initialize colors
250-
curses.start_color()
251-
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
252-
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
253-
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
254-
curses.init_pair(4, curses.COLOR_WHITE, curses.COLOR_BLACK)
255-
curses.init_pair(5, curses.COLOR_RED, curses.COLOR_BLACK)
256-
257248
# Calculate window max dimensions
258249
height, width = stdscr.getmaxyx()
259250

ui/dialog.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def dialog(stdscr, title, message):
2525
win.addstr(2 + i, 2, l)
2626

2727
# Add button
28-
win.addstr(dialog_height - 2, (dialog_width - 4) // 2, " Ok ", curses.color_pair(3))
28+
win.addstr(dialog_height - 2, (dialog_width - 4) // 2, " Ok ", curses.color_pair(1) | curses.A_REVERSE)
2929

3030
# Refresh dialog window
3131
win.refresh()

0 commit comments

Comments
 (0)