zebra

create zebra lines over stdin
git clone git://jb55.com/zebra
Log | Files | Refs | README

zebra.c (1094B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 
      5 #define streq(a,b) strcmp(a,b) == 0
      6 #define RESET printf("\x1B[0m")
      7 #define DARK "8m"
      8 #define LIGHT "254m"
      9 #define BASE "\x1B[48;5;"
     10 
     11 void usage()
     12 {
     13 	printf("usage: zebra [OPTION] < file\n\n");
     14 	printf("OPTION\n\n");
     15 	printf("     --light        set light background\n");
     16 	printf("     --color 128    set any background color\n\n");
     17 	exit(1);
     18 }
     19 
     20 int main(int argc, const char* argv[])
     21 {
     22 	int c;
     23 	int alt = 0;
     24 	int light = 0;
     25 	char bgcol[32] = {0};
     26 
     27 	if (argc >= 2 && streq(argv[1], "--help")) {
     28 		usage();
     29 	}
     30 	else if (argc >= 2 && streq(argv[1], "--light")) {
     31 		light = 1;
     32 	}
     33 	else if (argc >= 3 && streq(argv[1], "--color")) {
     34 		snprintf(bgcol, sizeof(bgcol), BASE "%sm", argv[2]);
     35 	}
     36 
     37 	if (bgcol[0] == 0) {
     38 		if (light)
     39 			strcpy(bgcol, BASE LIGHT);
     40 		else
     41 			strcpy(bgcol, BASE DARK);
     42 	}
     43 
     44 	// TODO: files
     45 	FILE *stream = stdin;
     46 
     47 	while (1) {
     48 		c = getc(stream);
     49 
     50 		if (c == EOF)
     51 			break;
     52 
     53 		putc(c, stdout);
     54 
     55 		if (c == '\n') {
     56 			alt ^= 1;
     57 			if (alt) {
     58 				printf("%s", bgcol);
     59 			}
     60 			else {
     61 				RESET;
     62 			}
     63 		}
     64 	}
     65 
     66 	RESET;
     67 }