rbtext/rbtext.c
0xf8 526a64e982
New color system; it's now a simple array
Signed-off-by: 0xf8 <0xf8.dev@proton.me>
2023-02-25 12:27:56 -05:00

81 lines
2.1 KiB
C

/*
Rainbow Text (rbtext)
$ gcc -o rbtext rbtext.c
Created by Seelenlos <samoyo@protonmail.com>
NO WARRANTY OF ANY KIND.
NO LIABILITY TO CREATOR FOR ANY TYPE OF DAMAGE.
I DON'T CARE HOW YOU USE OR CONFIGURE THIS PROGRAM.
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
// How many milliseconds the program will wait until the next print ( DEFAULT = 100 )
#define CONF_SLEEP_TIMEMS 100
// How many times a color should be shown before switching to the next ( DEFAULT = 2 )
#define CONF_COLOR_LAG 2
// RICERS:
// To add colors, simply add the color (according to the format below) to COLORS
// FORMAT: "R;G;B",
const char *COLORS[] = {
"193;71;87", // RED
"193;116;71", // ORANGE
"193;177;71", // YELLOW
"149;193;71", // GREEN
"71;193;161", // CYAN
"71;165;193", // BLUE
"155;71;193", // PURPLE
"193;71;153", // PINK
};
unsigned long long t = 0;
void genColors(char *s) {
char *cstr = (char *)malloc(20 * sizeof(char) + 1);
for (unsigned long i = 0; i < strlen(s); i++) {
unsigned long c = (((t + i) / CONF_COLOR_LAG) % (sizeof(COLORS) / sizeof(char *)));
snprintf(cstr, 20 * sizeof(char), "\e[38;2;%sm", COLORS[c]);
printf("%s%c\e[0m",cstr,s[i]);
}
free(cstr);
}
int main(int carg, char **varg) {
if (carg < 2) {
// [ERROR] No arguments given. USAGE: ... <text>
printf("\e[1;31m[ERROR]\e[0m No arguments given.\n\tUSAGE: \e[34m%s \e[33m<text>\e[0m\n",varg[0]);
return 1;
}
char *text = (char *)malloc(1024 * sizeof(char) + 1);
// Similar to pythons str.join(' ') method
// Adds all the arguments together with spaces seperating them
// EG: ./rbtext hello world > "hello world"
for (int i = 1, j = 0; i < carg; i++) {
for (int k = 0; k < strlen(varg[i]); k++, j++) {
text[j] = varg[i][k];
}
text[j] = ' ';
j++;
}
while (1) {
printf("\r\e[2K");
genColors(text);
fflush(stdout);
usleep(CONF_SLEEP_TIMEMS*1000);
t += 1;
}
return 0;
}