blob: f845c6092c9665e74af28ab4bb6f7decba9223d1 (
plain)
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
|
use std::io;
use std::io::Write;
use std::process::{Command, Stdio};
use crate::commands::cursor_move;
use crate::context::AppContext;
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
use crate::ui::AppBackend;
pub fn search_fzf(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult {
let items = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.map(|list| {
let v: Vec<String> = list
.iter()
.enumerate()
.map(|(i, entry)| format!("{} {}\n", i, entry.file_name()))
.collect();
v
})
.unwrap_or_default();
if items.is_empty() {
return Err(JoshutoError::new(
JoshutoErrorKind::Io(io::ErrorKind::InvalidData),
"no files to select".to_string(),
));
}
backend.terminal_drop();
let mut fzf = match Command::new("fzf")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
{
Ok(child) => child,
Err(e) => {
backend.terminal_restore()?;
return Err(JoshutoError::from(e));
}
};
if let Some(fzf_stdin) = fzf.stdin.as_mut() {
let mut writer = io::BufWriter::new(fzf_stdin);
for item in items {
writer.write_all(item.as_bytes())?;
}
}
let fzf_output = fzf.wait_with_output();
backend.terminal_restore()?;
if let Ok(output) = fzf_output {
if output.status.success() {
if let Ok(selected) = std::str::from_utf8(&output.stdout) {
let selected_idx_str = selected.split_once(' ');
if let Some((selected_idx_str, _)) = selected_idx_str {
if let Ok(index) = selected_idx_str.parse::<usize>() {
cursor_move::cursor_move(context, index);
}
}
}
}
}
Ok(())
}
|