cutecat.c/cutecat.c

112 lines
1.9 KiB
C

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <math.h>
#define FREQ 220
unsigned int d_flag;
unsigned int r_flag;
void OwO(char *buf, off_t len) {
if (d_flag)
return;
char eyes[] = {'U', 'O'};
for (int i = 0; i < len; i++) {
size_t i = rand() % len;
if (i + 3 < len && !isspace(buf[i]) && !isspace(buf[i + 1]) && !isspace(buf[i + 2])) {
char eye = eyes[rand() % sizeof(eyes)];
buf[i] = eye;
buf[i + 1] = 'w';
buf[i + 2] = eye;
}
}
}
void PrintRainbow(char c, off_t i) {
unsigned int red = sin(FREQ * i) * 127 + 128;
unsigned int blue = sin(FREQ * i + 2 * M_PI / 3) * 127 + 128;
unsigned int green = sin(FREQ * i + 4 * M_PI / 3) * 127 + 128;
printf("\033[38;2;%u;%u;%um%c\033[0m", red, green, blue, c);
}
int cat(const char *path) {
int fd = STDIN_FILENO;
if (strcmp(path, "-"))
fd = open(path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "cutecat: %s: %s\n", path, strerror(errno));
return 1;
}
char buf[1025];
off_t len = 0;
off_t n_line = 10;
while ((len = read(fd, buf, sizeof(buf))) > 0) {
OwO(buf, len);
for (off_t i = 0; i < len; i++) {
if (buf[i] == '\n')
n_line++;
if (!isspace(buf[i]) && !r_flag)
PrintRainbow(buf[i], n_line + i / 10);
else
putchar(buf[i]);
}
}
if (strcmp(path, "-"))
close(fd);
return 0;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "dr")) != -1) {
switch (opt) {
case 'd':
d_flag = 1;
break;
case 'r':
r_flag = 1;
break;
default:
printf("cutecat [file1 file2...]\n\t[-d Default mode]\n\t[-r Print without colorise text]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (!d_flag)
srand(getpid());
if (argc == 0)
return cat("-");
else {
int ret = 0;
for (int i = 0; i < argc; i++)
if (cat(argv[i]))
ret = 1;
return ret;
}
}