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
use {
crate::{
errors::{ProgramError, TreeBuildError},
tree_options::{OptionBool, TreeOptions},
},
std::{
env,
io,
path::{Path, PathBuf},
},
};
pub struct AppLaunchArgs {
pub root: PathBuf,
pub file_export_path: Option<String>,
pub cmd_export_path: Option<String>,
pub tree_options: TreeOptions,
pub commands: Option<String>,
pub install: bool,
pub height: Option<u16>,
pub no_style: bool,
}
fn get_cli_args<'a>() -> clap::ArgMatches<'a> {
clap::App::new("broot")
.version(env!("CARGO_PKG_VERSION"))
.author("dystroy <denys.seguret@gmail.com>")
.about("Balanced tree view + fuzzy search + BFS + customizable launcher")
.arg(clap::Arg::with_name("root").help("sets the root directory"))
.arg(
clap::Arg::with_name("cmd_export_path")
.long("outcmd")
.takes_value(true)
.help("where to write the produced cmd (if any)"),
)
.arg(
clap::Arg::with_name("commands")
.short("c")
.long("cmd")
.takes_value(true)
.help("commands to execute (space separated, experimental)"),
)
.arg(
clap::Arg::with_name("dates")
.short("d")
.long("dates")
.help("show the last modified date of files and directories"),
)
.arg(
clap::Arg::with_name("file_export_path")
.short("o")
.long("out")
.takes_value(true)
.help("where to write the produced path (if any)"),
)
.arg(
clap::Arg::with_name("gitignore")
.short("g")
.long("gitignore")
.takes_value(true)
.help("respect .gitignore rules (yes, no, auto)"),
)
.arg(
clap::Arg::with_name("hidden")
.short("h")
.long("hidden")
.help("show hidden files"),
)
.arg(
clap::Arg::with_name("height")
.long("height")
.help("height (if you don't want to fill the screen or for file export)")
.takes_value(true),
)
.arg(
clap::Arg::with_name("install")
.long("install")
.help("install or reinstall the br shell function"),
)
.arg(
clap::Arg::with_name("no-style")
.long("no-style")
.help("whether to remove all style and colors"),
)
.arg(
clap::Arg::with_name("only-folders")
.short("f")
.long("only-folders")
.help("only show folders"),
)
.arg(
clap::Arg::with_name("permissions")
.short("p")
.long("permissions")
.help("show permissions, with owner and group"),
)
.arg(
clap::Arg::with_name("sizes")
.short("s")
.long("sizes")
.help("show the size of files and directories"),
)
.get_matches()
}
#[cfg(not(windows))]
fn canonicalize_root(root: &Path) -> io::Result<PathBuf> {
root.canonicalize()
}
#[cfg(windows)]
fn canonicalize_root(root: &Path) -> io::Result<PathBuf> {
Ok(
if root.is_relative() {
env::current_dir()?.join(root)
} else {
root.to_path_buf()
}
)
}
pub fn read_launch_args() -> Result<AppLaunchArgs, ProgramError> {
let cli_args = get_cli_args();
let mut root = match cli_args.value_of("root") {
Some(path) => {
PathBuf::from(path)
}
None => env::current_dir()?,
};
if !root.exists() {
Err(TreeBuildError::FileNotFound {
path: format!("{:?}", &root),
})?;
}
if !root.is_dir() {
if let Some(parent) = root.parent() {
info!("Passed path isn't a directory => opening parent instead");
root = parent.to_path_buf();
} else {
Err(TreeBuildError::NotADirectory {
path: format!("{:?}", &root),
})?;
}
}
let root = canonicalize_root(&root)?;
let mut tree_options = TreeOptions::default();
tree_options.show_sizes = cli_args.is_present("sizes");
if tree_options.show_sizes {
tree_options.show_hidden = true;
tree_options.respect_git_ignore = OptionBool::No;
}
tree_options.only_folders = cli_args.is_present("only-folders");
tree_options.show_hidden = cli_args.is_present("hidden");
tree_options.show_dates = cli_args.is_present("dates");
tree_options.show_permissions = cli_args.is_present("permissions");
if let Some(respect_ignore) = cli_args.value_of("gitignore") {
tree_options.respect_git_ignore = respect_ignore.parse()?;
}
let install = cli_args.is_present("install");
let file_export_path = cli_args
.value_of("file_export_path")
.and_then(|s| Some(s.to_owned()));
let cmd_export_path = cli_args
.value_of("cmd_export_path")
.and_then(|s| Some(s.to_owned()));
let commands = cli_args
.value_of("commands")
.and_then(|s| Some(s.to_owned()));
let no_style = cli_args.is_present("no-style");
let height = cli_args.value_of("height").and_then(|s| s.parse().ok());
Ok(AppLaunchArgs {
root,
file_export_path,
cmd_export_path,
tree_options,
commands,
install,
height,
no_style,
})
}
pub fn ask_authorization() -> Result<bool, ProgramError> {
let mut answer = String::new();
io::stdin().read_line(&mut answer)?;
let answer = answer.trim();
Ok(match answer.as_ref() {
"n" | "N" => false,
_ => true,
})
}