dysk_cli/
lib.rs

1pub mod args;
2pub mod col;
3pub mod col_expr;
4pub mod cols;
5pub mod csv;
6pub mod filter;
7pub mod help;
8pub mod json;
9pub mod list_cols;
10pub mod normal;
11pub mod order;
12pub mod sorting;
13pub mod table;
14pub mod timeout;
15pub mod units;
16
17use {
18    crate::{
19        args::*,
20        normal::*,
21    },
22    clap::Parser,
23    std::io::{
24        self,
25        Write,
26    },
27};
28
29/// Print according to launch arguments
30///
31/// # Errors
32/// Returns an `io::Error` if writing to stdout fails
33#[allow(clippy::match_like_matches_macro)]
34pub fn run() -> io::Result<()> {
35    let mut w = io::stdout();
36    let args = Args::parse();
37    if args.version {
38        return writeln!(&mut w, "dysk {}", env!("CARGO_PKG_VERSION"));
39    }
40    if args.help {
41        help::print(args.ascii);
42        if args.color() {
43            csi_reset();
44        }
45        return Ok(());
46    }
47    if args.list_cols {
48        list_cols::write(&mut w, args.color(), args.ascii)?;
49        if args.color() {
50            csi_reset();
51        }
52        return Ok(());
53    }
54    let mut options =
55        lfs_core::ReadOptions::default()
56        .remote_stats(args.remote_stats.unwrap_or_else(|| true));
57    if let Some(timeout) = args.timeout {
58        options = options.stats_timeout(timeout.as_duration());
59    }
60    if let Some(strategy) = &args.strategy {
61        match strategy.parse() {
62            Ok(strategy) => {
63                options = options.strategy(strategy);
64            }
65            Err(_) => {
66                eprintln!("Ignoring unrecognized strategy");
67            }
68        }
69    }
70    let mut mounts = match lfs_core::read_mounts(&options) {
71        Ok(mounts) => mounts,
72        Err(e) => {
73            eprintln!("Error reading mounts: {}", e);
74            return Ok(());
75        }
76    };
77    if !args.all {
78        mounts.retain(is_normal);
79    }
80    if let Some(path) = &args.path {
81        let dev = match lfs_core::DeviceId::of_path(path) {
82            Ok(dev) => dev,
83            Err(e) => {
84                eprintln!("Error getting device of path {}: {}", path.display(), e);
85                return Ok(());
86            }
87        };
88        mounts.retain(|m| m.info.dev == dev);
89    }
90    args.sort.sort(&mut mounts);
91    let mounts = match args.filter.clone().unwrap_or_default().filter(&mounts) {
92        Ok(mounts) => mounts,
93        Err(e) => {
94            eprintln!("Error in filter evaluation: {}", e);
95            return Ok(());
96        }
97    };
98    if args.csv {
99        return csv::write(&mut w, &mounts, &args);
100    }
101    if args.json {
102        return writeln!(
103            &mut w,
104            "{}",
105            serde_json::to_string_pretty(&json::output_value(&mounts, args.units)).unwrap()
106        );
107    }
108    if mounts.is_empty() {
109        return writeln!(&mut w, "no mount to display - try\n    dysk -a");
110    }
111    table::write(&mut w, &mounts, args.color(), &args)?;
112    if args.color() {
113        csi_reset();
114    }
115    Ok(())
116}
117
118/// output a Reset CSI sequence
119fn csi_reset() {
120    print!("\u{1b}[0m");
121}
122