Add --graph-style + use integer calculations

Backport from the Zig branch.
This commit is contained in:
Yorhel 2025-02-28 20:11:19 +01:00
parent b853185413
commit d6a129ff5a
4 changed files with 59 additions and 11 deletions

33
ncdu.1
View file

@ -6,6 +6,7 @@
.Sh NAME .Sh NAME
.Nm ncdu .Nm ncdu
.Nd NCurses Disk Usage .Nd NCurses Disk Usage
.
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl f Ar file .Op Fl f Ar file
@ -32,6 +33,7 @@
.Op Fl \-show\-mtime , \-hide\-mtime .Op Fl \-show\-mtime , \-hide\-mtime
.Op Fl \-show\-graph , \-hide\-graph .Op Fl \-show\-graph , \-hide\-graph
.Op Fl \-show\-percent , \-hide\-percent .Op Fl \-show\-percent , \-hide\-percent
.Op Fl \-graph\-style Ar hash | half\-block | eighth\-block
.Op Fl \-sort Ar column .Op Fl \-sort Ar column
.Op Fl \-enable\-natsort , \-disable\-natsort .Op Fl \-enable\-natsort , \-disable\-natsort
.Op Fl \-group\-directories\-first , \-no\-group\-directories\-first .Op Fl \-group\-directories\-first , \-no\-group\-directories\-first
@ -43,11 +45,13 @@
.Op Fl h , \-help .Op Fl h , \-help
.Nm .Nm
.Op Fl v , V , \-version .Op Fl v , V , \-version
.
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
(NCurses Disk Usage) is an interactive curses-based version of the well-known (NCurses Disk Usage) is an interactive curses-based version of the well-known
.Xr du 1 , .Xr du 1 ,
and provides a fast way to see what directories are using your disk space. and provides a fast way to see what directories are using your disk space.
.
.Sh OPTIONS .Sh OPTIONS
.Ss Mode Selection .Ss Mode Selection
.Bl -tag -width Ds .Bl -tag -width Ds
@ -104,6 +108,7 @@ using 'm' and 'M', respectively.
.It Fl \-ignore\-config .It Fl \-ignore\-config
Do not attempt to load any configuration files. Do not attempt to load any configuration files.
.El .El
.
.Ss Scan Options .Ss Scan Options
These options affect the scanning progress, they have no effect when importing These options affect the scanning progress, they have no effect when importing
directory information from a file. directory information from a file.
@ -160,9 +165,8 @@ When exporting the data with
ncurses will not be initialized at all. ncurses will not be initialized at all.
This option is the default when exporting to standard output. This option is the default when exporting to standard output.
.It Fl 1 .It Fl 1
Similar to Write progress information to the terminal, but don't open a full-screen
.Fl 0 , ncurses interface.
but does give feedback on the scanning progress with a single line of output.
This option is the default when exporting to a file. This option is the default when exporting to a file.
.Pp .Pp
In some cases, the ncurses browser interface which you'll see after the 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 .It Fl \-show\-percent , \-hide\-percent
Show (default) or hide the relative size percent column. Show (default) or hide the relative size percent column.
Can also be toggled in the file browser with the 'g' key. 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 .It Fl \-sort Ar column
Change the default column to sort on. Change the default column to sort on.
Accepted values are Accepted values are
@ -278,6 +293,7 @@ color scheme that also works in terminals with a light background.
The default is The default is
.Ar off . .Ar off .
.El .El
.
.Sh CONFIGURATION .Sh CONFIGURATION
.Nm .Nm
can be configured by placing command-line options in can be configured by placing command-line options in
@ -306,6 +322,7 @@ Example configuration file:
# Exclude .git directories # Exclude .git directories
\-\-exclude .git \-\-exclude .git
.Ed .Ed
.
.Sh KEYS .Sh KEYS
.Bl -tag -width Ds .Bl -tag -width Ds
.It ? .It ?
@ -387,6 +404,7 @@ itself does not (currently) warn about or prevent this situation.
.It q .It q
Quit Quit
.El .El
.
.Sh FILE FLAGS .Sh FILE FLAGS
Entries in the browser interface may be prefixed by a one\-character flag. Entries in the browser interface may be prefixed by a one\-character flag.
These flags have the following meaning: These flags have the following meaning:
@ -410,11 +428,11 @@ Same file was already counted (hard link).
.It e .It e
Empty directory. Empty directory.
.El .El
.
.Sh EXAMPLES .Sh EXAMPLES
To scan and browse the directory you're currently in, all you need is a simple: To scan and browse the directory you're currently in, all you need is a simple:
.Dl ncdu .Dl ncdu
If you want to scan a full filesystem, for example your root filesystem, then To scan a full filesystem, for example your root filesystem, you'll want to use
you'll want to use
.Fl x : .Fl x :
.Dl ncdu \-x / .Dl ncdu \-x /
.Pp .Pp
@ -429,7 +447,7 @@ To export from a cron job, make sure to replace
.Fl 1 .Fl 1
with with
.Fl 0 .Fl 0
to suppress any unnecessary output. to suppress unnecessary progress output.
.Pp .Pp
You can also export a directory and browse it once scanning is done: You can also export a directory and browse it once scanning is done:
.Dl ncdu \-o\- | tee export.file | ./ncdu \-f\- .Dl ncdu \-o\- | tee export.file | ./ncdu \-f\-
@ -449,6 +467,7 @@ the local system without any network latency, and
.Nm .Nm
does not keep the entire directory structure in memory when exporting, so this does not keep the entire directory structure in memory when exporting, so this
won't consume much memory on the remote system. won't consume much memory on the remote system.
.
.Sh SEE ALSO .Sh SEE ALSO
.Xr du 1 , .Xr du 1 ,
.Xr tree 1 . .Xr tree 1 .
@ -456,9 +475,11 @@ won't consume much memory on the remote system.
.Nm .Nm
has a website: has a website:
.Lk https://dev.yorhel.nl/ncdu .Lk https://dev.yorhel.nl/ncdu
.
.Sh AUTHORS .Sh AUTHORS
Written by Written by
.An Yorhel Aq Mt projects@yorhel.nl .An Yorhel Aq Mt projects@yorhel.nl
.
.Sh BUGS .Sh BUGS
Directory hard links are not supported. Directory hard links are not supported.
They are not detected as being hard links, and will thus get scanned and They are not detected as being hard links, and will thus get scanned and

