broot 1.56.2

File browser and launcher
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use {
    super::*,
    crate::{
        app::*,
        command::*,
        path::{
            self,
            PathAnchor,
        },
    },
    regex::Captures,
    rustc_hash::FxHashMap,
    std::path::{
        Path,
        PathBuf,
    },
};

/// a temporary structure gathering selection and invocation
/// parameters and able to generate an executable string from
/// a verb's execution pattern
pub struct ExecutionBuilder<'b> {
    /// the current file selection
    pub sel_info: SelInfo<'b>,

    /// the current root of the app
    root: &'b Path,

    /// the selection in the other panel, when there are exactly two
    other_file: Option<&'b PathBuf>,

    /// parsed arguments
    invocation_values: Option<FxHashMap<String, String>>,

    /// whether to keep groups which can't be solved or remove them
    keep_groups: bool,

    target: Target,
}

/// Whether we're trying to build the command as a string or as a vec of tokens (in
/// which case we don't want to do the same escaping, for example)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Target {
    String,
    Tokens,
}

impl<'b> ExecutionBuilder<'b> {
    /// constructor to use when there's no invocation string
    /// (because we're in the process of building one, for example
    /// when a verb is triggered from a key shortcut)
    pub fn without_invocation(
        sel_info: SelInfo<'b>,
        app_state: &'b AppState,
    ) -> Self {
        Self {
            sel_info,
            root: &app_state.root,
            other_file: app_state.other_panel_path.as_ref(),
            invocation_values: None,
            keep_groups: false,
            target: Target::Tokens,
        }
    }
    pub fn with_invocation(
        invocation_parser: Option<&InvocationParser>,
        sel_info: SelInfo<'b>,
        app_state: &'b AppState,
        invocation_args: Option<&String>,
    ) -> Self {
        let invocation_values = invocation_parser
            .as_ref()
            .zip(invocation_args.as_ref())
            .and_then(|(parser, args)| parser.parse(args));
        Self {
            sel_info,
            root: &app_state.root,
            other_file: app_state.other_panel_path.as_ref(),
            invocation_values,
            keep_groups: false,
            target: Target::Tokens,
        }
    }

    /// Return the replacing value for the whole sel_info
    ///
    /// When you have a multiselection and no merging flag, don't call this function
    /// but get_sel_capture_replacement while building a command per selection.
    fn get_arg_replacement(
        &self,
        arg_def: &VerbArgDef,
        con: &AppContext,
    ) -> Option<String> {
        let merging_flag = arg_def.merging_flag();
        match self.sel_info {
            SelInfo::None => self.get_sel_arg_replacement(arg_def, None, con),
            SelInfo::One(sel) => self.get_sel_arg_replacement(arg_def, Some(sel), con),
            SelInfo::More(stage) => {
                let mut sels = stage.to_selections();
                if let Some(merging_flag) = merging_flag {
                    let mut values = Vec::new();
                    for sel in sels {
                        let rcr = self.get_sel_arg_replacement(arg_def, Some(sel), con);
                        if let Some(rcr) = rcr {
                            values.push(rcr);
                        }
                    }
                    merging_flag.merge_values(values)
                } else {
                    // we're called with no specific selection and there's no merging
                    // strategy, this should probably not really happen, we'll take
                    // the first selection
                    let sel = if sels.is_empty() {
                        None
                    } else {
                        Some(sels.swap_remove(0))
                    };
                    self.get_sel_arg_replacement(arg_def, sel, con)
                }
            }
        }
    }

