Browse Source

improve esc code handling

dev
alistair 4 years ago
parent
commit
5cbee2ef21
  1. 16
      colcat.c

16
colcat.c

@ -24,6 +24,16 @@ void print_next_colour(int current) { @@ -24,6 +24,16 @@ void print_next_colour(int current) {
}
}
bool end_ansi_code(char c) {
char codes[] = "HfABCDsuJKmhlP";
for (int i = 0; i < strlen(codes); i++) {
if (c == codes[i]) {
return true;
}
}
return false;
}
int display(FILE *file) {
char c = fgetc(file);
@ -36,22 +46,22 @@ int display(FILE *file) { @@ -36,22 +46,22 @@ int display(FILE *file) {
}
if (c == '\033') {
in_escape = true;
} else if (c == 'm' && in_escape) {
} else if (end_ansi_code(c) && in_escape) {
in_escape = false;
putc(c, stdout);
c = fgetc(file);
counter++;
continue;
}
if (!in_escape) {
print_next_colour(counter);
}
putc(c, stdout);
counter++;
putc(c, stdout);
c = fgetc(file);
}
counter++;
return 0;
}

Loading…
Cancel
Save