-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
201 lines (172 loc) · 5.69 KB
/
Copy pathutils.rs
File metadata and controls
201 lines (172 loc) · 5.69 KB
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
use anyhow::{Context, Result};
use std::{
env,
fs::DirEntry,
io::{Write, stdout},
path::PathBuf,
sync::mpsc::{self, Receiver},
time::Duration,
};
use termion::{cursor, event::Key};
use thiserror::Error;
use crate::history::HISTORY;
#[derive(Debug, Error)]
pub enum CustomError {
#[error("{0}: command not found")]
CommandNotFound(String),
}
fn replace_input<W: Write>(stdout: &mut W, old: &str, new: &str) -> Result<()> {
if !old.is_empty() {
write!(stdout, "{}\x1B[K", cursor::Left(old.len() as u16))?;
}
write!(stdout, "{} ", new)?;
write!(stdout, "{}", cursor::Left(1))?;
stdout.flush()?;
Ok(())
}
pub fn get_input(key_rx: &Receiver<Key>, job_rx: &Receiver<i32>) -> Result<String> {
let mut stdout = stdout();
let mut input = String::new();
let mut input_pos: usize = 0;
let mut history_index: Option<usize> = None;
write!(stdout, "\r\n$ ")?;
stdout.flush()?;
loop {
while let Ok(_) = job_rx.try_recv() {
write!(stdout, "$ {}", input)?;
let tail = (input.len() - input_pos) as u16;
if tail > 0 {
write!(stdout, "{}", cursor::Left(tail))?;
}
stdout.flush()?;
}
// wait up to 50ms for a key, then loop back to check sigchld
match key_rx.recv_timeout(Duration::from_millis(50)) {
Ok(Key::Char('\n')) => {
write!(stdout, "\r\n")?;
stdout.flush()?;
break;
}
Ok(Key::Char('\t')) => {}
Ok(Key::Char(c)) => {
input.insert(input_pos, c);
input_pos += 1;
write!(stdout, "{}", &input[input_pos - 1..])?;
let tail = input.len() - input_pos;
if tail > 0 {
write!(stdout, "{}", cursor::Left(tail as u16))?;
}
stdout.flush()?;
}
Ok(Key::Backspace) => {
if input_pos > 0 {
input_pos -= 1;
input.remove(input_pos);
write!(stdout, "{}", cursor::Left(1))?;
write!(stdout, "{} ", &input[input_pos..])?;
let tail = input.len() - input_pos + 1;
write!(stdout, "{}", cursor::Left(tail as u16))?;
stdout.flush()?;
}
}
Ok(Key::Left) => {
if input_pos > 0 {
input_pos -= 1;
write!(stdout, "{}", cursor::Left(1))?;
stdout.flush()?;
}
}
Ok(Key::Right) => {
if input_pos < input.len() {
input_pos += 1;
write!(stdout, "{}", cursor::Right(1))?;
stdout.flush()?;
}
}
Ok(Key::Up) => {
let history = &HISTORY.lock().unwrap().items;
if history.is_empty() {
continue;
}
let next_index = match history_index {
Some(index) => {
if index > 0 {
index - 1
} else {
index
}
}
None => history.len() - 1,
};
let new_input = history[next_index].clone();
replace_input(&mut stdout, &input, &new_input)?;
input = new_input;
input_pos = input.len();
history_index = Some(next_index);
}
Ok(Key::Down) => {
let history = &HISTORY.lock().unwrap().items;
let Some(index) = history_index else { continue };
let (next_index, new_input) = if index == history.len() - 1 {
(None, String::new())
} else {
(Some(index + 1), history[index + 1].clone())
};
replace_input(&mut stdout, &input, &new_input)?;
input = new_input;
input_pos = input.len();
history_index = next_index;
}
Ok(_) => {}
Err(mpsc::RecvTimeoutError::Timeout) => continue,
Err(mpsc::RecvTimeoutError::Disconnected) => break,
}
}
let result = input.trim().to_string();
if !result.is_empty() {
let history = &mut HISTORY.lock().unwrap().items;
history.push(result.clone());
}
Ok(result)
}
pub fn get_paths() -> Result<Vec<PathBuf>> {
let paths = env::var_os("PATH").context("Getting PATH evn variable")?;
let split_paths = env::split_paths(&paths).filter(|path| path.is_dir());
Ok(split_paths.collect())
}
#[cfg(unix)]
fn is_executable(entry: &DirEntry) -> bool {
use std::os::unix::fs::PermissionsExt;
entry
.metadata()
.map(|m| m.permissions().mode() & 0o111 != 0)
.unwrap_or(false)
}
#[cfg(windows)]
fn is_executable(entry: &DirEntry) -> bool {
matches!(
entry.path().extension().and_then(|e| e.to_str()),
Some("exe" | "bat" | "cmd")
)
}
pub fn find_in_path(
paths: &[PathBuf],
file_name: &str,
permissions_mode: Option<u32>,
) -> Option<DirEntry> {
for path in paths {
let Ok(entries) = path.read_dir() else {
continue;
};
for entry in entries.flatten() {
if entry.file_name() != file_name {
continue;
}
if permissions_mode.is_some() && !is_executable(&entry) {
continue;
}
return Some(entry);
}
}
None
}