From 2a23642da7d6b98dd7c4c3b92c5e3fef3c70fb9d Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 19 Nov 2022 12:41:00 +0100 Subject: Add 16-color mode for limited terminals closes #43 --- lolcat.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lolcat.c b/lolcat.c index bcf026a..8257b34 100755 --- a/lolcat.c +++ b/lolcat.c @@ -44,6 +44,7 @@ static char helpstr[] = "\n" " --color_offset , -o : Start with a different color\n" " --24bit, -b: Output in 24-bit \"true\" RGB mode (slower and\n" " not supported by all terminals)\n" + " --16color, -x: Output in 16-color mode for basic terminals\n" " --version: Print version and exit\n" " --help: Show this message\n" "\n" @@ -58,6 +59,7 @@ static char helpstr[] = "\n" #define ARRAY_SIZE(foo) (sizeof(foo) / sizeof(foo[0])) const unsigned char codes[] = { 39, 38, 44, 43, 49, 48, 84, 83, 119, 118, 154, 148, 184, 178, 214, 208, 209, 203, 204, 198, 199, 163, 164, 128, 129, 93, 99, 63, 69, 33 }; +const unsigned char codes16[] = {31, 33, 32, 36, 34, 35, 95, 94, 96, 92, 93, 91}; static void find_escape_sequences(wint_t c, int* state) { @@ -74,7 +76,7 @@ static void find_escape_sequences(wint_t c, int* state) static void usage(void) { wprintf(L"Usage: lolcat [-h horizontal_speed] [-v vertical_speed] [--] [FILES...]\n"); - exit(1); + exit(2); } static void version(void) @@ -104,6 +106,7 @@ int main(int argc, char** argv) int random = 0; int start_color = 0; int rgb = 0; + int ansi16 = 0; double freq_h = 0.23, freq_v = 0.1; struct timeval tv; @@ -144,6 +147,8 @@ int main(int argc, char** argv) } } else if (!strcmp(argv[i], "-b") || !strcmp(argv[i], "--24bit")) { rgb = 1; + } else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--16color")) { + ansi16 = 1; } else if (!strcmp(argv[i], "--version")) { version(); } else { @@ -153,6 +158,11 @@ int main(int argc, char** argv) } } + if (rgb && ansi16) { + wprintf(L"Only one of --24bit and --16color can be given at a time\n"); + usage(); + } + int rand_offset = 0; if (random) { srand(time(NULL)); @@ -217,6 +227,11 @@ int main(int argc, char** argv) uint8_t blue = lrintf((offset + (1.0f - offset) * (0.5f + 0.5f * sin(theta + 4 * M_PI / 3 ))) * 255.0f); wprintf(L"\033[38;2;%d;%d;%dm", red, green, blue); + } else if (ansi16) { + int ncc = offx * ARRAY_SIZE(codes16) + (int)((i += wcwidth(c)) * freq_h + l * freq_v); + if (cc != ncc) + wprintf(L"\033[%hhum", codes16[(rand_offset + start_color + (cc = ncc)) % ARRAY_SIZE(codes16)]); + } else { int ncc = offx * ARRAY_SIZE(codes) + (int)((i += wcwidth(c)) * freq_h + l * freq_v); if (cc != ncc) -- cgit