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
|
use nix::sys::stat::Mode;
use radix_trie::{Trie, TrieCommon};
use rustyline::hint::{Hint, Hinter};
use rustyline::Context;
use rustyline_derive::{Completer, Helper, Highlighter, Validator};
use std::ffi::OsString;
use std::os::unix::fs::PermissionsExt;
static EXE_MASK: Mode = Mode::from_bits_truncate(0o111);
#[test]
fn test_exe_mask() {
let a = Mode::from_bits_truncate(0o100755);
assert!(a.contains(EXE_MASK));
}
#[derive(Completer, Helper, Validator, Highlighter)]
pub struct CommandHinter {
cmd_hints: Trie<String, CompleterHint>,
path_hints: Trie<String, CompleterHint>,
}
#[derive(Hash, Debug, PartialEq, Eq)]
pub struct CompleterHint {
display: String,
complete_up_to: usize,
}
impl Hint for CompleterHint {
fn display(&self) -> &str {
&self.display
}
fn completion(&self) -> Option<&str> {
if self.complete_up_to > 0 {
Some(&self.display[..self.complete_up_to])
} else {
None
}
}
}
impl CompleterHint {
fn new(text: &str, complete_up_to: &str) -> CompleterHint {
assert!(text.starts_with(complete_up_to));
CompleterHint {
display: text.into(),
complete_up_to: complete_up_to.len(),
}
}
fn from_path(text: OsString) -> CompleterHint {
CompleterHint {
complete_up_to: text.len(),
display: text.into_string().unwrap(),
}
}
fn suffix(&self, strip_chars: usize) -> CompleterHint {
CompleterHint {
display: self.display[strip_chars..].to_owned(),
complete_up_to: self.complete_up_to.saturating_sub(strip_chars),
}
}
}
impl CommandHinter {
pub fn new() -> Result<CommandHinter, std::io::Error> {
let mut cmd_hints = Trie::new();
let path_hints = Trie::new();
for path in std::env::var("PATH").unwrap_or_default().split(":") {
#[cfg(debug_assertions)]
println!("+index {}", path);
if let Ok(entries) = std::fs::read_dir(path) {
for x in entries {
if let Ok(entry) = x {
let mode = Mode::from_bits_truncate(entry.metadata()?.permissions().mode());
if mode.contains(EXE_MASK) {
cmd_hints.insert(
entry.file_name().into_string().unwrap(),
CompleterHint::from_path(entry.file_name()),
);
}
}
}
}
}
Ok(CommandHinter {
cmd_hints,
path_hints,
})
}
pub fn set_cwd(&mut self, path: &std::path::PathBuf) {
self.path_hints = Trie::new();
if let Ok(entries) = std::fs::read_dir(path) {
for x in entries {
if let Ok(entry) = x {
self.path_hints.insert(
entry.file_name().into_string().unwrap(),
CompleterHint::from_path(entry.file_name()),
);
}
}
}
}
}
fn hints(
trie: &Trie<String, CompleterHint>,
line: &str,
pos: usize,
) -> Option<std::collections::VecDeque<CompleterHint>> {
match trie.get_raw_descendant(line) {
Some(subtrie) => Some(
subtrie
.values()
.filter_map(|hint| {
if pos > 0 && hint.display.starts_with(&line[..pos]) {
Some(hint.suffix(pos))
} else {
None
}
})
.collect(),
),
None => None,
}
}
impl Hinter for CommandHinter {
type Hint = CompleterHint;
/// Takes the currently edited `line` with the cursor `pos`ition and
/// returns the string that should be displayed or `None`
/// if no hint is available for the text the user currently typed.
fn hint(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Option<CompleterHint> {
if line.is_empty() {
return None;
}
if pos < line.len() {
return None;
}
if let Some(mut idx) = line.rfind(' ') {
idx += 1;
if pos <= idx {
return None;
}
return hints(&self.path_hints, &line[idx..], pos - idx)
.and_then(|mut l| l.pop_front());
}
hints(&self.cmd_hints, line, pos).and_then(|mut l| l.pop_front())
}
}
|