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