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