dysk_cli/
table.rs

1use {
2    crate::{
3        Args,
4        col::Col,
5    },
6    lfs_core::*,
7    std::io::Write,
8    termimad::{
9        CompoundStyle,
10        MadSkin,
11        ProgressBar,
12        crossterm::style::Color::*,
13        minimad::{
14            self,
15            OwningTemplateExpander,
16            TableBuilder,
17        },
18    },
19};
20
21// those colors are chosen to be "redish" for used, "greenish" for available
22// and, most importantly, to work on both white and black backgrounds. If you
23// find a better combination, please show me.
24static USED_COLOR: u8 = 209;
25static AVAI_COLOR: u8 = 65;
26static SIZE_COLOR: u8 = 172;
27
28static BAR_WIDTH: usize = 5;
29static INODES_BAR_WIDTH: usize = 5;
30
31pub fn write<W: Write>(
32    w: &mut W,
33    mounts: &[&Mount],
34    color: bool,
35    args: &Args,
36) -> std::io::Result<()> {
37    if args.cols.is_empty() {
38        return Ok(());
39    }
40    let units = args.units;
41    let mut expander = OwningTemplateExpander::new();
42    expander.set_default("");
43    for mount in mounts {
44        let sub = expander
45            .sub("rows")
46            .set(
47                "id",
48                mount
49                    .info
50                    .id
51                    .as_ref()
52                    .map_or("".to_string(), |i| i.to_string()),
53            )
54            .set("dev", mount.info.dev)
55            .set("filesystem", &mount.info.fs)
56            .set("disk", mount.disk.as_ref().map_or("", |d| d.disk_type()))
57            .set("type", &mount.info.fs_type)
58            .set("mount-point", mount.info.mount_point.to_string_lossy())
59            .set("mount-options", mount.info.options_string())
60            .set_option("uuid", mount.uuid.as_ref())
61            .set_option("part_uuid", mount.part_uuid.as_ref())
62            .set_option(
63                "compress-level",
64                mount.info.option_value("compress"),
65            );
66        if let Some(label) = &mount.fs_label {
67            sub.set("label", label);
68        }
69        if mount.is_remote() {
70            sub.set("remote", "x");
71        }
72        if let Some(stats) = mount.stats() {
73            let use_share = stats.use_share();
74            let free_share = 1.0 - use_share;
75            sub.set("size", units.fmt(stats.size()))
76                .set("used", units.fmt(stats.used()))
77                .set("use-percents", format!("{:.0}%", 100.0 * use_share))
78                .set_md("bar", progress_bar_md(use_share, BAR_WIDTH, args.ascii))
79                .set("free", units.fmt(stats.available()))
80                .set("free-percents", format!("{:.0}%", 100.0 * free_share));
81            if let Some(inodes) = &stats.inodes {
82                let iuse_share = inodes.use_share();
83                sub.set("inodes", inodes.files)
84                    .set("iused", inodes.used())
85                    .set("iuse-percents", format!("{:.0}%", 100.0 * iuse_share))
86                    .set_md(
87                        "ibar",
88                        progress_bar_md(iuse_share, INODES_BAR_WIDTH, args.ascii),
89                    )
90                    .set("ifree", inodes.favail);
91            }
92        } else if mount.is_unreachable() {
93            sub.set("use-error", "unreachable");
94        }
95    }
96    let mut skin = if color {
97        make_colored_skin()
98    } else {
99        MadSkin::no_style()
100    };
101    if args.ascii {
102        skin.limit_to_ascii();
103    }
104
105    let mut tbl = TableBuilder::default();
106    for col in args.cols.cols() {
107        tbl.col(
108            minimad::Col::new(
109                col.title(),
110                match col {
111                    Col::Id => "${id}",
112                    Col::Dev => "${dev}",
113                    Col::Filesystem => "${filesystem}",
114                    Col::Label => "${label}",
115                    Col::Disk => "${disk}",
116                    Col::Type => "${type}",
117                    Col::Remote => "${remote}",
118                    Col::Used => "~~${used}~~",
119                    Col::Use => "~~${use-percents}~~ ${bar}~~${use-error}~~",
120                    Col::UsePercent => "~~${use-percents}~~",
121                    Col::Free => "*${free}*",
122                    Col::FreePercent => "*${free-percents}*",
123                    Col::Size => "**${size}**",
124                    Col::InodesFree => "*${ifree}*",
125                    Col::InodesUsed => "~~${iused}~~",
126                    Col::InodesUse => "~~${iuse-percents}~~ ${ibar}",
127                    Col::InodesUsePercent => "~~${iuse-percents}~~",
128                    Col::InodesCount => "**${inodes}**",
129                    Col::MountPoint => "${mount-point}",
130                    Col::Uuid => "${uuid}",
131                    Col::PartUuid => "${part_uuid}",
132                    Col::MountOptions => "${mount-options}",
133                    Col::CompressLevel => "${compress-level}",
134                },
135            )
136            .align_content(col.content_align())
137            .align_header(col.header_align()),
138        );
139    }
140
141    skin.write_owning_expander_md(w, &expander, &tbl)
142}
143
144fn make_colored_skin() -> MadSkin {
145    MadSkin {
146        bold: CompoundStyle::with_fg(AnsiValue(SIZE_COLOR)), // size
147        inline_code: CompoundStyle::with_fgbg(AnsiValue(USED_COLOR), AnsiValue(AVAI_COLOR)), // use bar
148        strikeout: CompoundStyle::with_fg(AnsiValue(USED_COLOR)),                            // use%
149        italic: CompoundStyle::with_fg(AnsiValue(AVAI_COLOR)), // available
150        ..Default::default()
151    }
152}
153
154fn progress_bar_md(
155    share: f64,
156    bar_width: usize,
157    ascii: bool,
158) -> String {
159    if ascii {
160        let count = (share * bar_width as f64).round() as usize;
161        let bar: String = "".repeat(count);
162        let no_bar: String = "-".repeat(bar_width - count);
163        format!("~~{}~~*{}*", bar, no_bar)
164    } else {
165        let pb = ProgressBar::new(share as f32, bar_width);
166        format!("`{:<width$}`", pb, width = bar_width)
167    }
168}