summaryrefslogtreecommitdiffstats
path: root/src/user/mod.rs
blob: c6ae071796912571fd39c09935ccd13fb472a736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use crate::error::prelude::*;
use ahash::HashMap;
use clap::{parser::ValueSource, ArgMatches, Args, CommandFactory, FromArgMatches, Parser};
use std::{env, fs, path::PathBuf};

/// Enum definitions for enumerated command-line arguments.
pub mod args;

/// Concerned with properties of columns in the output which is essentially a 2D grid.
pub mod column;

/// Concerned with loading and parsing the optional `erdtree.toml` config file.
mod config;

#[cfg(test)]
mod test;

/// Defines the CLI whose purpose is to capture user arguments and reconcile them with arguments
/// found with a config file if relevant.
#[derive(Parser, Debug)]
#[command(name = "erdtree")]
#[command(author = "Benjamin Nguyen. <benjamin.van.nguyen@gmail.com>")]
#[command(version = "4.0.0")]
#[command(
    about = "erdtree (erd) is a cross-platform, multi-threaded, and general purpose filesystem and disk usage utility.",
    long_about = None,
)]
pub struct Context {
    /// Directory to traverse; defaults to current working directory
    dir: Option<PathBuf>,

    /// Run the program ignoring hidden files
    #[arg(short = '.', long)]
    pub no_hidden: bool,

    /// Run the program skipping the .git directory
    #[arg(long)]
    pub no_git: bool,

    /// Report byte size in either binary or SI units
    #[arg(short, long, value_enum, default_value_t)]
    pub byte_units: args::BytePresentation,

    /// Use configuration of a named table rather than the top-level table in .erdtree.toml
    #[arg(short = 'c', long)]
    pub config: Option<String>,

    /// Sort directories before or after all other file types
    #[arg(short, long, value_enum, default_value_t)]
    pub dir_order: args::DirOrder,

    /// Filter for specified file types
    #[arg(short = 'F', long, value_enum)]
    pub file_type: Vec<args::FileType>,

    /// Follow symlinks
    #[arg(short = 'f', long)]
    pub follow: bool,

    /// Run the program ignoring files that match rules in all .gitignore files encountered during traversal
    #[arg(short = 'i', long)]
    pub gitignore: bool,

    /// Display file icons
    #[arg(short = 'I', long)]
    pub icons: bool,

    /// Ignore files that match rules in the global .gitignore file
    #[arg(long)]
    pub global_gitignore: bool,

    #[cfg(unix)]
    #[command(flatten)]
    pub long: Long,

    /// Maximum depth to display
    #[arg(short = 'L', long, value_name = "NUM")]
    pub level: Option<usize>,

    /// Metric used when reporting disk usage
    #[arg(short, long, value_enum, default_value_t)]
    pub metric: args::Metric,

    /// Run the program without reading .erdtree.toml
    #[arg(short, long)]
    pub no_config: bool,

    #[command(flatten)]
    pub search: Search,

    /// Omit empty directories from the output
    #[arg(short = 'P', long)]
    pub prune: bool,

    /// Field whereby to sort entries
    #[arg(short, long, value_enum, default_value_t)]
    pub sort: args::Sort,

    /// Sort entries relative either to their siblings or all other entries
    #[arg(long, value_enum, default_value_t)]
    pub sort_type: args::SortType,

    /// Don't compute disk-usage and omit file size from output
    #[arg(short = 'S', long)]
    pub suppress_size: bool,

    /// Which kind of layout to use when rendering the output
    #[arg(short = 'y', long, value_enum, default_value_t)]
    pub layout: args::Layout,

    /// Number of threads to use for disk reads
    #[arg(short = 'T', long, default_value_t = Context::default_num_threads())]
    pub threads: usize,

    /// Prevent traversal into directories that are on different filesystems
    #[arg(short = 'x', long = "one-file-system")]
    pub same_fs: bool,

    /// Prints logs at the end of the output
    #[arg(short = 'v', long = "verbose")]
    pub verbose: bool,

    #[arg(long)]
    /// Print completions for a given shell to stdout
    pub completions: Option<clap_complete::Shell>,

    //////////////////////////
    /* INTERNAL USAGE BELOW */
    //////////////////////////
    #[clap(skip = column::Metadata::default())]
    pub column_metadata: column::Metadata,
}

#[derive(Args, Debug)]
#[group(required = false, multiple = false)]
pub struct Search {
    /// Regular expression (or glob if '--glob' or '--iglob' is used) used to match files by their
    /// relative path
    #[arg(short, long, group = "searching")]
    pub pattern: Option<String>,

    /// Enables glob based searching instead of regular expressions
    #[arg(long, requires = "searching")]
    pub glob: bool,

    /// Enables case-insensitive glob based searching instead of regular expressions
    #[arg(long, requires = "searching")]
    pub iglob: bool,
}

#[cfg(unix)]
#[derive(Args, Debug)]
#[group(required = false, multiple = true)]
pub struct Long {
    /// Show extended metadata and attributes
    #[arg(short, long, group = "ls-long")]
    pub long: bool,

