diff --git a/tlc.c b/tlc.c index f6f4023..a882738 100644 --- a/tlc.c +++ b/tlc.c @@ -1,31 +1,103 @@ #include -#include #include -#include +#include +#include -void rgb(int i, int *r, int *g, int *b) +enum esc_st { + ST_NONE = 0, + ST_ESC_BEGIN, + ST_ESC_STRING, + ST_ESC_CSI, + ST_ESC_STRING_TERM, + ST_ESC_CSI_TERM, + ST_ESC_TERM, + NUM_ST +}; + +static enum esc_st find_escape_sequences(int c, enum esc_st st) { - float f = 0.1; - *r = (int)(sin(f * i + 0) * 127 + 128); - *g = (int)(sin(f * i + 2 * M_PI / 3) * 127 + 128); - *b = (int)(sin(f * i + 4 * M_PI / 3) * 127 + 128); + if (st == ST_NONE || st == ST_ESC_CSI_TERM) { + if (c == '\033') { + return ST_ESC_BEGIN; + } else { + return ST_NONE; + } + + } else if (st == ST_ESC_BEGIN) { + if (c == '[') { + return ST_ESC_CSI; + } else if (c == 'P' || c == ']' || c == 'X' || c == '^' || c == '_') { + return ST_ESC_STRING; + } else { + return ST_ESC_TERM; + } + + } else if (st == ST_ESC_CSI) { + if (0x40 <= c && c <= 0x7e) { + return ST_ESC_CSI_TERM; + } else { + return st; + } + + } else if (st == ST_ESC_STRING) { + if (c == '\007') { + return ST_NONE; + } else if (c == '\033') { + return ST_ESC_STRING_TERM; + } else { + return st; + } + + } else if (st == ST_ESC_STRING_TERM) { + if (c == '\\') { + return ST_NONE; + } else { + return ST_ESC_STRING; + } + + } else { + return ST_NONE; + } } -int main() +int main(int argc, char** argv) { - if (isatty(fileno(stdin))) { - fprintf(stderr, "The command is intended to work with pipes.\n"); - exit(1); - } + int i = 0, l = 0; + int c; + srand(time(NULL)); + double offx = rand() % 300; - int j = 0; - int c; - while ((c = getchar()) != EOF) { - int r, g, b; - rgb(j, &r, &g, &b); - printf("\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, c); - j++; - } + while ((c = getchar()) != EOF) { + int escape_state = find_escape_sequences(c, escape_state); + if (escape_state == ST_ESC_CSI_TERM) { + putchar(c); + } - return 0; + if (escape_state == ST_NONE || escape_state == ST_ESC_CSI_TERM) { + if (c == '\n') { + l++; + i = 0; + } else { + if (escape_state == ST_NONE) { + i++; + } + + float theta = i * 0.23 / 5.0f + l * 0.1 + (offx / RAND_MAX) * M_PI; + + float offset = 0.1; + unsigned char r = lrintf((offset + (1.0f - offset) * (0.5f + 0.5f * sin(theta))) * 255.0f); + unsigned char g = lrintf((offset + (1.0f - offset) * (0.5f + 0.5f * sin(theta + 2 * M_PI / 3))) * 255.0f); + unsigned char b = lrintf((offset + (1.0f - offset) * (0.5f + 0.5f * sin(theta + 4 * M_PI / 3))) * 255.0f); + + printf("\033[38;2;%d;%d;%dm", r, g, b); + } + } + + if (escape_state != ST_ESC_CSI_TERM) { + putchar(c); + } + } + + printf("\033[0m"); + return 0; }