    /// return the standard replacement (ie not one from the invocation)
    fn get_sel_name_standard_replacement(
        &self,
        name: &str,
        sel: Option<Selection<'_>>,
        con: &AppContext,
    ) -> Option<String> {
        match name {
            "root" => Some(self.path_to_string(self.root)),
            "initial-root" => Some(self.path_to_string(&con.initial_root)),
            "line" => sel.map(|s| s.line.to_string()),
            "file" => sel.map(|s| s.path).map(|p| self.path_to_string(p)),
            "file-name" => sel
                .map(|s| s.path)
                .and_then(|path| path.file_name())
                .and_then(|oss| oss.to_str())
                .map(|s| s.to_string()),
            "file-stem" => sel
                .map(|s| s.path)
                .and_then(|path| path.file_stem())
                .and_then(|oss| oss.to_str())
                .map(|s| s.to_string()),
            "file-extension" => {
                debug!("expending file extension");
                sel.map(|s| s.path)
                    .and_then(|path| path.extension())
                    .and_then(|oss| oss.to_str())
                    .map(|s| s.to_string())
            }
            "file-dot-extension" => {
                debug!("expending file dot extension");
                sel.map(|s| s.path)
                    .and_then(|path| path.extension())
                    .and_then(|oss| oss.to_str())
                    .map(|ext| format!(".{ext}"))
                    .or_else(|| Some("".to_string()))
            }
            "directory" => sel
                .map(|s| path::closest_dir(s.path))
                .map(|p| self.path_to_string(p)),
            "parent" => sel
                .and_then(|s| s.path.parent())
                .map(|p| self.path_to_string(p)),
            "other-panel-file" => self.other_file.map(|p| self.path_to_string(p)),
            "other-panel-filename" => self
                .other_file
                .and_then(|path| path.file_name())
                .and_then(|oss| oss.to_str())
                .map(|s| s.to_string()),
            "other-panel-directory" => self
                .other_file
                .map(|p| path::closest_dir(p))
                .as_ref()
                .map(|p| self.path_to_string(p)),
            "other-panel-parent" => self
                .other_file
                .and_then(|p| p.parent())
                .map(|p| self.path_to_string(p)),
            "git-root" => {
                // path to git repo workdir
                debug!("finding git root");
                sel.and_then(|s| git2::Repository::discover(s.path).ok())
                    .and_then(|repo| repo.workdir().map(|p| self.path_to_string(p)))
            }
            "git-name" => {
                // name of the git repo workdir
                sel.and_then(|s| git2::Repository::discover(s.path).ok())
                    .and_then(|repo| {
                        repo.workdir().and_then(|path| {
                            path.file_name()
                                .and_then(|oss| oss.to_str())
                                .map(|s| s.to_string())
                        })
                    })
            }
            "file-git-relative" => {
                // file path relative to git repo workdir
                let sel = sel?;
                let path = git2::Repository::discover(self.root)
                    .ok()
                    .and_then(|repo| repo.workdir().map(|p| self.path_to_string(p)))
                    .and_then(|gitroot| sel.path.strip_prefix(gitroot).ok())
                    .filter(|p| {
                        // it's empty when the file is both the tree root and the git root
                        !p.as_os_str().is_empty()
                    })
                    .unwrap_or(sel.path);
                Some(self.path_to_string(path))
            }
            "file-root-relative" => {
                // file path relative to the tree root
                sel?
                    .path
                    .strip_prefix(self.root)
                    .ok()
                    .map(|p| self.path_to_string(p))
            }
            #[cfg(unix)]
            "server-name" => con.server_name.clone(),
            _ => None,
        }
    }
    fn get_sel_arg_replacement(
        &self,
        arg_def: &VerbArgDef,
        sel: Option<Selection<'_>>,
        con: &AppContext,
    ) -> Option<String> {
        let name = &arg_def.name;
        self.get_sel_name_standard_replacement(name, sel, con)
            .or_else(|| {
                // it's not one of the standard group names, so we'll look
                // into the ones provided by the invocation pattern
                self.invocation_values
                    .as_ref()
                    .and_then(|map| map.get(name))
                    .and_then(|value| {
                        if arg_def.has_flag(VerbArgFlag::PathFromDirectory) {
                            sel.map(|s| path::closest_dir(s.path))
                                .map(|dir| path::path_from(dir, PathAnchor::Unspecified, value))
                                .map(|pb| self.path_to_string(pb))
                        } else if arg_def.has_flag(VerbArgFlag::PathFromParent) {
                            sel.and_then(|s| s.path.parent())
                                .map(|dir| path::path_from(dir, PathAnchor::Unspecified, value))
                                .map(|pb| self.path_to_string(pb))
                        } else {
                            Some(value.to_string())
                        }
                    })
            })
    }
    fn replace_args(
        &self,
        s: &str,
        replacer: &mut dyn FnMut(&VerbArgDef) -> Option<String>,
    ) -> String {
        ARG_DEF_GROUP
            .replace_all(s, |ec: &Captures<'_>| {
                let arg_def = VerbArgDef::from_capture(ec);
                replacer(&arg_def).unwrap_or_else(|| {
                    if self.keep_groups {
                        ec[0].to_string()
                    } else {
                        "".to_string()
                    }
                })
            })
            .to_string()
    }
    /// fills groups having a default value (after the colon)
    ///
    /// This is used to fill the input in case on non auto_exec
    /// verb triggered with a key.
    ///
    /// In invocation pattern, the part after the colon isn't handled
    /// as a 'flag' but as a default value
    pub fn invocation_with_default(
        &self,
        verb_invocation: &VerbInvocation,
        con: &AppContext,
    ) -> VerbInvocation {
        VerbInvocation {
            name: verb_invocation.name.clone(),
            args: verb_invocation.args.as_ref().map(|a| {
                ARG_DEF_GROUP
                    .replace_all(a.as_str(), |ec: &Captures<'_>| {
                        ec.get(2)
                            .map(|default_name| default_name.as_str())
                            .and_then(|default_name| {
                                self.get_sel_name_standard_replacement(
                                    default_name,
                                    self.sel_info.first_sel(),
                                    con,
                                )
                            })
                            .unwrap_or_default()
                    })
                    .to_string()
            }),
            bang: verb_invocation.bang,
        }
    }

    fn base_dir(&self) -> &Path {
        self.sel_info.one_sel().map_or(self.root, |sel| sel.path)
    }
    /// replace groups in a sequence
    ///
    /// Replacing escapes for the shell for externals, and without
    /// escaping for internals.
    ///
    /// Note that this is *before* asking the (local or remote) panel
    /// state the sequential execution of the different commands. In
    /// this secondary execution, new replacements are expected too,
    /// depending on the verbs.
    pub fn sequence(
        &mut self,
        sequence: &Sequence,
        verb_store: &VerbStore,
        con: &AppContext,
        panel_state_type: Option<PanelStateType>,
    ) -> Sequence {
        let mut inputs = Vec::new();
        for input in sequence.raw.split(&sequence.separator) {
            let raw_parts = CommandParts::from(input.to_string());
            let (_, verb_invocation) = raw_parts.split();
            let verb_is_external = verb_invocation
                .and_then(|vi| {
                    let command = Command::from_parts(vi, true);
                    if let Command::VerbInvocate(invocation) = &command {
                        let search = verb_store.search_prefix(&invocation.name, panel_state_type);
                        if let PrefixSearchResult::Match(_, verb) = search {
                            return Some(verb);
                        }
                    }
                    None
                })
                .map_or(false, |verb| verb.get_internal().is_none());
            let input = if verb_is_external {
                self.shell_exec_string(&ExecPattern::from_string(input), con)
            } else {
                self.string(input, con)
            };
            inputs.push(input);
        }
        Sequence {
            raw: inputs.join(&sequence.separator),
            separator: sequence.separator.clone(),
        }
    }

    fn string(
        &self,
        pattern: &str,
        con: &AppContext,
    ) -> String {
        self.replace_args(pattern, &mut |arg_def| {
            self.get_arg_replacement(arg_def, con)
        })
    }

    /// build a path from a pattern (eg the `working_dir` parameter of a verb definition)
    pub fn path(
        &self,
        pattern: &str,
        con: &AppContext,
    ) -> PathBuf {
        path::path_from(
            self.base_dir(),
            path::PathAnchor::Unspecified,
            &self.replace_args(pattern, &mut |arg_def| {
                self.get_arg_replacement(arg_def, con)
            }),
        )
    }

    /// build a shell compatible command, with escapings
    pub fn shell_exec_string(
        &mut self,
        exec_pattern: &ExecPattern,
        con: &AppContext,
    ) -> String {
        self.target = Target::String; // this ensures proper escaping
        let tokens = self.exec_token(exec_pattern, con);
        tokens.join(" ")
    }

    /// build a shell compatible command, with escapings, for a specific
    /// selection (this is intended for execution on all selections of a
    /// stage)
    pub fn sel_shell_exec_string(
        &mut self,
        exec_pattern: &ExecPattern,
        sel: Option<Selection<'_>>,
        con: &AppContext,
    ) -> String {
        self.target = Target::String; // this ensures proper escaping
        let tokens = self.sel_exec_token(exec_pattern, sel, con);
        tokens.join(" ")
    }

    /// build a vec of tokens which can be passed to Command to
    /// launch an executable.
    pub fn exec_token(
        &self,
        exec_pattern: &ExecPattern,
        con: &AppContext,
    ) -> Vec<String> {
        // When a token is a space-separated arg, and the selection is multiple,
        // we want to build several tokens so that it's received as several args by the
        // executed program, and not as a single arg with spaces.
        // This complex work is needed only when the selection is multiple and there's
        // a "space-separated" flag in the capture
        let mut output = Vec::new();
        for token in exec_pattern.tokens() {
            if let Some(ec) = capture_if_total(&ARG_DEF_GROUP, token) {
                let arg_def = VerbArgDef::from_capture(&ec);
                let space_separated = arg_def.has_flag(VerbArgFlag::SpaceSeparated);
                if space_separated {
                    if let SelInfo::More(stage) = &self.sel_info {
                        let sels = stage.to_selections();
                        for sel in sels {
                            if let Some(s) = self.get_sel_arg_replacement(&arg_def, Some(sel), con)
                            {
                                output.push(s);
                            }
                        }
                        continue; // we did the replacement
                    }
                }
            }
            // as we won't be able to build several tokens from this one, we do the
            // standard replacement
            let replaced =
                self.replace_args(token, &mut |arg_def| self.get_arg_replacement(arg_def, con));
            output.push(fix_token_path(replaced));
        }
        output
    }

    /// build a vec of tokens which can be passed to Command to
    /// launch an executable.
    /// This is intended for execution on all selections of a stage
    /// when the exec pattern isn't merging.
    pub fn sel_exec_token(
        &mut self,
        exec_pattern: &ExecPattern,
        sel: Option<Selection<'_>>,
        con: &AppContext,
    ) -> Vec<String> {
        exec_pattern
            .tokens()
            .iter()
            .map(|s| {
                self.replace_args(s, &mut |arg_def| {
                    self.get_sel_arg_replacement(arg_def, sel, con)
                })
            })
            .map(fix_token_path)
            .collect()
    }

    /// Convert a path (or part of a path) to a string, with escaping if needed (depending on the target)
    fn path_to_string<P: AsRef<Path>>(
        &self,
        path: P,
    ) -> String {
        let s = path.as_ref().to_string_lossy();
        if self.target == Target::Tokens {
            // when building tokens, we don't want to do any escaping,
            // even if there are special characters
            return s.to_string();
        }
        if !regex_is_match!(r#"[\s"']"#, &s) {
            // if there's no special character, we don't need to escape or wrap
            return s.to_string();
        }
        // first we replace single quotes by `'"'"'` (close the single quote, add an escaped
        // single quote, and reopen the single quote)
        let s = s.replace('\'', r#"'"'"#);
        // then we wrap the whole thing in single quotes
        let s = format!("'{}'", s);
        s
    }
}

