summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2022-02-03 16:07:37 +0100
committerYorhel <git@yorhel.nl>2022-02-03 16:10:18 +0100
commit7d2905952d956801050baaed08eb092fb22f661f (patch)
tree7878139c612cf71004d3e0f1dcf4dc368a75cef5
parentedf48f6f11b1a8ea773cb91ebda92fd006a4b1e7 (diff)
Add --graph-style option and Unicode graph drawing
And also adjust the graph width calculation to do a better job when the largest item is smaller than the number of columns used for the graph, which would previously draw either nothing (if size = 0) or a full bar (if size > 0). Fixes #172.
-rw-r--r--ncdu.pod8
-rw-r--r--src/browser.zig19
-rw-r--r--src/main.zig9
3 files changed, 30 insertions, 6 deletions
diff --git a/ncdu.pod b/ncdu.pod
index bd34231..fe81d9e 100644
--- a/ncdu.pod
+++ b/ncdu.pod
@@ -227,6 +227,14 @@ browser with the 'g' key.
Show (default) or hide the relative size percent column. Can also be toggled in
the browser with the 'g' key.
+=item B<--graph-style> I<OPTION>
+
+Change the way that the relative size bar column is drawn. Recognized values
+are I<hash> to draw ASCII C<#> characters (most portable), I<half-block> to use
+half-block drawing characters (the default) or I<eigth-block> to use
+eigth-block drawing characters. Eigth-block characters are the most precise but
+may not render correctly in all terminals.
+
=item B<--shared-column> I<OPTION>
Set to I<off> to disable the shared size column for directories, I<shared>
diff --git a/src/browser.zig b/src/browser.zig
index 44fda3f..d3526ec 100644
--- a/src/browser.zig
+++ b/src/browser.zig
@@ -224,14 +224,23 @@ const Row = struct {
}
if (main.config.show_graph and main.config.show_percent) ui.addch(' ');
if (main.config.show_graph) {
- const perblock = std.math.divFloor(u64, if (main.config.show_blocks) dir_max_blocks else dir_max_size, bar_size) catch unreachable;
- const num = if (main.config.show_blocks) item.blocks else item.size;
+ var max = if (main.config.show_blocks) dir_max_blocks else dir_max_size;
+ var num = if (main.config.show_blocks) item.blocks else item.size;
+ if (max < bar_size) {
+ max *= bar_size;
+ num *= bar_size;
+ }
+ const perblock = std.math.divFloor(u64, max, bar_size) catch unreachable;
var i: u32 = 0;
- var siz: u64 = 0;
self.bg.fg(.graph);
while (i < bar_size) : (i += 1) {
- siz = siz +| perblock;
- ui.addch(if (siz <= num) '#' else ' ');
+ const frac = std.math.min(8, (num *| 8) / perblock);
+ ui.addstr(switch (main.config.graph_style) {
+ .hash => ([_][:0]const u8{ " ", " ", " ", " ", " ", " ", " ", " ", "#" })[frac],
+ .half => ([_][:0]const u8{ " ", " ", " ", " ", "▌", "▌", "▌", "▌", "█" })[frac],
+ .eigth => ([_][:0]const u8{ " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" })[frac],
+ });
+ num -|= perblock;
}
}
self.bg.fg(.default);
diff --git a/src/main.zig b/src/main.zig
index b064b7e..aabb50a 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -61,6 +61,7 @@ pub const config = struct {
pub var show_mtime: bool = false;
pub var show_graph: bool = true;
pub var show_percent: bool = false;
+ pub var graph_style: enum { hash, half, eigth } = .half;
pub var sort_col: SortCol = .blocks;
pub var sort_order: SortOrder = .desc;
pub var sort_dirsfirst: bool = false;
@@ -181,7 +182,13 @@ fn argConfig(args: *Args, opt: Args.Option) bool {
else if (opt.is("--hide-percent")) config.show_percent = false
else if (opt.is("--group-directories-first")) config.sort_dirsfirst = true
else if (opt.is("--no-group-directories-first")) config.sort_dirsfirst = false
- else if (opt.is("--sort")) {
+ else if (opt.is("--graph-style")) {
+ const val = args.arg();
+ if (std.mem.eql(u8, val, "hash")) config.graph_style = .hash
+ else if (std.mem.eql(u8, val, "half-block")) config.graph_style = .half
+ else if (std.mem.eql(u8, val, "eigth-block")) config.graph_style = .eigth
+ else ui.die("Unknown --graph-style option: {s}.\n", .{val});
+ } else if (opt.is("--sort")) {
var val: []const u8 = args.arg();
var ord: ?config.SortOrder = null;
if (std.mem.endsWith(u8, val, "-asc")) {