logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: ddbe20ca32a6bfd1b3b5536af1707cbdf981e829
parent e07a034ec948f1fa25d83170755e26ebb19135f2
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 15 May 2021 02:49:10 +0200

bin/lolcat: Add ability to open files

Diffstat:

Mbin/lolcat.114+++++++++-----
Mbin/lolcat.c77++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
2 files changed, 77 insertions(+), 14 deletions(-)

diff --git a/bin/lolcat.1 b/bin/lolcat.1 @@ -1,7 +1,7 @@ .\" Collection of Unix tools, comparable to coreutils .\" Copyright 2017-2021 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> .\" SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only -.Dd 2019-03-03 +.Dd 2021-05-15 .Dt LOLCAT 1 .Os .Sh NAME @@ -9,14 +9,17 @@ .Nd a truecolor rainbow filter .Sh SYNOPSIS .Nm -takes no options +.Ar .Sh DESCRIPTION .Nm takes each character from it's standard input and prints it with a truecolor escape sequence, making the text output rainbow colored given enough characters. +.Pp +.Nm +also replaces escape to +.Sq ^[ +in bold, this allows to strip escape codes easily. .Sh KNOWN ISSUES & LIMITATIONS -.Bl -.It -Does not strip already existing escape codes, thus creating a mess when some are already present, considered a non-issue since programs should avoid coloring piped output. +.Bl -bullet .It Frequence is not adjustable yet .It @@ -24,6 +27,7 @@ Frequence is not adjustable yet puts escape codes even when stdout isn't a tty, this is expected, do not call .Nm otherwise +.El .Sh EXIT STATUS .Ex -std .Sh SEE ALSO diff --git a/bin/lolcat.c b/bin/lolcat.c @@ -3,8 +3,10 @@ // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only #define _POSIX_C_SOURCE 200809L -#include <math.h> /* sin() */ -#include <stdio.h> /* getchar(), putchar(), snprintf(), printf() */ +#include <errno.h> /* errno */ +#include <math.h> /* sin() */ +#include <stdio.h> /* fgetc(), fputc(), printf(), fopen(), fclose() */ +#include <string.h> /* strerror() */ void rainbow(double freq, int i) @@ -17,22 +19,79 @@ rainbow(double freq, int i) blue = sin(freq * i + 4 * pi / 3) * 127 + 128; printf("[38;2;%02d;%02d;%02dm", red, green, blue); - // TODO: Replace to sprintf? } int -main(void) +concat(FILE *stream) { - int c, i; double freq = 0.1; + int i = 0; - i = 0; - - while((c = getchar()) != EOF) + int c; + errno = 0; + while((c = fgetc(stream)) != EOF) { rainbow(freq, i); i++; - putchar(c); + if(c == '') + { + printf("^["); + continue; + } + + if(fputc(c, stdout) == EOF) + { + printf("\nWrite error: %s\n", strerror(errno)); + return 1; + } + } + + if(c == EOF && errno != 0) + { + printf("\nRead error: %s\n", strerror(errno)); + return 1; + } + + return 0; +} + +int +main(int argc, char *argv[]) +{ + if(argc <= 1) + { + return concat(stdin); + } + + for(int argi = 1; argi < argc; argi++) + { + if(strncmp(argv[argi], "-", 2) == 0) + { + if(concat(stdin) != 0) + { + return 1; + } + } + else + { + FILE *file = fopen(argv[argi], "r"); + if(!file) + { + printf("\nError opening ā€˜%sā€™: %s\n", argv[argi], strerror(errno)); + } + else + { + if(concat(file) != 0) + { + return 1; + } + + if(fclose(file) != 0) + { + return 1; + } + } + } } printf("");