skroll

scroll a text to stdout
git clone git://z3bra.org/skroll
Log | Files | Refs | README | LICENSE

commit 39c8d624a1306f052a5a57d005ea7046f91028ab
Author: z3bra <willy@mailoo.org>
Date:   Wed Mar 26 13:28:16 2014

First commit, first release

Diffstat:
 LICENSE   |  14 ++++++++-
 Makefile  |  29 +++++++++++++++++-
 README    |  20 +++++++++++-
 config.mk |  10 ++++++-
 skroll.1  |  24 ++++++++++++++-
 skroll.c  | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 207 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + diff --git a/Makefile b/Makefile @@ -0,0 +1,29 @@ +include config.mk + +.SUFFIXES: .c .o .gz +.PHONY : all clean install uninstall + +.c.o: + @echo -e "CC $<" + @${CC} -c ${CFLAGS} $< -o $@ + +skroll: skroll.o + @echo -e "LD skroll" + @${LD} $^ -o $@ ${LDFLAGS} + +skroll.1.gz: skroll.1 + @echo "GZ $<" + @${GZ} -c $< > $@ + +all : skroll skroll.1.gz + +clean : + ${RM} -f skroll *.o *.gz *~ + +install: skroll skroll.1.gz + install -D -m 0755 skroll ${DESTDIR}${PREFIX}/bin/skroll + install -D -m 0644 skroll.1.gz ${DESTDIR}${MANPREFIX}/man1/skroll.1.gz + +uninstall: + ${RM} ${DESTDIR}${PREFIX}/bin/skroll + ${RM} ${DESTDIR}${MANPREFIX}/man1/skroll.1.gz diff --git a/README b/README @@ -0,0 +1,20 @@ +┏━┓╻┏ ┏━┓┏━┓╻ ╻ +┗━┓┣┻┓┣┳┛┃ ┃┃ ┃ +┗━┛╹ ╹╹┗╸┗━┛┗━╸┗━╸ + -- by z3bra +=========================== + +Skroll is a small utility that you can use to make a text scroll. +Pipe text to it, and it will scroll a given number of letters from right to +left. + +You can pass a few options to it, to change its behavior: + + # loop indefinitely + echo shblah | skroll -l + + # output <n> chars at a time + echo shblah | skroll -n 3 + + # rotate letters every <x> seconds + echo shblah | skroll -d 0.25 diff --git a/config.mk b/config.mk @@ -0,0 +1,10 @@ +# paths +PREFIX:=/usr +MANPREFIX:=${PREFIX}/share/man + +CC = cc +LD= ${CC} +RM = rm +GZ = gzip +CFLAGS = -Wall -pedantic +LDFLAGS = diff --git a/skroll.1 b/skroll.1 @@ -0,0 +1,24 @@ +.TH MAN 1 2014-04-26 "Linux" "skroll" +.SH NAME +skroll - make a given text scroll +.SH SYNOPSIS +.B skroll +.RI [\| \-hl\| ]\ [\| -n\ num\| ]\ [\| -d\ delay\| ] +.SH DESCRIPTION +.PP +.B skroll +reads text on stdin and make it scroll to stdout +.TP +.B \-h +Display a help text +.B \-l +Loop text forever +.B \-n num +Show <num> char at a time +.B \-d delay +Make scroll steps last <delay> seconds +.SH BUGS +.PP +No bugs known actually. Feel free to report them at willy@mailoo.org +.SH AUTHORS / CONTRIBUTORS +Willy Goiffon is the main author. diff --git a/skroll.c b/skroll.c @@ -0,0 +1,110 @@ +/* + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * Version 2, December 2004 + * + * Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> + * + * Everyone is permitted to copy and distribute verbatim or modified + * copies of this license document, and changing it is allowed as long + * as the name is changed. + * + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + * + * 0. You just DO WHAT THE FUCK YOU WANT TO. + * + */ + +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <getopt.h> + +#define BUFFER_SIZE 512 + +static bool loop = false; /* wether to loop text or not */ +static float delay = 1; /* scroll speed, in usec */ +static int number = 10; /* number of chars to be shown at the same time */ + +void zero_fill (char **str, size_t num) +{ + int c; + size_t s; + + s = strnlen((*str),num); + + for (c=0; c<num; ++c) { + if ( (*str)[c] == '\n' || c >= s ) { + (*str)[c] = ' '; + } + } + + (*str)[num] = 0; + + return; /* void */ +} + +void skroll (const char *input) +{ + int offset = 0; + char *buf = NULL; + + if ( !(buf = calloc (number, sizeof(char))) ) return; + buf[number] = 0; + + do { + for (offset=0; input[offset]; ++offset) { + strncpy(buf, input + offset, number-1); + zero_fill(&buf, number); + printf("\r%s", buf); + + fflush(stdout); + usleep(delay*1000000); + } + } while(loop); + + putc('\n', stdout); + + return; /* void */ +} + +const char *bufferize (FILE *stream) +{ + char *buf = NULL; + + if ( !(buf = calloc (BUFFER_SIZE, sizeof(char))) ) return NULL; + + buf[BUFFER_SIZE] = 0; + + if ( feof(stream) || !fgets(buf, BUFFER_SIZE, stream) ) { + free (buf); + return NULL; + } + + return buf; +} + +int main (int argc, char **argv) +{ + char ch; + const char *buf = NULL; + + while ( (ch = getopt(argc, argv, "hd:ln:")) != -1 ) { + switch (ch) { + case 'h': + printf("usage: %s [-hl] [-d delay] [-n number]", argv[0]); + break; + case 'd': delay = strtof(optarg, NULL); break; + case 'n': number = strtoul(optarg, NULL, 10); break; + case 'l': loop = true; break; + } + } + + while( (buf = bufferize(stdin)) != NULL ) { + skroll(buf); + } + + return 0; +}