View file

@ -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) { static void browse_draw_graph(struct dir *n, int *x) {
float pc = 0.0f; 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; enum ui_coltype c = n->flags & FF_BSEL ? UIC_SEL : UIC_DEFAULT;
if(!graph) if(!graph)
@ -166,9 +173,21 @@ static void browse_draw_graph(struct dir *n, int *x) {
/* graph (10+ columns) */ /* graph (10+ columns) */
if(graph == 1 || graph == 3) { if(graph == 1 || graph == 3) {
uic_set(c == UIC_SEL ? UIC_GRAPH_SEL : UIC_GRAPH); 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))); max = show_as ? dirlist_maxa : dirlist_maxs;
for(i=0; i<bar_size; i++) num = show_as ? n->asize : n->size;
addch(i < o ? '#' : ' '); 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<bar_size; i++) {
frac = (num * 8) / (perblock < 1 ? 1 : perblock);
addstr(graph_styles[graph_style][frac > 8 ? 8 : frac < 0 ? 0 : frac]);
num -= perblock;
}
} }
addchc(c, ']'); addchc(c, ']');

View file

@ -124,6 +124,7 @@ extern int si;
extern int show_as; extern int show_as;
/* graph display setting */ /* graph display setting */
extern int graph; extern int graph;
extern int graph_style;
/* column visibility */ /* column visibility */
extern int show_items; extern int show_items;
extern int show_mtime; extern int show_mtime;

View file

@ -47,6 +47,7 @@ int confirm_quit = 0;
int si = 0; int si = 0;
int show_as = 0; int show_as = 0;
int graph = 1; int graph = 1;
int graph_style = 0;
int show_items = 0; int show_items = 0;
int show_mtime = 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("--no-group-directories-first")) dirlist_sort_df = 0;
else if(OPT("--enable-natsort")) dirlist_natsort = 1; else if(OPT("--enable-natsort")) dirlist_natsort = 1;
else if(OPT("--disable-natsort")) dirlist_natsort = 0; 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; arg = ARG;
tmp = strrchr(arg, '-'); tmp = strrchr(arg, '-');
if(tmp && (strcmp(tmp, "-asc") == 0 || strcmp(tmp, "-desc") == 0)) *tmp = 0; if(tmp && (strcmp(tmp, "-asc") == 0 || strcmp(tmp, "-desc") == 0)) *tmp = 0;