Skip to content
Snippets Groups Projects
Commit 6c2ee0d6 authored by Yorhel's avatar Yorhel
Browse files

Add --color option, simple theme support, disable colors by default

I've decided not to use ls-like file name coloring for now, instead just
coloring the difference between a (regular) file and a dir.

Still looking for a good color scheme for light backgrounds.
parent a369a43d
Branches
No related tags found
No related merge requests found
...@@ -118,6 +118,12 @@ prefixes, that is, powers of 1024 (KiB, MiB, etc). ...@@ -118,6 +118,12 @@ prefixes, that is, powers of 1024 (KiB, MiB, etc).
Requires a confirmation before quitting ncdu. Very helpful when you Requires a confirmation before quitting ncdu. Very helpful when you
accidentally press 'q' during or after a very long scan. accidentally press 'q' during or after a very long scan.
=item --color I<SCHEME>
Select a color scheme. Currently only two schemes are recognized: I<off> to
disable colors (the default) and I<dark> for a color scheme intended for dark
backgrounds.
=back =back
=head2 Scan Options =head2 Scan Options
......
...@@ -64,13 +64,13 @@ static void browse_draw_info(struct dir *dr) { ...@@ -64,13 +64,13 @@ static void browse_draw_info(struct dir *dr) {
ncmove(6, 18); ncmove(6, 18);
printsize(UIC_DEFAULT, dr->size); printsize(UIC_DEFAULT, dr->size);
addstrc(UIC_DEFAULT, " ("); addstrc(UIC_DEFAULT, " (");
addstrc(UIC_KEYNUM, fullsize(dr->size)); addstrc(UIC_NUM, fullsize(dr->size));
addstrc(UIC_DEFAULT, " B)"); addstrc(UIC_DEFAULT, " B)");
ncmove(7, 18); ncmove(7, 18);
printsize(UIC_DEFAULT, dr->asize); printsize(UIC_DEFAULT, dr->asize);
addstrc(UIC_DEFAULT, " ("); addstrc(UIC_DEFAULT, " (");
addstrc(UIC_KEYNUM, fullsize(dr->asize)); addstrc(UIC_NUM, fullsize(dr->asize));
addstrc(UIC_DEFAULT, " B)"); addstrc(UIC_DEFAULT, " B)");
break; break;
...@@ -88,7 +88,7 @@ static void browse_draw_info(struct dir *dr) { ...@@ -88,7 +88,7 @@ static void browse_draw_info(struct dir *dr) {
} }
ncaddstr(9, 31, "Press "); ncaddstr(9, 31, "Press ");
addchc(UIC_KEYNUM, 'i'); addchc(UIC_KEY, 'i');
addstrc(UIC_DEFAULT, " to hide this window"); addstrc(UIC_DEFAULT, " to hide this window");
} }
...@@ -129,7 +129,7 @@ static void browse_draw_graph(struct dir *n, int *x) { ...@@ -129,7 +129,7 @@ static void browse_draw_graph(struct dir *n, int *x) {
pc = (float)(show_as ? n->parent->asize : n->parent->size); pc = (float)(show_as ? n->parent->asize : n->parent->size);
if(pc < 1) if(pc < 1)
pc = 1.0f; pc = 1.0f;
uic_set(c == UIC_SEL ? UIC_KEYNUM_SEL : UIC_KEYNUM); uic_set(c == UIC_SEL ? UIC_NUM_SEL : UIC_NUM);
printw("%5.1f", ((float)(show_as ? n->asize : n->size) / pc) * 100.0f); printw("%5.1f", ((float)(show_as ? n->asize : n->size) / pc) * 100.0f);
addchc(c, '%'); addchc(c, '%');
} }
...@@ -158,10 +158,10 @@ static void browse_draw_items(struct dir *n, int *x) { ...@@ -158,10 +158,10 @@ static void browse_draw_items(struct dir *n, int *x) {
if(n->items > 99999) { if(n->items > 99999) {
addstrc(c, "> "); addstrc(c, "> ");
addstrc(c == UIC_SEL ? UIC_KEYNUM_SEL : UIC_KEYNUM, "100"); addstrc(c == UIC_SEL ? UIC_NUM_SEL : UIC_NUM, "100");
addchc(c, 'k'); addchc(c, 'k');
} else if(n->items) { } else if(n->items) {
uic_set(c == UIC_SEL ? UIC_KEYNUM_SEL : UIC_KEYNUM); uic_set(c == UIC_SEL ? UIC_NUM_SEL : UIC_NUM);
printw("%6s", fullsize(n->items)); printw("%6s", fullsize(n->items));
} }
} }
...@@ -189,7 +189,9 @@ static void browse_draw_item(struct dir *n, int row) { ...@@ -189,7 +189,9 @@ static void browse_draw_item(struct dir *n, int row) {
browse_draw_items(n, &x); browse_draw_items(n, &x);
move(row, x); move(row, x);
addchc(c == UIC_SEL ? UIC_DIR_SEL : UIC_DIR, n->flags & FF_DIR ? '/' : ' '); if(n->flags & FF_DIR)
c = c == UIC_SEL ? UIC_DIR_SEL : UIC_DIR;
addchc(c, n->flags & FF_DIR ? '/' : ' ');
addstrc(c, cropstr(n->name, wincols-x-1)); addstrc(c, cropstr(n->name, wincols-x-1));
} }
...@@ -206,7 +208,7 @@ void browse_draw() { ...@@ -206,7 +208,7 @@ void browse_draw() {
uic_set(UIC_HD); uic_set(UIC_HD);
mvhline(0, 0, ' ', wincols); mvhline(0, 0, ' ', wincols);
mvprintw(0,0,"%s %s ~ Use the arrow keys to navigate, press ", PACKAGE_NAME, PACKAGE_VERSION); mvprintw(0,0,"%s %s ~ Use the arrow keys to navigate, press ", PACKAGE_NAME, PACKAGE_VERSION);
addchc(UIC_KEYNUM_HD, '?'); addchc(UIC_KEY_HD, '?');
addstrc(UIC_HD, " for help"); addstrc(UIC_HD, " for help");
if(dir_import_active) if(dir_import_active)
mvaddstr(0, wincols-10, "[imported]"); mvaddstr(0, wincols-10, "[imported]");
...@@ -229,10 +231,10 @@ void browse_draw() { ...@@ -229,10 +231,10 @@ void browse_draw() {
mvaddstr(winrows-1, 0, " Total disk usage: "); mvaddstr(winrows-1, 0, " Total disk usage: ");
printsize(UIC_HD, t->parent->size); printsize(UIC_HD, t->parent->size);
addstrc(UIC_HD, " Apparent size: "); addstrc(UIC_HD, " Apparent size: ");
uic_set(UIC_KEYNUM_HD); uic_set(UIC_NUM_HD);
printsize(UIC_HD, t->parent->asize); printsize(UIC_HD, t->parent->asize);
addstrc(UIC_HD, " Items: "); addstrc(UIC_HD, " Items: ");
uic_set(UIC_KEYNUM_HD); uic_set(UIC_NUM_HD);
printw("%d", t->parent->items); printw("%d", t->parent->items);
} else } else
mvaddstr(winrows-1, 0, " No items to display."); mvaddstr(winrows-1, 0, " No items to display.");
......
...@@ -71,7 +71,7 @@ static void delete_draw_progress() { ...@@ -71,7 +71,7 @@ static void delete_draw_progress() {
ncaddstr(1, 2, cropstr(getpath(curdir), 47)); ncaddstr(1, 2, cropstr(getpath(curdir), 47));
ncaddstr(4, 41, "Press "); ncaddstr(4, 41, "Press ");
addchc(UIC_KEYNUM, 'q'); addchc(UIC_KEY, 'q');
addstrc(UIC_DEFAULT, " to abort"); addstrc(UIC_DEFAULT, " to abort");
} }
......
...@@ -132,7 +132,7 @@ static void draw_progress() { ...@@ -132,7 +132,7 @@ static void draw_progress() {
nccreate(10, width, antext); nccreate(10, width, antext);
ncaddstr(2, 2, "Total items: "); ncaddstr(2, 2, "Total items: ");
uic_set(UIC_KEYNUM); uic_set(UIC_NUM);
printw("%-8d", dir_output.items); printw("%-8d", dir_output.items);
if(dir_output.size) { if(dir_output.size) {
...@@ -144,11 +144,11 @@ static void draw_progress() { ...@@ -144,11 +144,11 @@ static void draw_progress() {
ncprint(3, 2, "Current item: %s", cropstr(dir_curpath, width-18)); ncprint(3, 2, "Current item: %s", cropstr(dir_curpath, width-18));
if(confirm_quit_while_scanning_stage_1_passed) { if(confirm_quit_while_scanning_stage_1_passed) {
ncaddstr(8, width-26, "Press "); ncaddstr(8, width-26, "Press ");
addchc(UIC_KEYNUM, 'y'); addchc(UIC_KEY, 'y');
addstrc(UIC_DEFAULT, " to confirm abort"); addstrc(UIC_DEFAULT, " to confirm abort");
} else { } else {
ncaddstr(8, width-18, "Press "); ncaddstr(8, width-18, "Press ");
addchc(UIC_KEYNUM, 'q'); addchc(UIC_KEY, 'q');
addstrc(UIC_DEFAULT, " to abort"); addstrc(UIC_DEFAULT, " to abort");
} }
......
...@@ -62,7 +62,7 @@ void help_draw() { ...@@ -62,7 +62,7 @@ void help_draw() {
nccreate(15, 60, "ncdu help"); nccreate(15, 60, "ncdu help");
ncaddstr(13, 42, "Press "); ncaddstr(13, 42, "Press ");
uic_set(UIC_KEYNUM); uic_set(UIC_KEY);
addch('q'); addch('q');
uic_set(UIC_DEFAULT); uic_set(UIC_DEFAULT);
addstr(" to close"); addstr(" to close");
...@@ -75,7 +75,7 @@ void help_draw() { ...@@ -75,7 +75,7 @@ void help_draw() {
case 1: case 1:
line = 1; line = 1;
for(i=start*2; i<start*2+20; i+=2) { for(i=start*2; i<start*2+20; i+=2) {
uic_set(UIC_KEYNUM); uic_set(UIC_KEY);
ncaddstr(++line, 13-strlen(keys[i]), keys[i]); ncaddstr(++line, 13-strlen(keys[i]), keys[i]);
uic_set(UIC_DEFAULT); uic_set(UIC_DEFAULT);
ncaddstr(line, 15, keys[i+1]); ncaddstr(line, 15, keys[i+1]);
......
...@@ -131,6 +131,7 @@ static void argv_parse(int argc, char **argv) { ...@@ -131,6 +131,7 @@ static void argv_parse(int argc, char **argv) {
{ 'C', 0, "--exclude-caches" }, { 'C', 0, "--exclude-caches" },
{ 's', 0, "--si" }, { 's', 0, "--si" },
{ 'Q', 0, "--confirm-quit" }, { 'Q', 0, "--confirm-quit" },
{ 'c', 1, "--color" },
{0,0,NULL} {0,0,NULL}
}; };
...@@ -180,6 +181,14 @@ static void argv_parse(int argc, char **argv) { ...@@ -180,6 +181,14 @@ static void argv_parse(int argc, char **argv) {
case 'C': case 'C':
cachedir_tags = 1; cachedir_tags = 1;
break; break;
case 'c':
if(strcmp(val, "off") == 0) { uic_theme = 0; }
if(strcmp(val, "dark") == 0) { uic_theme = 1; }
else {
fprintf(stderr, "Unknown --color option: %s\n", val);
exit(1);
}
break;
case -2: case -2:
fprintf(stderr, "ncdu: %s.\n", val); fprintf(stderr, "ncdu: %s.\n", val);
exit(1); exit(1);
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <locale.h> #include <locale.h>
#endif #endif
int uic_theme;
int winrows, wincols; int winrows, wincols;
int subwinr, subwinc; int subwinr, subwinc;
int si; int si;
...@@ -87,7 +88,7 @@ float formatsize(int64_t from, char **unit) { ...@@ -87,7 +88,7 @@ float formatsize(int64_t from, char **unit) {
void printsize(enum ui_coltype t, int64_t from) { void printsize(enum ui_coltype t, int64_t from) {
char *unit; char *unit;
float r = formatsize(from, &unit); float r = formatsize(from, &unit);
uic_set(t == UIC_HD ? UIC_KEYNUM_HD : t == UIC_SEL ? UIC_KEYNUM_SEL : UIC_KEYNUM); uic_set(t == UIC_HD ? UIC_NUM_HD : t == UIC_SEL ? UIC_NUM_SEL : UIC_NUM);
printw("%5.1f", r); printw("%5.1f", r);
addchc(t, ' '); addchc(t, ' ');
addstrc(t, unit); addstrc(t, unit);
...@@ -201,7 +202,7 @@ void ncprint(int r, int c, char *fmt, ...) { ...@@ -201,7 +202,7 @@ void ncprint(int r, int c, char *fmt, ...) {
void nctab(int c, int sel, int num, char *str) { void nctab(int c, int sel, int num, char *str) {
uic_set(sel ? UIC_KEYNUM_HD : UIC_KEYNUM); uic_set(sel ? UIC_KEY_HD : UIC_KEY);
ncprint(0, c, "%d", num); ncprint(0, c, "%d", num);
uic_set(sel ? UIC_HD : UIC_DEFAULT); uic_set(sel ? UIC_HD : UIC_DEFAULT);
addch(':'); addch(':');
...@@ -211,24 +212,36 @@ void nctab(int c, int sel, int num, char *str) { ...@@ -211,24 +212,36 @@ void nctab(int c, int sel, int num, char *str) {
static int colors[] = { static int colors[] = {
#define C(name, fg, bg, attr) 0, #define C(name, ...) 0,
UI_COLORS UI_COLORS
#undef C #undef C
0 0
}; };
static int lastcolor = 0; static int lastcolor = 0;
void uic_init() {
start_color();
use_default_colors();
int i=0; static const struct {
#define C(name, fg, bg, attr) \ short fg, bg;
init_pair(i+1, fg, bg);\ int attr;
colors[i] = attr | COLOR_PAIR(i+1);\ } color_defs[] = {
i++; #define C(name, off_fg, off_bg, off_a, dark_fg, dark_bg, dark_a) \
{off_fg, off_bg, off_a}, \
{dark_fg, dark_bg, dark_a},
UI_COLORS UI_COLORS
#undef C #undef C
{0,0,0}
};
void uic_init() {
size_t i, j;
start_color();
use_default_colors();
for(i=0; i<sizeof(colors)/sizeof(*colors)-1; i++) {
j = i*2 + uic_theme;
init_pair(i+1, color_defs[j].fg, color_defs[j].bg);
colors[i] = color_defs[j].attr | COLOR_PAIR(i+1);
}
} }
void uic_set(enum ui_coltype c) { void uic_set(enum ui_coltype c) {
......
...@@ -30,30 +30,36 @@ ...@@ -30,30 +30,36 @@
#include <ncurses.h> #include <ncurses.h>
/* UI colors: C(name, foreground, background, attrs) */ /* UI colors: (foreground, background, attrs)
* NAME OFF DARK
*/
#define UI_COLORS \ #define UI_COLORS \
C(DEFAULT, -1, -1, 0 )\ C(DEFAULT, -1,-1,0 , -1, -1, 0 )\
C(BOX_TITLE, COLOR_BLUE, -1, A_BOLD)\ C(BOX_TITLE, -1,-1,A_BOLD , COLOR_BLUE, -1, A_BOLD)\
C(HD, COLOR_BLACK, COLOR_CYAN, 0 ) /* header & footer */\ C(HD, -1,-1,A_REVERSE , COLOR_BLACK, COLOR_CYAN, 0 ) /* header & footer */\
C(SEL, COLOR_WHITE, COLOR_GREEN, A_BOLD)\ C(SEL, -1,-1,A_REVERSE , COLOR_WHITE, COLOR_GREEN,A_BOLD)\
C(KEYNUM, COLOR_YELLOW, -1, A_BOLD)\ C(NUM, -1,-1,0 , COLOR_YELLOW, -1, A_BOLD)\
C(KEYNUM_HD, COLOR_YELLOW, COLOR_CYAN, A_BOLD)\ C(NUM_HD, -1,-1,A_REVERSE , COLOR_YELLOW, COLOR_CYAN, A_BOLD)\
C(KEYNUM_SEL, COLOR_YELLOW, COLOR_GREEN, A_BOLD)\ C(NUM_SEL, -1,-1,A_REVERSE , COLOR_YELLOW, COLOR_GREEN,A_BOLD)\
C(DIR, COLOR_BLUE, -1, A_BOLD)\ C(KEY, -1,-1,A_BOLD , COLOR_YELLOW, -1, A_BOLD)\
C(DIR_SEL, COLOR_BLUE, COLOR_GREEN, A_BOLD)\ C(KEY_HD, -1,-1,A_BOLD|A_REVERSE, COLOR_YELLOW, COLOR_CYAN, A_BOLD)\
C(FLAG, COLOR_RED, -1, 0 )\ C(DIR, -1,-1,0 , COLOR_BLUE, -1, A_BOLD)\
C(FLAG_SEL, COLOR_RED, COLOR_GREEN, 0 )\ C(DIR_SEL, -1,-1,A_REVERSE , COLOR_BLUE, COLOR_GREEN,A_BOLD)\
C(GRAPH, COLOR_MAGENTA, -1, 0 )\ C(FLAG, -1,-1,0 , COLOR_RED, -1, 0 )\
C(GRAPH_SEL, COLOR_MAGENTA, COLOR_GREEN, 0 ) C(FLAG_SEL, -1,-1,A_REVERSE , COLOR_RED, COLOR_GREEN,0 )\
C(GRAPH, -1,-1,0 , COLOR_MAGENTA,-1, 0 )\
C(GRAPH_SEL, -1,-1,A_REVERSE , COLOR_MAGENTA,COLOR_GREEN,0 )
enum ui_coltype { enum ui_coltype {
#define C(name, fg, bg, attr) UIC_##name, #define C(name, ...) UIC_##name,
UI_COLORS UI_COLORS
#undef C #undef C
UIC_NONE UIC_NONE
}; };
/* Color & attribute manipulation */ /* Color & attribute manipulation */
extern int uic_theme;
void uic_init(); void uic_init();
void uic_set(enum ui_coltype); void uic_set(enum ui_coltype);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment