-
Notifications
You must be signed in to change notification settings - Fork 7
/
rainbow.c
55 lines (47 loc) · 1.18 KB
/
rainbow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
const char *usage_str = "Usage: %s [filename]\n";
const char *ansi_color_code_fmt = "\x1B[%um";
const char *ansi_color_reset_str = "\x1B[m";
typedef enum { RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA } ansi_color;
typedef unsigned int ansi_color_code;
ansi_color_code nextANSIColorCode(ansi_color *color)
{
ansi_color_code colorCodes[] = { 31, 33, 32, 36, 34, 35 };
ansi_color_code codeToReturn = colorCodes[*color];
*color = (*color + 1) % 6;
return codeToReturn;
}
int main(int argc, char *argv[])
{
FILE *fp;
/* Process the program's arguments */
if (argc == 2) {
fp = fopen(argv[1], "r");
if (!fp) {
perror(argv[0]);
return 1;
}
} else if (argc == 1) {
fp = stdin;
} else {
printf(usage_str, argv[0]);
return 2;
}
/* Loop through the file */
for (;;) { /* Loop ends when fgetc(fp) returns EOF */
static ansi_color color = RED;
int ch = fgetc(fp);
if (ch == EOF) break;
printf(ansi_color_code_fmt, nextANSIColorCode(&color));
putchar(ch);
if (ch == '\n') {
printf("%s", ansi_color_reset_str);
color = RED;
}
}
printf("%s", ansi_color_reset_str);
if (fp != stdin) fclose(fp);
return 0;
}