logo

utils

~/.local/bin tools and git-hooks
commit: bb1e01c41ce37b9f1e711643a433baeb4dec099b
parent: 810306aec2857fd6970275cbfbe57b558b435658
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon, 10 Feb 2020 07:18:43 +0100

src/xcd.c: new, a lolcat hexdump

Diffstat:

Mmkfile3+++
Asrc/xcd.c51+++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 54 insertions(+), 0 deletions(-)

diff --git a/mkfile b/mkfile @@ -24,3 +24,6 @@ bin/&: src/&.c bin/lolcat: src/lolcat.c $CC $CDEFS $CFLAGS -lm src/lolcat.c -o bin/lolcat + +bin/xcd: src/xcd.c + $CC $CDEFS $CFLAGS -lm src/xcd.c -o bin/xcd diff --git a/src/xcd.c b/src/xcd.c @@ -0,0 +1,51 @@ +// Copyright 2018-2020 Haelwenn (lanodan) Monnier <contact@hacktivis.me> +// Distributed under the terms of the CC-BY-SA-4.0 license + +#include <stdio.h> /* getchar(), putchar(), snprintf(), printf() */ +#include <math.h> /* sin() */ +#include <stdint.h> + +void rainbow(double freq, int i) { + uint8_t red, green, blue; + double pi = 3.14159; + + red = sin(freq*i + 0) * 127 + 128; + green = sin(freq*i + 2*pi/3) * 127 + 128; + blue = sin(freq*i + 4*pi/3) * 127 + 128; + + printf("[38;2;%02d;%02d;%02dm", red, green, blue); + //printf("[48;2;%02d;%02d;%02dm", red, green, blue); + // TODO: Replace to sprintf? +} + +int main(void) { + int cols = 0, bytes = 0; + double freq = 0.2; + char c; + + printf(""); + + rainbow(freq, bytes); + printf("%08x ", bytes); + while((c = getc(stdin)) != EOF) + { + if(cols >= 16) { + cols = 0; + rainbow(freq, bytes); + printf("\n%08x ", bytes); + } else { + if(cols > 0 && ((cols % 2) == 0)) printf(" "); + if(cols > 0 && ((cols % 8) == 0)) printf(" "); + } + + rainbow(freq, c); + printf("%02X", c); + cols++; + bytes++; + } + + rainbow(freq, bytes); + printf("\n%08x\n", bytes); + + return 0; +}