xcd.c (1439B)
1 // Copyright 2018-2020 Haelwenn (lanodan) Monnier <contact@hacktivis.me> 2 // Distributed under the terms of the CC-BY-SA-4.0 license 3 4 #include <stdio.h> /* printf(), fread() */ 5 #include <math.h> /* sin() */ 6 #include <stdint.h> /* uint8_t */ 7 #include <string.h> /* memset() */ 8 #include <ctype.h> /* isprint() */ 9 10 #define LANODAN_XCD_RESET printf("[0m[48;2;0;0;0m"); 11 #define LANODAN_XCD_PRINT printf(" >%s<", line); 12 13 void rainbow(double freq, char i) { 14 uint8_t red, green, blue; 15 double pi = 3.14159; 16 17 red = sin(freq*i + 0) * 127 + 128; 18 green = sin(freq*i + 2*pi/3) * 127 + 128; 19 blue = sin(freq*i + 4*pi/3) * 127 + 128; 20 21 printf("[38;2;%02d;%02d;%02dm", red, green, blue); 22 } 23 24 int main(void) { 25 int cols = 0, bytes = 0; 26 int line_width = 16; 27 double freq = 0.2; 28 char c, line[line_width]; 29 30 memset(&line, 0, line_width); 31 32 LANODAN_XCD_RESET 33 34 rainbow(freq, bytes); 35 printf("%06x ", bytes); 36 while(fread(&c, 1, 1, stdin) > 0) 37 { 38 if(cols >= line_width) { 39 cols = 0; 40 41 LANODAN_XCD_RESET 42 LANODAN_XCD_PRINT 43 memset(&line, 0, line_width); 44 45 rainbow(freq, bytes); 46 printf("\n%06x ", bytes); 47 } 48 49 rainbow(freq, c); 50 printf("%02hhx ", c); 51 line[cols] = isprint(c) ? c : '.'; 52 53 cols++; 54 bytes++; 55 } 56 57 58 // Fill the rest of the hex space with spaces 59 for(cols; cols < line_width; cols++) printf(" "); 60 61 LANODAN_XCD_RESET 62 LANODAN_XCD_PRINT 63 64 rainbow(freq, bytes); 65 printf("\n%06x\n", bytes); 66 LANODAN_XCD_RESET 67 68 return 0; 69 }