forked from lsd-rs/lsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rs
412 lines (409 loc) · 15.1 KB
/
app.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use clap::{Arg, ArgAction, Command, ValueHint};
pub fn build() -> Command<'static> {
Command::new("lsd")
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(
Arg::new("FILE")
.action(ArgAction::Append)
.multiple_values(true)
.default_value(".")
.value_hint(ValueHint::AnyPath),
)
.arg(
Arg::new("all")
.short('a')
.overrides_with("almost-all")
.long("all")
.action(ArgAction::SetTrue)
.help("Do not ignore entries starting with ."),
)
.arg(
Arg::new("almost-all")
.short('A')
.overrides_with("all")
.long("almost-all")
.action(ArgAction::SetTrue)
.help("Do not list implied . and .."),
)
.arg(
Arg::new("color")
.long("color")
.value_parser(["always", "auto", "never"])
.default_value("auto")
.action(ArgAction::Append)
.takes_value(true)
.number_of_values(1)
.help("When to use terminal colours"),
)
.arg(
Arg::new("icon")
.long("icon")
.value_parser(["always", "auto", "never"])
.default_value("auto")
.action(ArgAction::Append)
.takes_value(true)
.number_of_values(1)
.help("When to print the icons"),
)
.arg(
Arg::new("icon-theme")
.long("icon-theme")
.default_value("fancy")
.value_parser(["fancy", "unicode"])
.action(ArgAction::Append)
.takes_value(true)
.number_of_values(1)
.help("Whether to use fancy or unicode icons"),
)
.arg(
Arg::new("indicators")
.short('F')
.long("classify")
.action(ArgAction::SetTrue)
.help("Append indicator (one of */=>@|) at the end of the file names"),
)
.arg(
Arg::new("long")
.short('l')
.long("long")
.action(ArgAction::SetTrue)
.help("Display extended file metadata as a table"),
)
.arg(
Arg::new("ignore-config")
.long("ignore-config")
.action(ArgAction::SetTrue)
.help("Ignore the configuration file"),
)
.arg(
Arg::new("config-file")
.long("config-file")
.help("Provide a custom lsd configuration file")
.value_name("config-file")
.takes_value(true)
)
.arg(
Arg::new("oneline")
.short('1')
.long("oneline")
.action(ArgAction::SetTrue)
.help("Display one entry per line"),
)
.arg(
Arg::new("recursive")
.short('R')
.long("recursive")
.action(ArgAction::SetTrue)
.conflicts_with("tree")
.help("Recurse into directories"),
)
.arg(
Arg::new("human_readable")
.short('h')
.long("human-readable")
.action(ArgAction::SetTrue)
.help("For ls compatibility purposes ONLY, currently set by default"),
)
.arg(
Arg::new("tree")
.long("tree")
.action(ArgAction::SetTrue)
.conflicts_with("recursive")
.help("Recurse into directories and present the result as a tree"),
)
.arg(
Arg::new("depth")
.long("depth")
.action(ArgAction::Append)
.takes_value(true)
.value_name("num")
.help("Stop recursing into directories after reaching specified depth"),
)
.arg(
Arg::new("directory-only")
.short('d')
.long("directory-only")
.action(ArgAction::SetTrue)
.conflicts_with("depth")
.conflicts_with("recursive")
.help("Display directories themselves, and not their contents (recursively when used with --tree)"),
)
.arg(
Arg::new("permission")
.long("permission")
.default_value("rwx")
.value_parser(["rwx", "octal"])
.action(ArgAction::Append)
.takes_value(true)
.number_of_values(1)
.help("How to display permissions"),
)
.arg(
Arg::new("size")
.long("size")
.value_parser(["default", "short", "bytes"])
.default_value("default")
.action(ArgAction::Append)
.takes_value(true)
.number_of_values(1)
.help("How to display size"),
)
.arg(
Arg::new("total-size")
.long("total-size")
.action(ArgAction::SetTrue)
.help("Display the total size of directories"),
)
.arg(
Arg::new("date")
.long("date")
.value_parser(validate_date_argument)
.default_value("date")
.action(ArgAction::Append)
.takes_value(true)
.number_of_values(1)
.help("How to display date [possible values: date, relative, +date-time-format]"),
)
.arg(
Arg::new("timesort")
.short('t')
.long("timesort")
.overrides_with("sizesort")
.overrides_with("extensionsort")
.overrides_with("versionsort")
.overrides_with("sort")
.overrides_with("no-sort")
.action(ArgAction::SetTrue)
.help("Sort by time modified"),
)
.arg(
Arg::new("sizesort")
.short('S')
.long("sizesort")
.overrides_with("timesort")
.overrides_with("extensionsort")
.overrides_with("versionsort")
.overrides_with("sort")
.overrides_with("no-sort")
.action(ArgAction::SetTrue)
.help("Sort by size"),
)
.arg(
Arg::new("extensionsort")
.short('X')
.long("extensionsort")
.overrides_with("sizesort")
.overrides_with("timesort")
.overrides_with("versionsort")
.overrides_with("sort")
.overrides_with("no-sort")
.action(ArgAction::SetTrue)
.help("Sort by file extension"),
)
.arg(
Arg::new("versionsort")
.short('v')
.long("versionsort")
.action(ArgAction::SetTrue)
.overrides_with("timesort")
.overrides_with("sizesort")
.overrides_with("extensionsort")
.overrides_with("sort")
.overrides_with("no-sort")
.help("Natural sort of (version) numbers within text"),
)
.arg(
Arg::new("sort")
.long("sort")
.action(ArgAction::Append)
.value_parser(["size", "time", "version", "extension", "none"])
.takes_value(true)
.value_name("WORD")
.overrides_with("timesort")
.overrides_with("sizesort")
.overrides_with("extensionsort")
.overrides_with("versionsort")
.overrides_with("no-sort")
.help("sort by WORD instead of name")
)
.arg(
Arg::new("no-sort")
.short('U')
.long("no-sort")
.action(ArgAction::SetTrue)
.overrides_with("timesort")
.overrides_with("sizesort")
.overrides_with("extensionsort")
.overrides_with("sort")
.overrides_with("versionsort")
.help("Do not sort. List entries in directory order")
)
.arg(
Arg::new("reverse")
.short('r')
.long("reverse")
.action(ArgAction::SetTrue)
.help("Reverse the order of the sort"),
)
.arg(
Arg::new("group-dirs")
.long("group-dirs")
.value_parser(["none", "first", "last"])
.action(ArgAction::Append)
.number_of_values(1)
.help("Sort the directories then the files"),
)
.arg(
Arg::new("group-directories-first")
.long("group-directories-first")
.action(ArgAction::SetTrue)
.help("Groups the directories at the top before the files. Same as --group-dirs=first")
)
.arg(
Arg::new("blocks")
.long("blocks")
.action(ArgAction::Append)
.multiple_values(true)
.takes_value(true)
.use_value_delimiter(true)
.require_value_delimiter(true)
.value_parser([
"permission",
"user",
"group",
"context",
"size",
"date",
"name",
"inode",
"links",
])
.help("Specify the blocks that will be displayed and in what order"),
)
.arg(
Arg::new("classic")
.long("classic")
.action(ArgAction::SetTrue)
.help("Enable classic mode (display output similar to ls)"),
)
.arg(
Arg::new("no-symlink")
.long("no-symlink")
.action(ArgAction::SetTrue)
.help("Do not display symlink target"),
)
.arg(
Arg::new("ignore-glob")
.short('I')
.long("ignore-glob")
.action(ArgAction::Append)
.number_of_values(1)
.value_name("pattern")
.default_value("")
.help("Do not display files/directories with names matching the glob pattern(s). More than one can be specified by repeating the argument"),
)
.arg(
Arg::new("inode")
.short('i')
.long("inode")
.action(ArgAction::SetTrue)
.help("Display the index number of each file"),
)
.arg(
Arg::new("dereference")
.short('L')
.long("dereference")
.action(ArgAction::SetTrue)
.help("When showing file information for a symbolic link, show information for the file the link references rather than for the link itself"),
)
.arg(
Arg::new("context")
.short('Z')
.long("context")
.required(false)
.takes_value(false)
.action(ArgAction::SetTrue)
.help("Print security context (label) of each file"),
)
.arg(
Arg::new("hyperlink")
.long("hyperlink")
.value_parser(["always", "auto", "never"])
.default_value("never")
.action(ArgAction::Append)
.takes_value(true)
.number_of_values(1)
.help("Attach hyperlink to filenames"),
)
.arg(
Arg::new("header")
.long("header")
.action(ArgAction::SetTrue)
.help("Display block headers"),
)
.arg(
Arg::new("system-protected")
.long("system-protected")
.action(ArgAction::SetTrue)
.help("Includes files with the windows system protection flag set. This is the same as --all on other platforms")
.hide(!cfg!(windows)),
)
}
fn validate_date_argument(arg: &str) -> Result<String, String> {
if arg.starts_with('+') {
validate_time_format(arg)
} else if arg == "date" || arg == "relative" {
Result::Ok(arg.to_owned())
} else {
Result::Err("possible values: date, relative, +date-time-format".to_owned())
}
}
pub fn validate_time_format(formatter: &str) -> Result<String, String> {
let mut chars = formatter.chars();
loop {
match chars.next() {
Some('%') => match chars.next() {
Some('.') => match chars.next() {
Some('f') => (),
Some(n @ ('3' | '6' | '9')) => match chars.next() {
Some('f') => (),
Some(c) => return Err(format!("invalid format specifier: %.{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
},
Some(c) => return Err(format!("invalid format specifier: %.{}", c)),
None => return Err("missing format specifier".to_owned()),
},
Some(n @ (':' | '#')) => match chars.next() {
Some('z') => (),
Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
},
Some(n @ ('-' | '_' | '0')) => match chars.next() {
Some(
'C' | 'd' | 'e' | 'f' | 'G' | 'g' | 'H' | 'I' | 'j' | 'k' | 'l' | 'M' | 'm'
| 'S' | 's' | 'U' | 'u' | 'V' | 'W' | 'w' | 'Y' | 'y',
) => (),
Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
},
Some(
'A' | 'a' | 'B' | 'b' | 'C' | 'c' | 'D' | 'd' | 'e' | 'F' | 'f' | 'G' | 'g'
| 'H' | 'h' | 'I' | 'j' | 'k' | 'l' | 'M' | 'm' | 'n' | 'P' | 'p' | 'R' | 'r'
| 'S' | 's' | 'T' | 't' | 'U' | 'u' | 'V' | 'v' | 'W' | 'w' | 'X' | 'x' | 'Y'
| 'y' | 'Z' | 'z' | '+' | '%',
) => (),
Some(n @ ('3' | '6' | '9')) => match chars.next() {
Some('f') => (),
Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
},
Some(c) => return Err(format!("invalid format specifier: %{}", c)),
None => return Err("missing format specifier".to_owned()),
},
None => break,
_ => continue,
}
}
Ok(formatter.to_owned())
}