From 7218d55f081aae645a41faf24b7295eeaa626cf5 Mon Sep 17 00:00:00 2001 From: Dianne Skoll Date: Sun, 9 Oct 2022 12:40:48 -0400 Subject: [PATCH] Add $TerminalBackground special variable. --- src/expr.c | 7 ++++- src/funcs.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/var.c | 1 + tests/test.cmp | 1 + 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/expr.c b/src/expr.c index 21acfcb4..37df286b 100644 --- a/src/expr.c +++ b/src/expr.c @@ -1260,7 +1260,12 @@ void PrintValue (Value *v, FILE *fp) case '\v': fprintf(ErrFp, "\\v"); break; case '"': fprintf(ErrFp, "\\\""); break; case '\\': fprintf(ErrFp, "\\\\"); break; - default: putc(*s, ErrFp); break; + default: + if (*s < 32 || *s > 126) { + fprintf(ErrFp, "\\x%02x", (unsigned int) *s); + } else { + putc(*s, ErrFp); break; + } } s++; } diff --git a/src/funcs.c b/src/funcs.c index 3c92cf04..5bbcfca4 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -58,6 +58,7 @@ static int FADusk (func_info *); static int FAbs (func_info *); static int FAccess (func_info *); static int FAmpm (func_info *); +static int FAnsicolor (func_info *); static int FTrig (func_info *); static int FIsAny (func_info *); static int FArgs (func_info *); @@ -218,6 +219,7 @@ BuiltinFunc Func[] = { { "adawn", 0, 1, 0, FADawn}, { "adusk", 0, 1, 0, FADusk}, { "ampm", 1, 3, 1, FAmpm }, + { "ansicolor", 1, 5, 1, FAnsicolor }, { "args", 1, 1, 0, FArgs }, { "asc", 1, 1, 1, FAsc }, { "baseyr", 0, 0, 1, FBaseyr }, @@ -911,6 +913,87 @@ static int FSgn(func_info *info) return OK; } +static int parse_color_helper(char const *str, int *r, int *g, int *b) +{ + if (!*str) { + /* Empty string means "reset to normal" */ + *r = -1; + *g = -1; + *b = -1; + return OK; + } + if (sscanf(str, "%d %d %d", r, g, b) != 3) { + return E_BAD_TYPE; + } + return OK; +} + +/***************************************************************/ +/* */ +/* FAnsicolor - return an ANSI terminal color sequence */ +/* */ +/***************************************************************/ +static int FAnsicolor(func_info *info) +{ + int r=0, g=0, b=0, bg=0, clamp=1; + int status = 0; + int index = 0; + bg = 0; + clamp = 1; + + /* If first arg is a string: Parse out the colors */ + if (ARG(0).type == STR_TYPE) { + /* If first arg is a string: Parse out the colors */ + status = parse_color_helper(ARGSTR(0), &r, &g, &b); + if (status != 0) { + return status; + } + index = 1; + } else if (ARG(0).type == INT_TYPE) { + /* Must be at least three arguments */ + if (Nargs < 3) return E_2FEW_ARGS; + ASSERT_TYPE(1, INT_TYPE); + ASSERT_TYPE(2, INT_TYPE); + r = ARGV(0); + g = ARGV(1); + b = ARGV(2); + index = 3; + } + if (r < -1 || g < -1 || b < -1) return E_2LOW; + if (r > 255 || g > 255 || b > 255) return E_2HIGH; + /* If any is -1, then all must be -1 */ + if (r == -1 || g == -1 || b == -1) { + if (r != -1 || g != -1 || b != -1) { + return E_2LOW; + } + } + if (Nargs > index) { + ASSERT_TYPE(index, INT_TYPE); + if (ARGV(index) < 0) return E_2LOW; + if (ARGV(index) > 1) return E_2HIGH; + bg = ARGV(index); + index++; + if (Nargs > index) { + ASSERT_TYPE(index, INT_TYPE); + if (ARGV(index) < 0) return E_2LOW; + if (ARGV(index) > 1) return E_2HIGH; + clamp = ARGV(index); + } + } + + /* All righ! We have our parameters; now return the string */ + if (!UseVTColors) { + /* Not using any colors: Empty strin */ + return RetStrVal("", info); + } + + if (r < 0) { + /* Return ANSI "reset to normal" string */ + return RetStrVal(Decolorize(), info); + } + return RetStrVal(Colorize(r, g, b, bg, clamp), info); +} + /***************************************************************/ /* */ /* FAmpm - return a time as a string with "AM" or "PM" suffix */ diff --git a/src/var.c b/src/var.c index 517df0cf..3ec2a192 100644 --- a/src/var.c +++ b/src/var.c @@ -854,6 +854,7 @@ static SysVar SysVarArr[] = { {"SysInclude", 0, STR_TYPE, &SysDir, 0, 0, NULL }, {"T", 0, SPECIAL_TYPE, trig_date_func, 0, 0, NULL }, {"Td", 0, SPECIAL_TYPE, trig_day_func, 0, 0, NULL }, + {"TerminalBackground", 0, INT_TYPE, &TerminalBackground, 0, 0, NULL }, {"Thursday", 1, STR_TYPE, &DynamicDayName[3], 0, 0, NULL }, {"TimeSep", 1, SPECIAL_TYPE, time_sep_func, 0, 0, NULL }, {"Tm", 0, SPECIAL_TYPE, trig_mon_func, 0, 0, NULL }, diff --git a/tests/test.cmp b/tests/test.cmp index 3b612762..93c19aed 100644 --- a/tests/test.cmp +++ b/tests/test.cmp @@ -2774,6 +2774,7 @@ Variable Value $SuppressLRM 0 [0, 1] $T 0 $Td -1 +$TerminalBackground 0 $Thursday "Thursday" $TimeSep ":" $Tm -1