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