Skip to main content

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().remote_stats(args.remote_stats.unwrap_or_else(|| true));
56    if let Some(timeout) = args.timeout {
57        options = options.stats_timeout(timeout.as_duration());
58    }
59    if let Some(strategy) = &args.strategy {
60        match strategy.parse() {
61            Ok(strategy) => {
62                options = options.strategy(strategy);
63            }
64            Err(_) => {
65                eprintln!("Ignoring unrecognized strategy");
66            }
67        }
68    }
69    let mut mounts = match lfs_core::read_mounts(&options) {
70        Ok(mounts) => mounts,
71        Err(e) => {
72            eprintln!("Error reading mounts: {}", e);
73            return Ok(());
74        }
75    };
76    if !args.all {
77        mounts.retain(is_normal);
78    }
79    if let Some(path) = &args.path {
80        let dev = match lfs_core::DeviceId::of_path(path) {
81            Ok(dev) => dev,
82            Err(e) => {
83                eprintln!("Error getting device of path {}: {}", path.display(), e);
84                return Ok(());
85            }
86        };
87        mounts.retain(|m| m.info.dev == dev);
88    }
89    args.sort.sort(&mut mounts);
90    let mounts = match args.filter.clone().unwrap_or_default().filter(&mounts) {
91        Ok(mounts) => mounts,
92        Err(e) => {
93            eprintln!("Error in filter evaluation: {}", e);
94            return Ok(());
95        }
96    };
97    if args.csv {
98        return csv::write(&mut w, &mounts, &args);
99    }
100    if args.json {
101        return writeln!(
102            &mut w,
103            "{}",
104            serde_json::to_string_pretty(&json::output_value(&mounts, args.units)).unwrap()
105        );
106    }
107    if mounts.is_empty() {
108        return writeln!(&mut w, "no mount to display - try\n    dysk -a");
109    }
110    table::write(&mut w, &mounts, args.color(), &args)?;
111    if args.color() {
112        csi_reset();
113    }
114    Ok(())
115}
116
117/// output a Reset CSI sequence
118fn csi_reset() {
119    print!("\u{1b}[0m");
120}