change usleep to nanosleep, new config option. slight refactoring

Signed-off-by: 0xf8 <0xf8.dev@proton.me>
This commit is contained in:
0xf8 2023-02-25 12:27:56 -05:00
parent 526a64e982
commit 538c69cbf8
Signed by: 0xf8
GPG Key ID: 446580D758689584

View File

@ -1,20 +1,25 @@
/* /*
Rainbow Text (rbtext)
$ gcc -o rbtext rbtext.c
Created by Seelenlos <samoyo@protonmail.com>
NO WARRANTY OF ANY KIND. Copyright 2022 g.uwu@tutanota.com
NO LIABILITY TO CREATOR FOR ANY TYPE OF DAMAGE.
I DON'T CARE HOW YOU USE OR CONFIGURE THIS PROGRAM. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <memory.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <memory.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
// How many milliseconds the program will wait until the next print ( DEFAULT = 100 ) // How many milliseconds the program will wait until the next print ( DEFAULT = 100 )
#define CONF_SLEEP_TIMEMS 100 #define CONF_SLEEP_TIMEMS (long)100
// How many seconds (in addition to millisecond above) the program will wait ( DEFAULT = 0 )
#define CONF_SLEEP_TIMESEC (time_t)0
// How many times a color should be shown before switching to the next ( DEFAULT = 2 ) // How many times a color should be shown before switching to the next ( DEFAULT = 2 )
#define CONF_COLOR_LAG 2 #define CONF_COLOR_LAG 2
@ -33,46 +38,44 @@ const char *COLORS[] = {
"193;71;153", // PINK "193;71;153", // PINK
}; };
unsigned long COLORS_SIZE = sizeof(COLORS) / sizeof(char *);
unsigned long long t = 0; unsigned long long t = 0;
void genColors(char *s) { void genColors(char *s) {
char *cstr = (char *)malloc(20 * sizeof(char) + 1); char *cstr = (char *)malloc(64 + 1);
for (unsigned long i = 0; i < strlen(s); i++) { for (unsigned long i = 0; i < strlen(s); i++) {
unsigned long c = (((t + i) / CONF_COLOR_LAG) % (sizeof(COLORS) / sizeof(char *))); unsigned long c = (((t + i) / CONF_COLOR_LAG) % COLORS_SIZE);
snprintf(cstr, 20 * sizeof(char), "\e[38;2;%sm", COLORS[c]); snprintf(cstr, 64, "\e[38;2;%sm", COLORS[c]);
printf("%s%c\e[0m",cstr,s[i]); printf("%s%c\e[0m",cstr,s[i]);
} }
free(cstr); free(cstr);
} }
int main(int carg, char **varg) { int main(int argc, char **argv) {
if (carg < 2) { if (argc < 2) {
// [ERROR] No arguments given. USAGE: ... <text> // [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]); printf("\e[1;31m[ERROR]\e[0m No arguments given.\n\tUSAGE: \e[34m%s \e[33m<text>\e[0m\n",argv[0]);
return 1; return 1;
} }
char *text = (char *)malloc(1024 * sizeof(char) + 1);
// Similar to pythons str.join(' ') method char *text = (char *)malloc(1024 + 1);
// Adds all the arguments together with spaces seperating them // Adds all the arguments together with spaces seperating them
// EG: ./rbtext hello world > "hello world" for (int i = 1, j = 0; i < argc; i++) {
for (int i = 1, j = 0; i < carg; i++) { for (unsigned int k = 0; k < strlen(argv[i]); k++, j++)
for (int k = 0; k < strlen(varg[i]); k++, j++) { text[j] = argv[i][k];
text[j] = varg[i][k];
}
text[j] = ' '; text[j] = ' ';
j++; j++;
} }
struct timespec rem, req = {CONF_SLEEP_TIMESEC, CONF_SLEEP_TIMEMS*1000*1000};
while (1) { while (1) {
printf("\r\e[2K"); printf("\r\e[2K");
genColors(text); genColors(text);
fflush(stdout); fflush(stdout);
usleep(CONF_SLEEP_TIMEMS*1000); nanosleep(&req, &rem);
t += 1; t += 1;
} }