Skip to content

Commit 9493bfd

Browse files
committed
menu: Use consistent left-biased centering for all UI elements
1 parent 42bb1cc commit 9493bfd

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

common/menu.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ char *config_entry_editor(const char *title, const char *orig_entry) {
231231
print("\n");
232232
if (menu_branding[0] != '\0') {
233233
terms[0]->get_cursor_pos(terms[0], &x, &y);
234-
set_cursor_pos_helper(terms[0]->cols / 2 - DIV_ROUNDUP(strlen(menu_branding), 2, panic(false, "Alignment overflow")), y);
234+
set_cursor_pos_helper((terms[0]->cols - strlen(menu_branding)) / 2, y);
235235
print("\e[3%sm%s\e[0m", menu_branding_colour, menu_branding);
236236
print("\n\n");
237237
}
@@ -259,7 +259,7 @@ char *config_entry_editor(const char *title, const char *orig_entry) {
259259
display_length = max_title;
260260
truncated = true;
261261
}
262-
if (i == (terms[0]->cols / 2) - DIV_ROUNDUP(display_length, 2, panic(false, "Alignment overflow")) - 1 - 1) {
262+
if (i == (terms[0]->cols - display_length - 4) / 2) {
263263
if (truncated) {
264264
print(serial ? "|%S...|" : "┤%S...├", title, (size_t)(display_length - 3));
265265
} else {
@@ -445,7 +445,7 @@ char *config_entry_editor(const char *title, const char *orig_entry) {
445445
// FALLTHRU
446446
default:
447447
if (overflow_msg != NULL
448-
&& i == (terms[0]->cols / 2) - DIV_ROUNDUP(overflow_len, 2, panic(false, "Alignment overflow")) - 1 - 1) {
448+
&& i == (terms[0]->cols - overflow_len - 4) / 2) {
449449
print(serial ? "|" : "┤");
450450
print("\e[31m%s\e[0m", overflow_msg);
451451
print(serial ? "|" : "├");
@@ -1360,7 +1360,7 @@ noreturn void _menu(bool first_run) {
13601360
print("\n");
13611361
if (menu_branding[0] != '\0') {
13621362
terms[0]->get_cursor_pos(terms[0], &x, &y);
1363-
set_cursor_pos_helper(terms[0]->cols / 2 - DIV_ROUNDUP(strlen(menu_branding), 2, panic(false, "Alignment overflow")), y);
1363+
set_cursor_pos_helper((terms[0]->cols - strlen(menu_branding)) / 2, y);
13641364
print("\e[3%sm%s\e[0m", menu_branding_colour, menu_branding);
13651365
print("\n\n\n\n");
13661366
}
@@ -1377,7 +1377,7 @@ noreturn void _menu(bool first_run) {
13771377
} else {
13781378
msg = "[config file not found]";
13791379
}
1380-
set_cursor_pos_helper(terms[0]->cols / 2 - strlen(msg) / 2, terms[0]->rows / 2);
1380+
set_cursor_pos_helper((terms[0]->cols - strlen(msg)) / 2, terms[0]->rows / 2);
13811381
print("%s\n", msg);
13821382
}
13831383

@@ -1386,9 +1386,7 @@ noreturn void _menu(bool first_run) {
13861386
&selected_menu_entry, &max_tree_len, &max_tree_height);
13871387

13881388
if (max_entries != 0) {
1389-
size_t half_cols = terms[0]->cols / 2;
1390-
size_t half_tree = DIV_ROUNDUP(max_tree_len, 2, panic(false, "Alignment overflow"));
1391-
size_t tree_prefix_len = (half_cols > half_tree + 2) ? (half_cols - half_tree - 2) : 1;
1389+
size_t tree_prefix_len = (terms[0]->cols > max_tree_len + 2) ? (terms[0]->cols - max_tree_len - 2) / 2 : 1;
13921390
char *tree_prefix = ext_mem_alloc(tree_prefix_len + 1);
13931391
memset(tree_prefix, ' ', tree_prefix_len);
13941392

@@ -1414,12 +1412,12 @@ noreturn void _menu(bool first_run) {
14141412

14151413
if (max_entries != 0) {
14161414
if (tree_offset > 0) {
1417-
set_cursor_pos_helper(terms[0]->cols / 2 - 1, 3 + header_offset);
1415+
set_cursor_pos_helper((terms[0]->cols - 3) / 2, 3 + header_offset);
14181416
print(serial ? "^^^" : "↑↑↑");
14191417
}
14201418

14211419
if (tree_offset + (terms[0]->rows - 8 - header_offset) < max_entries) {
1422-
set_cursor_pos_helper(terms[0]->cols / 2 - 1, terms[0]->rows - 4);
1420+
set_cursor_pos_helper((terms[0]->cols - 3) / 2, terms[0]->rows - 4);
14231421
print(serial ? "vvv" : "↓↓↓");
14241422
}
14251423
}

0 commit comments

Comments
 (0)