/
mod.rs
227 lines (180 loc) · 6.73 KB
/
mod.rs
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
use crate::error::prelude::*;
use clap::{Args, 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;
/// 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,
/// Show extended metadata and attributes
#[cfg(unix)]
#[arg(short, long, group = "ls-long")]
pub long: bool,
/// Show file's groups
#[cfg(unix)]
#[arg(long, requires = "ls-long")]
pub group: bool,
/// Show each file's ino
#[cfg(unix)]
#[arg(long, requires = "ls-long")]
pub ino: bool,
/// Show the total number of hardlinks to the underlying inode
#[cfg(unix)]
#[arg(long, requires = "ls-long")]
pub nlink: bool,
/// Show permissions in numeric octal format instead of symbolic
#[cfg(unix)]
#[arg(long, requires = "ls-long")]
pub octal: bool,
/// Which kind of timestamp to use
#[cfg(unix)]
#[arg(long, value_enum, requires = "ls-long", default_value_t)]
pub time: args::TimeStamp,
/// Which format to use for the timestamp; default by default
#[cfg(unix)]
#[arg(
long = "time-format",
requires = "ls-long",
value_enum,
default_value_t
)]
pub time_format: args::TimeFormat,
/// 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,
/// 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>,
#[command(flatten)]
pub globbing: Globbing,
/// 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(multiple = false)]
pub struct Globbing {
/// 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,
}
impl Context {
pub fn init() -> Result<Self> {
let mut clargs = Self::parse();
if clargs.dir.is_none() {
let current_dir = Self::get_current_dir()?;
clargs.dir = Some(current_dir);
}
Ok(clargs)
}
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)
}
}