From d6a129ff5afe6b33046836dc805b634986e8f53a Mon Sep 17 00:00:00 2001 From: Yorhel Date: Fri, 28 Feb 2025 20:11:19 +0100 Subject: [PATCH] Add --graph-style + use integer calculations Backport from the Zig branch. --- ncdu.1 | 33 +++++++++++++++++++++++++++------ src/browser.c | 27 +++++++++++++++++++++++---- src/global.h | 1 + src/main.c | 9 ++++++++- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/ncdu.1 b/ncdu.1 index 84d41b5..1fa50cf 100644 --- a/ncdu.1 +++ b/ncdu.1 @@ -6,6 +6,7 @@ .Sh NAME .Nm ncdu .Nd NCurses Disk Usage +. .Sh SYNOPSIS .Nm .Op Fl f Ar file @@ -32,6 +33,7 @@ .Op Fl \-show\-mtime , \-hide\-mtime .Op Fl \-show\-graph , \-hide\-graph .Op Fl \-show\-percent , \-hide\-percent +.Op Fl \-graph\-style Ar hash | half\-block | eighth\-block .Op Fl \-sort Ar column .Op Fl \-enable\-natsort , \-disable\-natsort .Op Fl \-group\-directories\-first , \-no\-group\-directories\-first @@ -43,11 +45,13 @@ .Op Fl h , \-help .Nm .Op Fl v , V , \-version +. .Sh DESCRIPTION .Nm (NCurses Disk Usage) is an interactive curses-based version of the well-known .Xr du 1 , and provides a fast way to see what directories are using your disk space. +. .Sh OPTIONS .Ss Mode Selection .Bl -tag -width Ds @@ -104,6 +108,7 @@ using 'm' and 'M', respectively. .It Fl \-ignore\-config Do not attempt to load any configuration files. .El +. .Ss Scan Options These options affect the scanning progress, they have no effect when importing directory information from a file. @@ -160,9 +165,8 @@ When exporting the data with ncurses will not be initialized at all. This option is the default when exporting to standard output. .It Fl 1 -Similar to -.Fl 0 , -but does give feedback on the scanning progress with a single line of output. +Write progress information to the terminal, but don't open a full-screen +ncurses interface. This option is the default when exporting to a file. .Pp In some cases, the ncurses browser interface which you'll see after the @@ -233,6 +237,17 @@ Can also be toggled in the file browser with the 'g' key. .It Fl \-show\-percent , \-hide\-percent Show (default) or hide the relative size percent column. Can also be toggled in the file browser with the 'g' key. +.It Fl \-graph\-style Ar hash | half\-block | eighth\-block +Change the way that the relative size bar column is drawn. +Recognized values are +.Ar hash +to draw ASCII '#' characters (default and most portable), +.Ar half\-block +to use half-block drawing characters or +.Ar eighth\-block +to use eighth-block drawing characters. +Eighth-block characters are the most precise but may not render correctly in +all terminals. .It Fl \-sort Ar column Change the default column to sort on. Accepted values are @@ -278,6 +293,7 @@ color scheme that also works in terminals with a light background. The default is .Ar off . .El +. .Sh CONFIGURATION .Nm can be configured by placing command-line options in @@ -306,6 +322,7 @@ Example configuration file: # Exclude .git directories \-\-exclude .git .Ed +. .Sh KEYS .Bl -tag -width Ds .It ? @@ -387,6 +404,7 @@ itself does not (currently) warn about or prevent this situation. .It q Quit .El +. .Sh FILE FLAGS Entries in the browser interface may be prefixed by a one\-character flag. These flags have the following meaning: @@ -410,11 +428,11 @@ Same file was already counted (hard link). .It e Empty directory. .El +. .Sh EXAMPLES To scan and browse the directory you're currently in, all you need is a simple: .Dl ncdu -If you want to scan a full filesystem, for example your root filesystem, then -you'll want to use +To scan a full filesystem, for example your root filesystem, you'll want to use .Fl x : .Dl ncdu \-x / .Pp @@ -429,7 +447,7 @@ To export from a cron job, make sure to replace .Fl 1 with .Fl 0 -to suppress any unnecessary output. +to suppress unnecessary progress output. .Pp You can also export a directory and browse it once scanning is done: .Dl ncdu \-o\- | tee export.file | ./ncdu \-f\- @@ -449,6 +467,7 @@ the local system without any network latency, and .Nm does not keep the entire directory structure in memory when exporting, so this won't consume much memory on the remote system. +. .Sh SEE ALSO .Xr du 1 , .Xr tree 1 . @@ -456,9 +475,11 @@ won't consume much memory on the remote system. .Nm has a website: .Lk https://dev.yorhel.nl/ncdu +. .Sh AUTHORS Written by .An Yorhel Aq Mt projects@yorhel.nl +. .Sh BUGS Directory hard links are not supported. They are not detected as being hard links, and will thus get scanned and diff --git a/src/browser.c b/src/browser.c index 26a27fe..7f05d12 100644 --- a/src/browser.c +++ b/src/browser.c @@ -136,9 +136,16 @@ static void browse_draw_flag(struct dir *n, int *x) { } +const char *graph_styles[3][9] = { + { " ", " ", " ", " ", " ", " ", " ", " ", "#" }, + { " ", " ", " ", " ", "▌", "▌", "▌", "▌", "█" }, + { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" }, +}; + static void browse_draw_graph(struct dir *n, int *x) { float pc = 0.0f; - int o, i, bar_size = wincols/7 > 10 ? wincols/7 : 10; + int64_t max, num, perblock, frac; + int i, bar_size = wincols/7 > 10 ? wincols/7 : 10; enum ui_coltype c = n->flags & FF_BSEL ? UIC_SEL : UIC_DEFAULT; if(!graph) @@ -166,9 +173,21 @@ static void browse_draw_graph(struct dir *n, int *x) { /* graph (10+ columns) */ if(graph == 1 || graph == 3) { uic_set(c == UIC_SEL ? UIC_GRAPH_SEL : UIC_GRAPH); - o = (int)((float)bar_size*((float)(show_as ? n->asize : n->size) / (float)(show_as ? dirlist_maxa : dirlist_maxs))); - for(i=0; iasize : n->size; + if (max < bar_size) { + max *= bar_size; + num *= bar_size; + } else if (max > ((int64_t)1)<<56) { /* Prevent overflow in calculation below */ + max <<= 5; + num <<= 5; + } + perblock = max / bar_size; + for(i=0; i 8 ? 8 : frac < 0 ? 0 : frac]); + num -= perblock; + } } addchc(c, ']'); diff --git a/src/global.h b/src/global.h index f7e81f5..fe2d120 100644 --- a/src/global.h +++ b/src/global.h @@ -124,6 +124,7 @@ extern int si; extern int show_as; /* graph display setting */ extern int graph; +extern int graph_style; /* column visibility */ extern int show_items; extern int show_mtime; diff --git a/src/main.c b/src/main.c index b6bad44..9785b12 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,7 @@ int confirm_quit = 0; int si = 0; int show_as = 0; int graph = 1; +int graph_style = 0; int show_items = 0; int show_mtime = 0; @@ -221,7 +222,13 @@ static int arg_option(int infile) { else if(OPT("--no-group-directories-first")) dirlist_sort_df = 0; else if(OPT("--enable-natsort")) dirlist_natsort = 1; else if(OPT("--disable-natsort")) dirlist_natsort = 0; - else if(OPT("--sort")) { + else if(OPT("--graph-style")) { + arg = ARG; + if (strcmp(arg, "hash") == 0) graph_style = 0; + else if (strcmp(arg, "half-block") == 0) graph_style = 1; + else if (strcmp(arg, "eighth-block") == 0 || strcmp(arg, "eigth-block") == 0) graph_style = 2; + else die("Unknown --graph-style option: %s.\n", arg); + } else if(OPT("--sort")) { arg = ARG; tmp = strrchr(arg, '-'); if(tmp && (strcmp(tmp, "-asc") == 0 || strcmp(tmp, "-desc") == 0)) *tmp = 0;