From 383abc8aa9265f3a81142973106dcaeaded63e85 Mon Sep 17 00:00:00 2001 From: dok Date: Sun, 6 Jan 2019 14:10:16 +0100 Subject: Add colors --- noice.c | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'noice.c') diff --git a/noice.c b/noice.c index 1def920..4040855 100644 --- a/noice.c +++ b/noice.c @@ -45,6 +45,11 @@ struct assoc { char *bin; /* Program */ }; +struct cpair { + int fg; + int bg; +}; + /* Supported actions */ enum action { SEL_QUIT = 1, @@ -269,6 +274,16 @@ entrycmp(const void *va, const void *vb) return strcmp(a->name, b->name); } +void +initcolor(void) +{ + int i; + + start_color(); + for (i = 1; i < LEN(pairs); i++) + init_pair(i, pairs[i].fg, pairs[i].bg); +} + void initcurses(void) { @@ -282,6 +297,8 @@ initcurses(void) fprintf(stderr, "failed to initialize curses\n"); exit(1); } + if (has_colors()) + initcolor(); cbreak(); noecho(); nonl(); @@ -420,37 +437,45 @@ void printent(struct entry *ent, int active) { char name[PATH_MAX]; - unsigned int maxlen = COLS - strlen(CURSR) - 1; + unsigned int len = COLS - strlen(CURSR) - 1; char cm = 0; + int attr = 0; /* Copy name locally */ strlcpy(name, ent->name, sizeof(name)); + /* No text wrapping in entries */ + if (strlen(name) < len) + len = strlen(name) + 1; + if (S_ISDIR(ent->mode)) { cm = '/'; - maxlen--; + attr |= DIR_ATTR; } else if (S_ISLNK(ent->mode)) { cm = '@'; - maxlen--; + attr |= LINK_ATTR; } else if (S_ISSOCK(ent->mode)) { cm = '='; - maxlen--; + attr |= SOCK_ATTR; } else if (S_ISFIFO(ent->mode)) { cm = '|'; - maxlen--; + attr |= FIFO_ATTR; } else if (ent->mode & S_IXUSR) { cm = '*'; - maxlen--; + attr |= EXEC_ATTR; } - /* No text wrapping in entries */ - if (strlen(name) > maxlen) - name[maxlen] = '\0'; + if (active) + attr |= CURSR_ATTR; - if (cm == 0) - printw("%s%s\n", active ? CURSR : EMPTY, name); - else - printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm); + if (cm) { + name[len-1] = cm; + name[len] = '\0'; + } + + attron(attr); + printw("%s%s\n", active ? CURSR : EMPTY, name); + attroff(attr); } int -- cgit v1.2.3