summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2022-11-19 12:41:00 +0100
committerjaseg <git@jaseg.de>2022-11-19 12:41:00 +0100
commit2a23642da7d6b98dd7c4c3b92c5e3fef3c70fb9d (patch)
treeefd1eb51938bd2efbb1b4882518c07a94be38c8d
parent8173ed8cb62f22ca2ebb33a63b5b9954afb20d7e (diff)
downloadlolcat-2a23642da7d6b98dd7c4c3b92c5e3fef3c70fb9d.tar.gz
lolcat-2a23642da7d6b98dd7c4c3b92c5e3fef3c70fb9d.tar.bz2
lolcat-2a23642da7d6b98dd7c4c3b92c5e3fef3c70fb9d.zip
Add 16-color mode for limited terminals
closes #43
-rwxr-xr-xlolcat.c17
1 files changed, 16 insertions, 1 deletions
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 <d>, -o <d>: 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)