diff --git a/src/calendar.c b/src/calendar.c index f2be8806..3f7f5135 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -281,12 +281,17 @@ static void goff(void) printf("%s", linestruct->graphics_off); } -void Decolorize(void) +char const * +Decolorize(int r, int g, int b) { - printf("%s", "\x1B[0m"); + if (!strcmp(Colorize(r, g, b), "")) { + return ""; + } + return "\x1B[0m"; } -void Colorize(int r, int g, int b) +char const * +Colorize(int r, int g, int b) { int bright = 0; if (r > 128 || g > 128 || b > 128) { @@ -299,12 +304,20 @@ void Colorize(int r, int g, int b) if (b > 64) b = 1; else b = 0; - printf("%s", VT100Colors[bright][r][g][b]); + /* Don't do black on a dark terminal or white + on a light terminal */ + if (TerminalBackground == TERMINAL_BACKGROUND_DARK) { + if (!r && !g && !b) return ""; + } + if (TerminalBackground == TERMINAL_BACKGROUND_LIGHT) { + if (r && g && b) return ""; + } + return VT100Colors[bright][r][g][b]; } static void ColorizeEntry(CalEntry const *e) { - Colorize(e->r, e->g, e->b); + printf("%s", Colorize(e->r, e->g, e->b)); } static int @@ -829,7 +842,7 @@ static int WriteOneColLine(int col) /* Decolorize reminder if necessary */ if (UseVTColors && e->is_color) { - Decolorize(); + printf("%s", Decolorize(e->r, e->g, e->b)); } /* Flesh out the rest of the column */ @@ -901,7 +914,7 @@ static int WriteOneColLine(int col) /* Decolorize reminder if necessary */ if (UseVTColors && e->is_color) { - Decolorize(); + printf("%s", Decolorize(e->r, e->g, e->b)); } /* Flesh out the rest of the column */ diff --git a/src/dorem.c b/src/dorem.c index 8cac6b5e..48e3b857 100644 --- a/src/dorem.c +++ b/src/dorem.c @@ -763,6 +763,9 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) char const *s; Value v; + int red = -1, green = -1, blue = -1; + int is_color = 0; + DBufInit(&buf); DBufInit(&calRow); DBufInit(&pre_buf); @@ -777,6 +780,7 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) if (t->typ == PASSTHRU_TYPE && (!strcmp(t->passthru, "COLOR") || !strcmp(t->passthru, "COLOUR"))) { /* Strip off three tokens */ r = ParseToken(p, &buf); + sscanf(DBufValue(&buf), "%d", &red); if (!NextMode) { DBufPuts(&pre_buf, DBufValue(&buf)); DBufPutc(&pre_buf, ' '); @@ -784,6 +788,7 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) DBufFree(&buf); if (r) return r; r = ParseToken(p, &buf); + sscanf(DBufValue(&buf), "%d", &green); if (!NextMode) { DBufPuts(&pre_buf, DBufValue(&buf)); DBufPutc(&pre_buf, ' '); @@ -791,6 +796,7 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) DBufFree(&buf); if (r) return r; r = ParseToken(p, &buf); + sscanf(DBufValue(&buf), "%d", &blue); if (!NextMode) { DBufPuts(&pre_buf, DBufValue(&buf)); DBufPutc(&pre_buf, ' '); @@ -874,6 +880,16 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) return OK; } + /* Correct colors */ + if (UseVTColors) { + if (red >= 0 && green >= 0 && blue >= 0) { + is_color = 1; + if (red > 255) red = 255; + if (green > 255) green = 255; + if (blue > 255) blue = 255; + } + } + /* Put the substituted string into the substitution buffer */ /* Don't use msgprefix() on RUN-type reminders */ @@ -884,6 +900,9 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) r = EvalExpr(&s, &v, NULL); if (!r) { if (!DoCoerce(STR_TYPE, &v)) { + if (is_color) { + DBufPuts(&buf, Colorize(red, green, blue)); + } if (DBufPuts(&buf, v.v.str) != OK) { DBufFree(&buf); DestroyValue(v); @@ -895,6 +914,9 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) } } + if (is_color) { + DBufPuts(&buf, Colorize(red, green, blue)); + } if ( (r=DoSubst(p, &buf, t, tim, jul, NORMAL_MODE)) ) return r; if (t->typ != RUN_TYPE) { if (UserFuncExists("msgsuffix") == 1) { @@ -903,6 +925,9 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) r = EvalExpr(&s, &v, NULL); if (!r) { if (!DoCoerce(STR_TYPE, &v)) { + if (is_color) { + DBufPuts(&buf, Colorize(red, green, blue)); + } if (DBufPuts(&buf, v.v.str) != OK) { DBufFree(&buf); DestroyValue(v); @@ -914,6 +939,10 @@ int TriggerReminder(ParsePtr p, Trigger *t, TimeTrig *tim, int jul) } } + if (is_color) { + DBufPuts(&buf, Decolorize(red, green, blue)); + } + if ((!MsgCommand && t->typ == MSG_TYPE) || t->typ == MSF_TYPE) { if (DBufPutc(&buf, '\n') != OK) { DBufFree(&buf); diff --git a/src/globals.h b/src/globals.h index 8c803b80..e268b004 100644 --- a/src/globals.h +++ b/src/globals.h @@ -103,6 +103,7 @@ EXTERN INIT( int CalPad, 1); EXTERN INIT( int UseVTChars, 0); EXTERN INIT( int UseUTF8Chars, 0); EXTERN INIT( int UseVTColors, 0); +EXTERN INIT( int TerminalBackground, TERMINAL_BACKGROUND_UNKNOWN); /* Latitude and longitude */ EXTERN INIT( int LatDeg, LAT_DEG); diff --git a/src/init.c b/src/init.c index 8a6560c5..dbd42fd0 100644 --- a/src/init.c +++ b/src/init.c @@ -207,6 +207,13 @@ void InitRemind(int argc, char const *argv[]) case '@': UseVTColors = 1; + if (*arg == '0') { + TerminalBackground = TERMINAL_BACKGROUND_DARK; + arg++; + } else if (*arg == '1') { + TerminalBackground = TERMINAL_BACKGROUND_LIGHT; + arg++; + } break; case 'j': diff --git a/src/protos.h b/src/protos.h index 74619f1e..1c068874 100644 --- a/src/protos.h +++ b/src/protos.h @@ -149,5 +149,5 @@ void ClearLastTriggers(void); void SaveLastTrigger(Trigger const *t); void SaveLastTimeTrig(TimeTrig const *t); void PerIterationInit(void); -void Decolorize(void); -void Colorize(int r, int g, int b); +char const *Decolorize(int r, int g, int b); +char const *Colorize(int r, int g, int b); diff --git a/src/types.h b/src/types.h index e02830b5..1e92e357 100644 --- a/src/types.h +++ b/src/types.h @@ -227,3 +227,7 @@ typedef struct { /* Pure JSON */ #define PSCAL_LEVEL3 3 + +#define TERMINAL_BACKGROUND_UNKNOWN 0 +#define TERMINAL_BACKGROUND_DARK 1 +#define TERMINAL_BACKGROUND_LIGHT 2