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
|
/// this module manages reading and translating
/// the arguments passed on launch of the application.
use {
crate::{
app::{App, AppContext},
conf::Conf,
display,
errors::{ProgramError, TreeBuildError},
launchable::Launchable,
shell_install::{ShellInstall, ShellInstallState},
tree::TreeOptions,
verb::VerbStore,
},
clap::{self, ArgMatches},
crossterm::{
self,
cursor,
event::{DisableMouseCapture, EnableMouseCapture},
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
QueueableCommand,
},
std::{
env,
io::{self, Write},
path::{Path, PathBuf},
str::FromStr,
},
};
/// launch arguments related to installation
/// (not used by the application after the first step)
struct InstallLaunchArgs {
install: Option<bool>, // installation is required
set_install_state: Option<ShellInstallState>, // the state to set
print_shell_function: Option<String>, // shell function to print on stdout
}
impl InstallLaunchArgs {
fn from(cli_args: &ArgMatches<'_>) -> Result<Self, ProgramError> {
let mut install = None;
if let Ok(s) = env::var("BR_INSTALL") {
if s == "yes" {
install = Some(true);
} else if s == "no" {
install = Some(false);
} else {
warn!("Unexpected value of BR_INSTALL: {:?}", s);
}
}
// the cli arguments may override the env var value
if cli_args.is_present("install") {
install = Some(true);
} else if cli_args.value_of("cmd-export-path").is_some() {
install = Some(false);
}
let print_shell_function = cli_args
.value_of("print-shell-function")
.map(str::to_string);
let set_install_state = cli_args
.value_of("set-install-state")
.map(ShellInstallState::from_str)
.transpose()?;
Ok(Self {
install,
set_install_state,
print_shell_function,
})
}
}
/// the parsed program launch arguments which are kept for the
/// life of the program
pub struct AppLaunchArgs {
pub root: PathBuf, // what should be the initial root
pub file_export_path: Option<String>, // where to write the produced path (if required with --out)
pub cmd_export_path: Option<String>, // where to write the produced command (if required with --outcmd)
pub tree_options: TreeOptions, // initial tree options
pub commands: Option<String>, // commands passed as cli argument, still unparsed
pub height: Option<u16>, // an optional height to replace the screen's one
pub no_style: bool, // whether to remove all styles (including colors)
#[cfg(feature = "client-server")]
pub listen: Option<String>,
}
#[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()
})
}
fn get_root_path(cli_args: &ArgMatches<'_>) -> Result<PathBuf, ProgramError> {
let mut root = cli_args
.value_of("ROOT")
.map_or(env::current_dir()?, PathBuf::from);
if !root.exists() {
return Err(TreeBuildError::FileNotFound {
path: format!("{:?}", &root),
}.into());
}
if !root.is_dir() {
// we try to open the parent directory if the passed file isn't one
if let Some(parent) = root.parent() {
info!("Passed path isn't a directory => opening parent instead");
root = parent.to_path_buf();
} else {
// let's give up
return Err(TreeBuildError::NotADirectory {
path: format!("{:?}", &root),
}.into());
}
}
Ok(canonicalize_root(&root)?)
}
fn is_output_piped() -> bool {
unsafe { libc::isatty(libc::STDOUT_FILENO) == 0 }
}
fn is_no_style(cli_matches: &ArgMatches) -> bool {
if cli_matches.is_present("no-style") {
return true;
}
match cli_matches.value_of("color") {
Some("yes") => false,
Some("no") => true,
_ => is_output_piped(),
}
}
/// run the application, and maybe return a launchable
/// which must be run after broot
pub fn run() -> Result<Option<Launchable>, ProgramError> {
let clap_app = crate::clap::clap_app();
// parse the launch arguments we got from cli
let cli_matches = clap_app.get_matches();
// read the install related arguments
let install_args = InstallLaunchArgs::from(&cli_matches)?;
// execute installation things required by launch args
let mut must_quit = false;
if let Some(state) = install_args.set_install_state {
state.write_file()?;
must_quit = true;
}
if let Some(shell) = &install_args.print_shell_function {
ShellInstall::print(shell)?;
must_quit = true;
}
if must_quit {
return Ok(None);
}
// read the list of specific config files
let specific_conf: Option<Vec<PathBuf>> = cli_matches
.value_of("conf")
.map(|s| s.split(';').map(PathBuf::from).collect());
// if we don't run on a specific config file, we check the
// configuration
if specific_conf.is_none() && install_args.install != Some(false) {
let mut shell_install = ShellInstall::new(install_args.install == Some(true));
shell_install.check()?;
if shell_install.should_quit {
return Ok(None);
}
}
// read the configuration file(s): either the standard one
// or the ones required by the launch args
let mut config = match &specific_conf {
Some(conf_paths) => {
let mut conf = Conf::default();
for path in conf_paths {
conf.read_file(path.to_path_buf())?;
}
conf
}
_ => time!(Conf::from_default_location())?,
};
debug!("config: {:#?}", &config);
// tree options are built from the default_flags
// found in the config file(s) (if any) then overriden
// by the cli args
let mut tree_options = TreeOptions::default();
tree_options.apply_config(&config)?;
tree_options.apply_launch_args(&cli_matches);
// verb store is completed from the config file(s)
let mut verb_store = VerbStore::default();
verb_store.init(&mut config)?;
// reading the other arguments
let file_export_path = cli_matches.value_of("file-export-path").map(str::to_string);
let cmd_export_path = cli_matches.value_of("cmd-export-path").map(str::to_string);
let commands = cli_matches.value_of("commands").map(str::to_string);
let no_style = is_no_style(&cli_matches);
let height = cli_matches.value_of("height").and_then(|s| s.parse().ok());
let root = get_root_path(&cli_matches)?;
#[cfg(feature = "client-server")]
if let Some(server_name) = cli_matches.value_of("send") {
use crate::{
command::Sequence,
net::{Client, Message},
};
let client = Client::new(server_name);
if let Some(seq) = &commands {
let message = Message::Sequence(Sequence::new_local(seq.to_string()));
client.send(&message)?;
} else if !cli_matches.is_present("get-root") {
let message = Message::Command(format!(":focus {}", root.to_string_lossy()));
client.send(&message)?;
};
if cli_matches.is_present("get-root") {
client.send(&Message::GetRoot)?;
}
return Ok(None);
}
let launch_args = AppLaunchArgs {
root,
file_export_path,
cmd_export_path,
tree_options,
commands,
height,
no_style,
#[cfg(feature = "client-server")]
listen: cli_matches.value_of("listen").map(str::to_string),
};
let context = AppContext::from(launch_args, verb_store, &config)?;
let mut w = display::writer();
let app = App::new(&context)?;
w.queue(EnterAlternateScreen)?;
w.queue(cursor::Hide)
|