    /// Show file's groups
    #[arg(long, requires = "ls-long")]
    pub group: bool,

    /// Show each file's ino
    #[arg(long, requires = "ls-long")]
    pub ino: bool,

    /// Show the total number of hardlinks to the underlying inode
    #[arg(long, requires = "ls-long")]
    pub nlink: bool,

    /// Show permissions in numeric octal format instead of symbolic
    #[arg(long, requires = "ls-long")]
    pub octal: bool,

    /// Which kind of timestamp to use
    #[arg(long, value_enum, requires = "ls-long")]
    pub time: Option<args::TimeStamp>,

    /// Which format to use for the timestamp; default by default
    #[arg(long = "time-format", requires = "ls-long", value_enum)]
    pub time_format: Option<args::TimeFormat>,
}

impl Context {
    pub fn init() -> Result<Self> {
        let clargs = Self::command().get_matches();
        let user_config = Self::load_config(&clargs)?;

        let mut ctx = if let Some(ref config) = user_config {
            let reconciled_args = Self::reconcile_args(&clargs, config);
            Self::try_parse_from(reconciled_args).into_report(ErrorCategory::User)?
        } else {
            Self::from_arg_matches(&clargs).into_report(ErrorCategory::User)?
        };

        if ctx.dir.is_none() {
            let current_dir = Self::get_current_dir()?;
            ctx.dir = Some(current_dir);
        }

        Ok(ctx)
    }

    pub fn dir(&self) -> Option<&PathBuf> {
        self.dir.as_ref()
    }

    pub fn dir_canonical(&self) -> Result<PathBuf> {
        match self.dir() {
            Some(root) => fs::canonicalize(root).into_report(ErrorCategory::Internal),
            None => Self::get_current_dir(),
        }
    }

    pub fn get_current_dir() -> Result<PathBuf> {
        env::current_dir()
            .and_then(fs::canonicalize)
            .into_report(ErrorCategory::System)
            .context("Failed to access current working directory")
            .set_help("Ensure current directory exists and sufficient permissions are granted")
    }

    /// The max depth to print. Note that all directories are fully traversed to compute file
    /// sizes; this just determines how much to print.
    pub fn level(&self) -> usize {
        self.level.unwrap_or(usize::MAX)
    }

    pub fn update_column_metadata(&mut self, new_metadata: column::Metadata) {
        self.column_metadata = new_metadata;
    }

    fn default_num_threads() -> usize {
        std::thread::available_parallelism().map_or(3, usize::from)
    }

    fn load_config(clargs: &ArgMatches) -> Result<Option<ArgMatches>> {
        let cmd = Self::from_arg_matches(clargs).into_report(ErrorCategory::User)?;

        if cmd.no_config {
            return Ok(None);
        }

        let Some(raw_config) = config::toml::load() else {
            return Ok(None);
        };

        match config::parse::args(raw_config, cmd.config.as_deref()) {
            Ok(config) => Ok(config),
            Err(err) => match err {
                config::parse::Error::TableNotFound(_) => Err(err).into_report(ErrorCategory::User),
                _ => Err(err).into_report(ErrorCategory::Internal),
            },
        }
    }

    /// Reconcile args between command-line and user config.
    fn reconcile_args(clargs: &ArgMatches, config: &ArgMatches) -> Vec<String> {
        let mut arg_id_map = HashMap::<clap::Id, clap::Arg>::default();

        for arg_def in Self::command().get_arguments() {
            if arg_def.is_positional() {
                continue;
            }
            arg_id_map.insert(arg_def.get_id().clone(), arg_def.clone());
        }

        let mut args = vec![crate::BIN_NAME.to_string()];

        let mut push_args = |arg_name: String, arg_id: &str, src: &ArgMatches| {
            if let Ok(Some(mut bool_args)) = src.try_get_many::<bool>(arg_id) {
                if bool_args.all(|arg| *arg) {
                    args.push(arg_name);
                }
                return;
            }

            let vals = src
                .get_raw_occurrences(arg_id)
                .unwrap()
                .flat_map(|i| {
                    i.map(|o| o.to_string_lossy().into_owned())
                        .collect::<Vec<_>>()
                })
                .collect::<Vec<_>>();

            args.push(arg_name);
            args.extend_from_slice(&vals);
        };

        for arg_id in arg_id_map.keys() {
            let arg_id_str = arg_id.as_str();

            let Some(arg_def) = arg_id_map.get(arg_id) else {
                continue;
            };

            let arg_name = arg_def.get_long().map_or_else(
                || arg_def.get_short().map(|c| format!("-{c}")).unwrap(),
                |long| format!("--{long}"),
            );

            let confarg_vs = config.value_source(arg_id_str);
            let clarg_vs = clargs.value_source(arg_id_str);

            match (clarg_vs, confarg_vs) {
                (None, None) => continue,
                (Some(_), None) => push_args(arg_name, arg_id_str, clargs),
                (None, Some(