Handle escape sequence

This commit is contained in:
Night Kaly 2024-10-12 17:49:29 +01:00
parent c3fee55c56
commit ceee0b78f0
Signed by: night0721
GPG key ID: 957D67B8DB7A119B

114
tlc.c
View file

@ -1,31 +1,103 @@
#include <stdio.h> #include <stdio.h>
#include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <time.h>
#include <math.h>
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; if (st == ST_NONE || st == ST_ESC_CSI_TERM) {
*r = (int)(sin(f * i + 0) * 127 + 128); if (c == '\033') {
*g = (int)(sin(f * i + 2 * M_PI / 3) * 127 + 128); return ST_ESC_BEGIN;
*b = (int)(sin(f * i + 4 * M_PI / 3) * 127 + 128); } 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))) { int i = 0, l = 0;
fprintf(stderr, "The command is intended to work with pipes.\n"); int c;
exit(1); srand(time(NULL));
} double offx = rand() % 300;
int j = 0; while ((c = getchar()) != EOF) {
int c; int escape_state = find_escape_sequences(c, escape_state);
while ((c = getchar()) != EOF) { if (escape_state == ST_ESC_CSI_TERM) {
int r, g, b; putchar(c);
rgb(j, &r, &g, &b); }
printf("\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, c);
j++;
}
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;
} }