Browse Source

help text, args, and custom gradient

dev
alistair 4 years ago
parent
commit
4dbc9c04dd
  1. 72
      colcat.c
  2. BIN
      demo.png
  3. 21
      readme.md

72
colcat.c

@ -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);
} }
} }

BIN
demo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 125 KiB

After

Width:  |  Height:  |  Size: 46 KiB

21
readme.md

@ -1,8 +1,8 @@
# colcat # colcat (lolcat)
Another lolcat clone, in 2020. Another lolcat clone, in 2020.
![image showing square of repeating 'a's in rainbow colour](demo.png) ![image showing help text in rainbow colour](demo.png)
This is probably the least feature-complete version of lolcat, (the `cat` clone This is probably the least feature-complete version of lolcat, (the `cat` clone
that makes the output rainbow), but I wrote it anyway and probably won't do any that makes the output rainbow), but I wrote it anyway and probably won't do any
@ -13,7 +13,20 @@ It seems to mostly be compatible with other ANSI codes now.
Options: Options:
colcat -h [rainbow width] -v [how much the hue is rotated each line] Usage: colcat [args] [--] [FILES]
Concatenate files (or standard input) and print on the standard
output, with a rainbow colour. The colour range and repeat
distance can be specified.
Args:
-h Print help text and exit
-w <int> Width, in chars, of the rainbow sequence
-r <int> The amount the hue is rotated on new lines
-s <int> The hue to start the rainbow at
-e <int> The hue to end the rainbow at
It is the norm for these programs to fuzz the rainbow a bit, and provide more It is the norm for these programs to fuzz the rainbow a bit, and provide more
options for the frequency and animation options. Mine doesn't have animation, options for the frequency and animation options. Mine doesn't have animation,
@ -38,7 +51,7 @@ Installs `colcat` to `/usr/local/bin/`
## Similar Programs ## Similar Programs
- [busyloop/lolcat](https://github.com/busyloop/lolcat/) (Ruby) - the original
- [jaseg/lolcat](https://github.com/jaseg/lolcat) ( C ) - [jaseg/lolcat](https://github.com/jaseg/lolcat) ( C )
- [busyloop/lolcat](https://github.com/busyloop/lolcat/) (Ruby)
- [tehmaze/lolcat](https://github.com/tehmaze/lolcat/) (Python) - [tehmaze/lolcat](https://github.com/tehmaze/lolcat/) (Python)
- [gkbrk](https://gist.github.com/gkbrk/34ed7df44ba1400f31eb73a3f238faa7) (x64 Assembly) - [gkbrk](https://gist.github.com/gkbrk/34ed7df44ba1400f31eb73a3f238faa7) (x64 Assembly)

Loading…
Cancel
Save