alistair 4 years ago
parent
commit
b2a4001145
  1. 11
      Makefile
  2. 26
      colcat.c
  3. 40
      readme.md

11
Makefile

@ -1,4 +1,12 @@ @@ -1,4 +1,12 @@
default: colcat.c
.PHONY=default
default: build
install: build
sudo cp bin/colcat /usr/local/bin/colcat
build: colcat.c
gcc -c colcat.c -o bin/colcat.o
gcc -c c-colours/colours.c -o bin/colours.o
gcc -lm bin/colcat.o bin/colours.o -o bin/colcat
@ -8,5 +16,6 @@ debug: colcat.c @@ -8,5 +16,6 @@ debug: colcat.c
gcc -g -c c-colours/colours.c -o bin/colours.o
gcc -g -lm bin/colcat.o bin/colours.o -o bin/colcat
clean:
rm bin/*

26
colcat.c

@ -8,22 +8,22 @@ @@ -8,22 +8,22 @@
* Basic Settings
*/
int rainbow_len = 90; // how many characters long is one full rainbow (360deg)
int col_change; // how many degrees between colours in the rainbow
int col_dist; // how many characters per colour in output
int frequency = 0; // how many degrees to rotate start point on new line
int col_change = -1; // how many degrees between colours in the rainbow
int col_dist = -1; // how many characters per colour in output
int frequency = -1; // how many degrees to rotate start point on new line
struct colour* colours; // the rainbow
int num_colours; // the length of the rainbow
/* Print the nth colour of the <rainbow_len> rainbow */
/* Print the nth colour of the <rainbow_len> chars long rainbow */
void print_next_colour(int current) {
if (current % col_dist == 0) {
struct colour c = colours[(current % num_colours) / col_dist];
c = get_rgb(c);
printf("\x1b[38;2;%d;%d;%dm", c.r, c.g, c.b);
}
}
/* Checks if a car is a possible ANSI escape code ending */
bool end_ansi_code(char c) {
char codes[] = "HfABCDsuJKmhlP";
for (int i = 0; i < strlen(codes); i++) {
@ -34,6 +34,7 @@ bool end_ansi_code(char c) { @@ -34,6 +34,7 @@ bool end_ansi_code(char c) {
return false;
}
/* Print a file with rainbow */
int display(FILE *file) {
char c = fgetc(file);
@ -65,6 +66,7 @@ int display(FILE *file) { @@ -65,6 +66,7 @@ int display(FILE *file) {
return 0;
}
/* Attempt to open a file and display, or return 1 on error */
int open_and_cat(char *file_name) {
FILE *file = fopen(file_name, "r");
@ -75,6 +77,13 @@ int open_and_cat(char *file_name) { @@ -75,6 +77,13 @@ int open_and_cat(char *file_name) {
return display(file);
}
/* - Generate an array containing all the colours to use for the
* rainbow by rotating HSV(0, 100, 100) through hue 0 to 360, using
* intervals fitting to how many characters are in the interval.
* - They are stored as RGB.
* - sets all the relevant global variables for printing the
* rainbow out again
*/
int generate_rainbow() {
if (rainbow_len < 360) {
num_colours = rainbow_len;
@ -96,12 +105,13 @@ int generate_rainbow() { @@ -96,12 +105,13 @@ int generate_rainbow() {
c.sp = CS_HSV;
for (int i = 0; i < num_colours; i++) {
c.h = (int)(c.h + col_change) % 360;
colours[i] = c;
colours[i] = get_rgb(c);
}
return 0;
}
/* Parses args, sets globals, and chooses whether to read files or stdin */
int main(int argc, char** argv) {
int opt;
int optcount = 0;
@ -120,6 +130,10 @@ int main(int argc, char** argv) { @@ -120,6 +130,10 @@ int main(int argc, char** argv) {
generate_rainbow();
if (frequency == -1) {
frequency = col_change;
}
if (argc > optcount + 1) {
for (int i = optcount + 1; i < argc; i++) {
int err = open_and_cat(argv[i]);

40
readme.md

@ -1,26 +1,44 @@ @@ -1,26 +1,44 @@
# colcat
Another lolcat clone.
Another lolcat clone, in 2020.
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 more work on it.
![image showing square of repeating 'a's in rainbow colour](demo.png)
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
more work on it. I wrote this to test my colours library that I'm writing for a
simple flat-shaded 2D game I've been working on for a while in bits and pieces.
It breaks neofetch and I'm not sure why, I haven't looked deeply into ANSI escape codes.
It seems to mostly be compatible with other ANSI codes now.
Options:
colcat -l [rainbow width]
colcat -h [rainbow width] -v [how much the hue is rotated each line]
Set how many characters it takes to change from red back to red.
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,
and it is deterministic.
It is the norm for these programs to fuzz the rainbow a bit, and provide more options for the frequency and speed (actually come to think of it a sinusoidal would have been a simpler implementation than what I do), and animation options.
I kind of like the look of the uniform rainbow though.
Compared to other versions this one is slightly more vibrant and uses truecolour.
This means the rainbow is slightly more accurate, but if your terminal doesn't
support truecolour then it will output garbage (I don't check any terminfo stuff,
or check that the output is a tty).
![image showing square of repeating 'a's in rainbow colour](demo.png)
It is also about 9K larger than jaseg's version, of course it is still more
performant than the ruby/node/python versions, it just might have bugs. :)
## Installation
git clone https://git.topost.net/alistair/colcat.git
cd colcat
make install
Installs `colcat` to `/usr/local/bin/`
## Similar Programs
- [/jaseg/lolcat](https://github.com/jaseg/lolcat) (C)
- [/busyloop/lolcat](https://github.com/busyloop/lolcat/) (Ruby)
- [tehmaze/lolcat/](https://github.com/tehmaze/lolcat/) (Python)
- [jaseg/lolcat](https://github.com/jaseg/lolcat) ( C )
- [busyloop/lolcat](https://github.com/busyloop/lolcat/) (Ruby)
- [tehmaze/lolcat](https://github.com/tehmaze/lolcat/) (Python)
- [gkbrk](https://gist.github.com/gkbrk/34ed7df44ba1400f31eb73a3f238faa7) (x64 Assembly)

Loading…
Cancel
Save