From 2e1a74d745bcd2e14ff9db7ef09233e1ac3ebef2 Mon Sep 17 00:00:00 2001 From: sbkelley Date: Thu, 18 Jun 2026 23:47:38 -0400 Subject: [PATCH] Add background color functionality to ATTR() * color_pair_idx no longer compiled in if USE_NCURSES_COLOR not defined * rename special_end to second_cursor * Invert if statement * Update indentation * Flatten the other if statement * Add fg,bg delimiter Update dgamelaunch.conf hints --- dgamelaunch.c | 114 ++++++++++++++++++++++---------------- dgamelaunch.h | 2 + examples/dgamelaunch.conf | 17 ++++-- 3 files changed, 79 insertions(+), 54 deletions(-) diff --git a/dgamelaunch.c b/dgamelaunch.c index 27babad..bb81e21 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -633,8 +633,30 @@ int remap_attr_string(char *s) case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { - int num = atoi(s); - if (num >= 0 && num <= 129) + char *num_delimiter; + int num; + int other_num; + + if ((num_delimiter = strchr(s, ','))) + { + *num_delimiter = '\0'; + num = atoi(s); + s = num_delimiter + 1; + other_num = atoi(s); + + if (other_num >= 0 && other_num <= 7) + { + // If there's a valid background color, set that, overriding whatever + // came from the first number (but leaving the foreground in place) + num &= 15; + num |= other_num << 4; + } + + } + else // no ',' delimiter + num = atoi(s); + + if (num >= -1 && num <= 128) attr |= COLOR_PAIR(num+1); } break; @@ -666,7 +688,7 @@ int remap_attr_string(char *s) void drawbanner(struct dg_banner *ban) { unsigned int i; - char *edit_cursor, *special_end, *next_delimiter; + char *edit_cursor, *second_cursor, *next_delimiter; int attr = 0, oattr = 0; if (!ban) @@ -681,53 +703,47 @@ void drawbanner(struct dg_banner *ban) do { // while (line_incomplete) line_incomplete = 0; - if ((edit_cursor = strstr(output_cursor, "$ATTR("))) + if (!(edit_cursor = strstr(output_cursor, "$ATTR("))) { - if ((special_end = strstr(edit_cursor, ")"))) - { - int delimited = 0; - char *next_attr_char; - line_incomplete = 1; - oattr = attr; - attr = A_NORMAL; - *edit_cursor = *special_end = '\0'; - edit_cursor += 6; - next_attr_char = edit_cursor; - do // while(delimited) - { - delimited = 0; - next_delimiter = strchr(edit_cursor, ';'); - if (next_delimiter && *next_delimiter) - { - delimited = 1; - next_attr_char = next_delimiter; - *next_attr_char = '\0'; - next_attr_char++; - } - attr |= remap_attr_string(edit_cursor); - edit_cursor = next_attr_char; - } while (delimited); - - mvaddstr(1 + i, x, output_cursor); - if (oattr) - attroff(oattr); - if (attr) - attron(attr); - x += strlen(output_cursor); - special_end++; - output_cursor = special_end; - } - else - mvaddstr(1 + i, x, output_cursor); + mvaddstr(1 + i, x, output_cursor); + break; } - else + if (!(second_cursor = strstr(edit_cursor, ")"))) + { mvaddstr(1 + i, x, output_cursor); -// elif ((edit_cursor = strstr(output_cursor, "$ATT2("))) -// { -// if ((special_end = strstr(edit_cursor, ")"))) -// { -// } -// } + break; + } + int delimited = 0; + char *next_attr_char; + line_incomplete = 1; + oattr = attr; + attr = A_NORMAL; + *edit_cursor = *second_cursor = '\0'; + edit_cursor += 6; + next_attr_char = edit_cursor; + do // while(delimited) + { + delimited = 0; + next_delimiter = strchr(edit_cursor, ';'); + if (next_delimiter && *next_delimiter) + { + delimited = 1; + next_attr_char = next_delimiter; + *next_attr_char = '\0'; + next_attr_char++; + } + attr |= remap_attr_string(edit_cursor); + edit_cursor = next_attr_char; + } while (delimited); + + mvaddstr(1 + i, x, output_cursor); + if (oattr) + attroff(oattr); + if (attr) + attron(attr); + x += strlen(output_cursor); + second_cursor++; + output_cursor = second_cursor; } while (line_incomplete); free(banner_line); } @@ -1925,7 +1941,9 @@ initcurses () clear(); refresh(); } +/* ************************************************************* */ +#ifdef USE_NCURSES_COLOR int color_pair_idx(int fg, int bg) { int bgbits = (((1 & bg) << 2) | (2 & bg) | ((4 & bg) >> 2)) << 4; @@ -1933,7 +1951,7 @@ int color_pair_idx(int fg, int bg) int fgbits = (8 & fg) | ((1 & fg) << 2) | (2 & fg) | ((4 & fg) >> 2); return (bgbits | fgbits) + 1; } - +#endif /* ************************************************************* */ void diff --git a/dgamelaunch.h b/dgamelaunch.h index c199676..92ffeac 100644 --- a/dgamelaunch.h +++ b/dgamelaunch.h @@ -351,7 +351,9 @@ extern void domailuser(char *username); extern void drawmenu(void); extern void freefile(void); extern void initcurses(void); +#ifdef USE_NCURSES_COLOR extern int color_pair_idx(int fg, int bg); +#endif extern void loginprompt(int from_ttyplay); extern void newuser(void); extern void autologin(char *user, char *pass); diff --git a/examples/dgamelaunch.conf b/examples/dgamelaunch.conf index 8c43728..fede966 100644 --- a/examples/dgamelaunch.conf +++ b/examples/dgamelaunch.conf @@ -221,12 +221,17 @@ menu["mainmenu_user"] { # String substitutions defined in bannervars-section above. # $VERSION = dgamelaunch version # $USERNAME = user name (or [Anonymous] if not logged in) -# $ATTR(params) = change text color and attributes. -# params can be either number (to set the text color), -# one, or any of "b" (bold), "s" (standout), "u" (underline), -# "r" (reverse) or "d" (dim), -# or both color number and attribute characters, separated by semicolon. -# Empty param resets color and attributes to default. +# $ATTR(params) = change text color and attributes. | 0 = Black +# params can be either number (to set the text color), | 1 = Blue +# one, or any of "b" (bold), "s" (standout), "u" (underline), | 2 = Green +# "r" (reverse) or "d" (dim), | 3 = Cyan +# or both color number and attribute characters, separated by semicolon.| 4 = Red +# Empty param resets color and attributes to default. | 5 = Purple +# Color number can be from -1 to 128. Two color numbers can be provided,| 6 = Yellow +# separated by a comma (eg: $ATTR(14,1;b;f)) -- the second number must | 7 = White +# be from 0-7, and will be used as the background colour. | 8-15 = bright-bit set +# If no background color is given, and the provided foreground is >15, | 16-127 = setting background bits +# the higher 3 bits will be used as a background color. bannerfile = "(:menudir:)/dgl_menu_main_user.txt" # after which cursor is moved to this location # if cursor-definition is missing, the cursor is put -- 2.47.3