#include #include #include static inline int convert(int color, int *bold) { if (color > 7) { color -= 8; *bold = 1; } switch (color) { case 0: return 0; case 1: return 4; case 2: return 2; case 3: return 6; case 4: return 1; case 5: return 5; case 6: return 3; case 7: return 7; } return 0; } int main(int argc, char *argv[]) { char buf[2*200*100], oldbuf[2*200*100]; int fd, res, started = 0; int pos, color = 7, bold = 0, oldcolor = 7, oldbg = 0; if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); return 1; } for (;;) { if ((fd = open(argv[1], O_RDONLY)) < 0) { perror("open()"); return 1; } memset(buf, 0, sizeof(buf)); if ((res = read(fd, buf, sizeof(buf))) < 0) { perror("read()"); return 1; } if (!memcmp(buf, oldbuf, res)) { usleep(250000L); close(fd); continue; } memcpy(oldbuf, buf, res); printf("\e[1;1H"); for (pos = 4; pos < res; pos += 2) { started = 0; color = buf[pos + 1] % 16; bold = 0; if (color != oldcolor) { oldcolor = color; color = convert(color, &bold); printf("\e[%d;%d", bold, color + 30); started = 1; if (bold == 0) oldbg = 0; bold = 0; } color = buf[pos + 1] / 16; if (color != oldbg) { oldbg = color; color = convert(color, &bold); if (bold) { printf("%s5", started ? ";" : "\e["); started = 1; } printf("%s%d", started ? ";" : "\e[", color + 40); started = 1; } if (started) putchar('m'); putchar(buf[pos]); } fflush(stdout); close(fd); usleep(1000000L); } }