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 units;
15
16use {
17    crate::{
18        args::*,
19        normal::*,
20    },
21    clap::Parser,
22};
23
24#[allow(clippy::match_like_matches_macro)]
25pub fn run() {
26    let args = Args::parse();
27    if args.version {
28        println!("dysk {}", env!("CARGO_PKG_VERSION"));
29        return;
30    }
31    if args.help {
32        help::print(args.ascii);
33        csi_reset();
34        return;
35    }
36    if args.list_cols {
37        list_cols::print(args.color(), args.ascii);
38        csi_reset();
39        return;
40    }
41    let mut options =
42        lfs_core::ReadOptions::default()
43        .remote_stats(args.remote_stats.unwrap_or_else(|| true));
44    if let Some(strategy) = &args.strategy {
45        match strategy.parse() {
46            Ok(strategy) => {
47                options = options.strategy(strategy);
48            }
49            Err(_) => {
50                eprintln!("Ignoring unrecognized strategy");
51            }
52        }
53    }
54    let mut mounts = match lfs_core::read_mounts(&options) {
55        Ok(mounts) => mounts,
56        Err(e) => {
57            eprintln!("Error reading mounts: {}", e);
58            return;
59        }
60    };
61    if !args.all {
62        mounts.retain(is_normal);
63    }
64    if let Some(path) = &args.path {
65        let dev = match lfs_core::DeviceId::of_path(path) {
66            Ok(dev) => dev,
67            Err(e) => {
68                eprintln!("Error getting device of path {}: {}", path.display(), e);
69                return;
70            }
71        };
72        mounts.retain(|m| m.info.dev == dev);
73    }
74    args.sort.sort(&mut mounts);
75    let mounts = match args.filter.clone().unwrap_or_default().filter(&mounts) {
76        Ok(mounts) => mounts,
77        Err(e) => {
78            eprintln!("Error in filter evaluation: {}", e);
79            return;
80        }
81    };
82    if args.csv {
83        csv::print(&mounts, &args).expect("writing csv failed");
84        return;
85    }
86    if args.json {
87        println!(
88            "{}",
89            serde_json::to_string_pretty(&json::output_value(&mounts, args.units)).unwrap()
90        );
91        return;
92    }
93    if mounts.is_empty() {
94        println!("no mount to display - try\n    dysk -a");
95        return;
96    }
97    table::print(&mounts, args.color(), &args);
98    csi_reset();
99}
100
101/// output a Reset CSI sequence
102fn csi_reset() {
103    print!("\u{1b}[0m");
104}