mkb

progress bar maker
git clone git://git.2f30.org/mkb.git
Log | Files | Refs

mkb.c (1291B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <unistd.h>
      4 #include <string.h>
      5 #include <err.h>
      6 
      7 #define DEFAULT_SIZE 32
      8 #define DEFAULT_CHAR1 "━"
      9 #define DEFAULT_CHAR2 "━"
     10 #define DEFAULT_START ""
     11 #define DEFAULT_END   ""
     12 #define DEFAULT_SEP   "╋"
     13 
     14 int
     15 main (int argc, char **argv)
     16 {
     17 	int i;
     18 	float size  = 0;
     19 	float value = 0;
     20 	char *end   = NULL;
     21 	char *sep   = NULL;
     22 	char *char1 = NULL;
     23 	char *char2 = NULL;
     24 	char *begin = NULL;
     25 	char **current = NULL;
     26 
     27 	size = getenv("SIZE")   ? atoi(getenv("SIZE")) : DEFAULT_SIZE;
     28 	char1 = getenv("CHAR1") ? getenv("CHAR1") : DEFAULT_CHAR1;
     29 	char2 = getenv("CHAR2") ? getenv("CHAR2") : DEFAULT_CHAR2;
     30 	begin = getenv("START") ? getenv("START") : DEFAULT_START;
     31 	end   = getenv("END")   ? getenv("END")   : DEFAULT_END;
     32 	sep   = getenv("SEP")   ? getenv("SEP")   : DEFAULT_SEP;
     33 
     34 	if (argc < 2)
     35 		scanf("%f", &value);
     36 	else
     37 		value = atof(argv[1]);
     38 
     39 	if (value > 100)
     40 		errx(1, "value should remain between 0 and 100");
     41 
     42 	write(fileno(stdout), begin, strnlen(begin, 32));
     43 	for (i=0; i<size; i++) {
     44 		current = (i < value / 100 * size) ? &char1 : (current == &char1 ? &sep : &char2);
     45 		write(fileno(stdout), *current, strnlen(*current, 32));
     46 	}
     47 	write(fileno(stdout), end, strnlen(end, 32));
     48 
     49 	putc('\n', stdout);
     50 
     51 	return 0;
     52 }