Add basic piped input support
This commit is contained in:
parent
78f1ad7362
commit
24a8565f6d
10
README.md
10
README.md
@ -14,8 +14,14 @@ OR
|
|||||||
|
|
||||||
## runtime configuration
|
## runtime configuration
|
||||||
This is done by setting environment variables.
|
This is done by setting environment variables.
|
||||||
- `RBTEXT_FULLSCREEN` (default: Enabled) - Clear screen every print (useful for text that will wrap)
|
- `RBTEXT_FULLSCREEN` (default: Enabled, force disable when non-tty) - Clear screen every print (useful for text that will wrap)
|
||||||
|
|
||||||
### example
|
### example
|
||||||
Runs rbtext without clearing the screen everytime
|
Runs rbtext without clearing the screen everytime <br/>
|
||||||
`RBTEXT_FULLSCREEN=-1 ./rbtext hello world`
|
`RBTEXT_FULLSCREEN=-1 ./rbtext hello world`
|
||||||
|
<hr/>
|
||||||
|
Runs rbtext with piped input <br/>
|
||||||
|
`echo "hello world" | ./rbtext`
|
||||||
|
|
||||||
|
## note on piped input
|
||||||
|
Piping input will cause rbtext to stop refreshing the screen. This is because it expects constant output, like in the following command: `cat /dev/urandom | base -w 0 | ./rbtext`
|
37
rbtext.c
37
rbtext.c
@ -16,7 +16,7 @@ this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <memory.h>
|
//#include <memory.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -31,7 +31,7 @@ this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
#define CONF_SLEEP_TIMESEC (time_t)0
|
#define CONF_SLEEP_TIMESEC (time_t)0
|
||||||
// How many times a color should be shown before switching to the next ( DEFAULT
|
// How many times a color should be shown before switching to the next ( DEFAULT
|
||||||
// = 2 )
|
// = 2 )
|
||||||
#define CONF_COLOR_LAG 2
|
#define CONF_COLOR_LAG 3
|
||||||
|
|
||||||
// Don't set to 0
|
// Don't set to 0
|
||||||
// Disabled = -1
|
// Disabled = -1
|
||||||
@ -53,16 +53,34 @@ const char *COLORS[] = {
|
|||||||
"193;71;153", // PINK
|
"193;71;153", // PINK
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Used for detecting pipe, don't change!
|
||||||
|
uint use_pipe = 0;
|
||||||
|
|
||||||
unsigned long COLORS_SIZE = sizeof(COLORS) / sizeof(char *);
|
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(64 + 1);
|
char *cstr = (char *)malloc(64 + 1);
|
||||||
|
|
||||||
|
if (use_pipe) {
|
||||||
|
char *buffer = (char *)malloc(1024 + 1);
|
||||||
|
long r = read(STDIN_FILENO, buffer, 1024);
|
||||||
|
|
||||||
|
if (r < 1) return;
|
||||||
|
|
||||||
|
for (unsigned long i = 0; i < r; i++) {
|
||||||
|
unsigned long c = ((t + 1) / CONF_COLOR_LAG) % COLORS_SIZE;
|
||||||
|
snprintf(cstr, 64, "\e[38;2;%sm", COLORS[c]);
|
||||||
|
printf("%s%c\e[0m", cstr, buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
|
} else {
|
||||||
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) % COLORS_SIZE;
|
unsigned long c = ((t + i) / CONF_COLOR_LAG) % COLORS_SIZE;
|
||||||
snprintf(cstr, 64, "\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);
|
||||||
}
|
}
|
||||||
@ -79,22 +97,26 @@ void set_flag(int *flag, const char *env) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
if (argc < 2) {
|
if (!isatty(STDIN_FILENO)) {
|
||||||
|
use_pipe = 1;
|
||||||
|
} else 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 "
|
printf("\e[1;31m[ERROR]\e[0m No arguments given.\n\tUSAGE: \e[34m%s "
|
||||||
"\e[33m<text>\e[0m\n",
|
"\e[33m[<text>...]\e[0m\n",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
set_flag(&FLAG_FULLSCREEN, "RBTEXT_FULLSCREEN");
|
set_flag(&FLAG_FULLSCREEN, "RBTEXT_FULLSCREEN");
|
||||||
|
|
||||||
|
char *text;
|
||||||
|
if (!use_pipe) {
|
||||||
uint reqSize = 0;
|
uint reqSize = 0;
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
reqSize += strlen(argv[i]);
|
reqSize += strlen(argv[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *text = (char *)malloc(reqSize + argc);
|
text = (char *)malloc(reqSize + argc);
|
||||||
|
|
||||||
// Adds all the arguments together with spaces seperating them
|
// Adds all the arguments together with spaces seperating them
|
||||||
for (int i = 1, j = 0; i < argc; i++) {
|
for (int i = 1, j = 0; i < argc; i++) {
|
||||||
@ -104,17 +126,22 @@ int main(int argc, char **argv) {
|
|||||||
text[j] = ' ';
|
text[j] = ' ';
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct timespec rem,
|
struct timespec rem,
|
||||||
req = {CONF_SLEEP_TIMESEC, CONF_SLEEP_TIMEMS * 1000 * 1000};
|
req = {CONF_SLEEP_TIMESEC, CONF_SLEEP_TIMEMS * 1000 * 1000};
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
if (!use_pipe) {
|
||||||
if (FLAG_FULLSCREEN == 1)
|
if (FLAG_FULLSCREEN == 1)
|
||||||
printf("\e[2J\e[f");
|
printf("\e[2J\e[f");
|
||||||
else
|
else
|
||||||
printf("\r");
|
printf("\r");
|
||||||
|
}
|
||||||
|
|
||||||
genColors(text);
|
genColors(text);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
nanosleep(&req, &rem);
|
nanosleep(&req, &rem);
|
||||||
t += 1;
|
t += 1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user