fn capture_if_total<'h>(
    regex: &Regex,
    s: &'h str,
) -> Option<Captures<'h>> {
    let captures = regex.captures(s)?;
    let overall_match = captures.get(0)?;
    if overall_match.start() == 0 && overall_match.end() == s.len() {
        Some(captures)
    } else {
        None
    }
}

fn fix_token_path<T: Into<String> + AsRef<str>>(token: T) -> String {
    let path = Path::new(token.as_ref());
    if path.exists() {
        if let Some(path) = path.to_str() {
            return path.to_string();
        }
    } else if path::TILDE_REGEX.is_match(token.as_ref()) {
        let path = path::untilde(token.as_ref());
        if path.exists() {
            if let Some(path) = path.to_str() {
                return path.to_string();
            }
        }
    }
    token.into()
}

#[cfg(test)]
mod execution_builder_test {

    // allows writing vo!["a", "b"] to build a vec of strings
    macro_rules! vo {
        ($($item:literal),* $(,)?) => {{
            let mut vec = Vec::new();
            $(
                vec.push($item.to_owned());
            )*
            vec
        }}
    }

    use super::*;

    fn check_build_execution_from_sel(
        exec_patterns: Vec<ExecPattern>,
        path: &str,
        replacements: Vec<(&str, &str)>,
        chk_exec_token: Vec<&str>,
    ) {
        let path = PathBuf::from(path);
        let sel = Selection {
            path: &path,
            line: 0,
            stype: SelectionType::File,
            is_exe: false,
        };
        let app_state = AppState::new(PathBuf::from("/".to_owned()));
        let mut builder = ExecutionBuilder::without_invocation(SelInfo::One(sel), &app_state);
        let mut map = FxHashMap::default();
        for (k, v) in replacements {
            map.insert(k.to_owned(), v.to_owned());
        }
        builder.invocation_values = Some(map);
        let con = AppContext::default();
        for exec_pattern in exec_patterns {
            dbg!("checking pattern: {:#?}", &exec_pattern);
            let exec_token = builder.exec_token(&exec_pattern, &con);
            assert_eq!(exec_token, chk_exec_token);
        }
    }

    #[test]
    fn test_build_execution() {
        check_build_execution_from_sel(
            vec![ExecPattern::from_string("vi {file}")],
            "/home/dys/dev",
            vec![],
            vec!["vi", "/home/dys/dev"],
        );
        check_build_execution_from_sel(
            vec![
                ExecPattern::from_string("/bin/e.exe -a {arg} -e {file}"),
                ExecPattern::from_tokens(vo!["/bin/e.exe", "-a", "{arg}", "-e", "{file}"]),
            ],
            "expérimental & 试验性",
            vec![("arg", "deux mots")],
            vec![
                "/bin/e.exe",
                "-a",
                "deux mots",
                "-e",
                "expérimental & 试验性",
            ],
        );
        check_build_execution_from_sel(
            vec![
                ExecPattern::from_string("xterm -e \"kak {file}\""),
                ExecPattern::from_tokens(vo!["xterm", "-e", "kak {file}"]),
            ],
            "/path/to/file",
            vec![],
            vec!["xterm", "-e", "kak /path/to/file"],
        );
    }
}