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
use super::*;
use rustyline::{completion::FilenameCompleter, Context, Helper};
use std::env;
use std::path::PathBuf;
impl Promptable for PathBuf {
fn prompt<S: AsRef<str>>(msg: S) -> Result<Self> {
prompt_path(msg)
}
fn prompt_opt<S: AsRef<str>>(msg: S) -> Result<Option<Self>> {
prompt_path_opt(msg)
}
fn prompt_default<S: AsRef<str>>(msg: S, default: Self) -> Result<Self> {
let msg = format!("{} (default={})", msg.as_ref(), default.display());
Ok(prompt_path_opt(msg)?.unwrap_or(default))
}
}
struct FilenameHelper {
completer: FilenameCompleter,
}
impl Helper for FilenameHelper {}
impl rustyline::hint::Hinter for FilenameHelper {}
impl rustyline::validate::Validator for FilenameHelper {}
impl rustyline::highlight::Highlighter for FilenameHelper {}
impl Completer for FilenameHelper {
type Candidate = Pair;
fn complete(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Result<(usize, Vec<Pair>)> {
self.completer.complete(line, pos, ctx)
}
}
fn prompt_path<S: AsRef<str>>(msg: S) -> Result<PathBuf> {
let helper = FilenameHelper {
completer: FilenameCompleter::new(),
};
let s = Prompter::with_helper(helper).prompt_nonempty(msg)?;
Ok(PathBuf::from(path_expand(s)))
}
fn prompt_path_opt<S: AsRef<str>>(msg: S) -> Result<Option<PathBuf>> {
let helper = FilenameHelper {
completer: FilenameCompleter::new(),
};
Ok(Prompter::with_helper(helper)
.prompt_opt(msg)?
.map(path_expand)
.map(PathBuf::from))
}
fn path_expand(s: String) -> String {
if s.starts_with('~') {
if let Ok(home) = env::var("HOME") {
return s.replacen("~", &home, 1);
}
}
s
}