|
|
@ -10,11 +10,29 @@ |
|
|
|
int rainbow_len = 90; // how many characters long is one full rainbow (360deg)
|
|
|
|
int rainbow_len = 90; // how many characters long is one full rainbow (360deg)
|
|
|
|
int col_change = -1; // how many degrees between colours in the rainbow
|
|
|
|
int col_change = -1; // how many degrees between colours in the rainbow
|
|
|
|
int col_dist = -1; // how many characters per colour in output
|
|
|
|
int col_dist = -1; // how many characters per colour in output
|
|
|
|
int frequency = -1; // how many degrees to rotate start point on new line
|
|
|
|
int frequency = -1; // how many degrees to rotate start_hue point on new line
|
|
|
|
|
|
|
|
|
|
|
|
struct colour* colours; // the rainbow
|
|
|
|
struct colour* colours; // the rainbow
|
|
|
|
int num_colours; // the length of the rainbow
|
|
|
|
int num_colours; // the length of the rainbow
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// start_hue and end_hue hue values
|
|
|
|
|
|
|
|
int start_hue = -1; |
|
|
|
|
|
|
|
int end_hue = -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char help_text[] = "\nUsage: colcat [args] [--] [FILES] \n" |
|
|
|
|
|
|
|
"\n" |
|
|
|
|
|
|
|
"Concatenate files (or standard input) and print on the standard\n" |
|
|
|
|
|
|
|
"output, with a rainbow colour. The colour range and repeat\n" |
|
|
|
|
|
|
|
"distance can be specified.\n\n" |
|
|
|
|
|
|
|
"Args:\n\n" |
|
|
|
|
|
|
|
" -h Print help text and exit\n" |
|
|
|
|
|
|
|
" -w <int> Width, in chars, of the rainbow sequence\n" |
|
|
|
|
|
|
|
" -r <int> The amount the hue is rotated on new lines\n" |
|
|
|
|
|
|
|
" -s <int> The hue to start the rainbow at\n" |
|
|
|
|
|
|
|
" -e <int> The hue to end the rainbow at\n" |
|
|
|
|
|
|
|
"\n"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Print the nth colour of the <rainbow_len> chars long rainbow */ |
|
|
|
/* Print the nth colour of the <rainbow_len> chars long rainbow */ |
|
|
|
void print_next_colour(int current) { |
|
|
|
void print_next_colour(int current) { |
|
|
|
if (current % col_dist == 0) { |
|
|
|
if (current % col_dist == 0) { |
|
|
@ -23,8 +41,8 @@ void print_next_colour(int current) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Checks if a car is a possible ANSI escape code ending */ |
|
|
|
/* Checks if a car is a possible ANSI escape code end_hueing */ |
|
|
|
bool end_ansi_code(char c) { |
|
|
|
bool end_hue_ansi_code(char c) { |
|
|
|
char codes[] = "HfABCDsuJKmhlP"; |
|
|
|
char codes[] = "HfABCDsuJKmhlP"; |
|
|
|
for (int i = 0; i < strlen(codes); i++) { |
|
|
|
for (int i = 0; i < strlen(codes); i++) { |
|
|
|
if (c == codes[i]) { |
|
|
|
if (c == codes[i]) { |
|
|
@ -38,16 +56,16 @@ bool end_ansi_code(char c) { |
|
|
|
int display(FILE *file) { |
|
|
|
int display(FILE *file) { |
|
|
|
char c = fgetc(file); |
|
|
|
char c = fgetc(file); |
|
|
|
|
|
|
|
|
|
|
|
int start = 0; |
|
|
|
int start_hue = 0; |
|
|
|
int counter = 0; |
|
|
|
int counter = 0; |
|
|
|
bool in_escape = false; |
|
|
|
bool in_escape = false; |
|
|
|
while (c != EOF) { |
|
|
|
while (c != EOF) { |
|
|
|
if (c == '\n') { |
|
|
|
if (c == '\n') { |
|
|
|
counter = (start += frequency); |
|
|
|
counter = (start_hue += frequency); |
|
|
|
} |
|
|
|
} |
|
|
|
if (c == '\033') { |
|
|
|
if (c == '\033') { |
|
|
|
in_escape = true; |
|
|
|
in_escape = true; |
|
|
|
} else if (end_ansi_code(c) && in_escape) { |
|
|
|
} else if (end_hue_ansi_code(c) && in_escape) { |
|
|
|
in_escape = false; |
|
|
|
in_escape = false; |
|
|
|
putc(c, stdout); |
|
|
|
putc(c, stdout); |
|
|
|
c = fgetc(file); |
|
|
|
c = fgetc(file); |
|
|
@ -85,26 +103,40 @@ int open_and_cat(char *file_name) { |
|
|
|
* rainbow out again |
|
|
|
* rainbow out again |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
int generate_rainbow() { |
|
|
|
int generate_rainbow() { |
|
|
|
if (rainbow_len < 360) { |
|
|
|
|
|
|
|
|
|
|
|
if (start_hue == -1) { |
|
|
|
|
|
|
|
start_hue = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (end_hue == -1) { |
|
|
|
|
|
|
|
end_hue = 360; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (end_hue < start_hue) { |
|
|
|
|
|
|
|
exit(1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int arclen = end_hue - start_hue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rainbow_len < arclen) { |
|
|
|
num_colours = rainbow_len; |
|
|
|
num_colours = rainbow_len; |
|
|
|
col_change = 360 / num_colours; |
|
|
|
col_change = arclen / num_colours; |
|
|
|
if (col_change == 0) { |
|
|
|
if (col_change == 0) { |
|
|
|
col_change = 1; |
|
|
|
col_change = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
num_colours = 360; |
|
|
|
num_colours = arclen; |
|
|
|
col_change = 1; |
|
|
|
col_change = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
col_dist = (rainbow_len) / (num_colours); |
|
|
|
col_dist = (rainbow_len) / (num_colours); |
|
|
|
colours = calloc(num_colours, sizeof(struct colour)); |
|
|
|
colours = calloc(num_colours, sizeof(struct colour)); |
|
|
|
struct colour c; |
|
|
|
struct colour c; |
|
|
|
c.h = 0; |
|
|
|
c.h = start_hue; |
|
|
|
c.s = 1; |
|
|
|
c.s = 1; |
|
|
|
c.v = 1; |
|
|
|
c.v = 1; |
|
|
|
c.sp = CS_HSV; |
|
|
|
c.sp = CS_HSV; |
|
|
|
for (int i = 0; i < num_colours; i++) { |
|
|
|
for (int i = 0; i < num_colours; i++) { |
|
|
|
c.h = (int)(c.h + col_change) % 360; |
|
|
|
c.h = (int)(c.h + col_change) % arclen; |
|
|
|
colours[i] = get_rgb(c); |
|
|
|
colours[i] = get_rgb(c); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -115,16 +147,26 @@ int generate_rainbow() { |
|
|
|
int main(int argc, char** argv) { |
|
|
|
int main(int argc, char** argv) { |
|
|
|
int opt; |
|
|
|
int opt; |
|
|
|
int optcount = 0; |
|
|
|
int optcount = 0; |
|
|
|
while ((opt = getopt(argc, argv, "h:v:")) != -1) { |
|
|
|
while ((opt = getopt(argc, argv, "hw:r:s:e:")) != -1) { |
|
|
|
optcount++; |
|
|
|
optcount++; |
|
|
|
if (opt == 'h') { |
|
|
|
if (opt == 'w') { |
|
|
|
optcount++; |
|
|
|
optcount++; |
|
|
|
rainbow_len = atoi(optarg); |
|
|
|
rainbow_len = atoi(optarg); |
|
|
|
} else if (opt == 'v') { |
|
|
|
} else if (opt == 'r') { |
|
|
|
optcount++; |
|
|
|
optcount++; |
|
|
|
frequency = atoi(optarg); |
|
|
|
frequency = atoi(optarg); |
|
|
|
|
|
|
|
} else if (opt == 's') { |
|
|
|
|
|
|
|
optcount++; |
|
|
|
|
|
|
|
start_hue = atoi(optarg); |
|
|
|
|
|
|
|
} else if (opt == 'e') { |
|
|
|
|
|
|
|
optcount++; |
|
|
|
|
|
|
|
end_hue = atoi(optarg); |
|
|
|
|
|
|
|
} else if (opt == 'h') { |
|
|
|
|
|
|
|
printf("%s", help_text); |
|
|
|
|
|
|
|
exit(0); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
printf("Usage: colcat -h [rainbow length] -v [vertical frequency]"); |
|
|
|
printf("%s", help_text); |
|
|
|
|
|
|
|
exit(1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|