mkb

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

commit cdd01da820c711fda6ae491e7513c7011115969e
parent c7e2e0c80d4cb55c12d4fa99faba1922d0d9aa36
Author: z3bra <willyatmailoodotorg>
Date:   Mon,  5 Oct 2015 13:25:19 +0200

Added 3 environment variables: START, END, SEP

START: Printed before the bar, defaults to brightwhite ANSI escape
END  : Printed after the bar, defaults to color reset ANSI escape
SEP  : Printed between done/remaining chars, defaults to brightwhite '╋'

Diffstat:
MMakefile | 7+++----
Mmkb.c | 25++++++++++++++++++-------
2 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,15 +1,14 @@ -PREFIX:=/usr +PREFIX:=/usr/local MANPREFIX:=$(PREFIX)/share/man -CC := tcc +CC := cc LD := $(CC) CFLAGS += -pedantic -Wall LDFLAGS += BIN = mkb -SRC = \ - mkb.c +SRC = mkb.c OBJ = $(SRC:.c=.o) diff --git a/mkb.c b/mkb.c @@ -5,22 +5,31 @@ #include <err.h> #define DEFAULT_SIZE 32 -#define DEFAULT_CHAR1 "▣" -#define DEFAULT_CHAR2 "▣" +#define DEFAULT_CHAR1 "━" +#define DEFAULT_CHAR2 "━" +#define DEFAULT_START "" +#define DEFAULT_END "" +#define DEFAULT_SEP "╋" int main (int argc, char **argv) { int i; - float size = 0; + float size = 0; float value = 0; + char *end = NULL; + char *sep = NULL; char *char1 = NULL; char *char2 = NULL; - char *current = NULL; + char *begin = NULL; + char **current = NULL; - size = getenv("SIZE") ? atoi(getenv("SIZE")) : DEFAULT_SIZE; + size = getenv("SIZE") ? atoi(getenv("SIZE")) : DEFAULT_SIZE; char1 = getenv("CHAR1") ? getenv("CHAR1") : DEFAULT_CHAR1; char2 = getenv("CHAR2") ? getenv("CHAR2") : DEFAULT_CHAR2; + begin = getenv("START") ? getenv("START") : DEFAULT_START; + end = getenv("END") ? getenv("END") : DEFAULT_END; + sep = getenv("SEP") ? getenv("SEP") : DEFAULT_SEP; if (argc < 2) scanf("%f", &value); @@ -30,10 +39,12 @@ main (int argc, char **argv) if (value > 100) errx(1, "value should remain between 0 and 100"); + write(fileno(stdout), begin, strnlen(begin, 32)); for (i=0; i<size; i++) { - current = (i < value / 100 * size) ? char1 : char2; - write(fileno(stdout), current, strnlen(current, 32)); + current = (i < value / 100 * size) ? &char1 : (current == &char1 ? &sep : &char2); + write(fileno(stdout), *current, strnlen(*current, 32)); } + write(fileno(stdout), end, strnlen(end, 32)); putc('\n', stdout);