Repository navigation
Expand file tree
/
Copy patheditor.rs
More file actions
135 lines (117 loc) · 4.18 KB
/
Copy patheditor.rs
File metadata and controls
135 lines (117 loc) · 4.18 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
use std::path::PathBuf;
use std::sync::Arc;
use forge_api::Environment;
use nu_ansi_term::{Color, Style};
use reedline::{
ColumnarMenu, DefaultHinter, EditCommand, Emacs, FileBackedHistory, KeyCode, KeyModifiers,
MenuBuilder, Prompt, Reedline, ReedlineEvent, ReedlineMenu, Signal, default_emacs_keybindings,
};
use super::completer::InputCompleter;
use crate::model::ForgeCommandManager;
// TODO: Store the last `HISTORY_CAPACITY` commands in the history file
const HISTORY_CAPACITY: usize = 1024 * 1024;
const COMPLETION_MENU: &str = "completion_menu";
pub struct ForgeEditor {
editor: Reedline,
}
pub enum ReadResult {
Success(String),
Empty,
Continue,
Exit,
}
impl ForgeEditor {
fn init() -> reedline::Keybindings {
let mut keybindings = default_emacs_keybindings();
// on TAB press shows the completion menu, and if we've exact match it will
// insert it
keybindings.add_binding(
KeyModifiers::NONE,
KeyCode::Tab,
ReedlineEvent::UntilFound(vec![
ReedlineEvent::Menu(COMPLETION_MENU.to_string()),
ReedlineEvent::Edit(vec![EditCommand::Complete]),
]),
);
// on CTRL + k press clears the screen
keybindings.add_binding(
KeyModifiers::CONTROL,
KeyCode::Char('k'),
ReedlineEvent::ClearScreen,
);
// on CTRL + r press searches the history
keybindings.add_binding(
KeyModifiers::CONTROL,
KeyCode::Char('r'),
ReedlineEvent::SearchHistory,
);
// on ALT + Enter press inserts a newline
keybindings.add_binding(
KeyModifiers::ALT,
KeyCode::Enter,
ReedlineEvent::Edit(vec![EditCommand::InsertNewline]),
);
keybindings
}
pub fn new(
env: Environment,
custom_history_path: Option<PathBuf>,
manager: Arc<ForgeCommandManager>,
) -> Self {
// Store file history in system config directory
let history_file = env.history_path(custom_history_path.as_ref());
let history = Box::new(
FileBackedHistory::with_file(HISTORY_CAPACITY, history_file).unwrap_or_default(),
);
let completion_menu = Box::new(
ColumnarMenu::default()
.with_name(COMPLETION_MENU)
.with_marker("")
.with_text_style(Style::new().bold().fg(Color::Cyan))
.with_selected_text_style(Style::new().on(Color::White).fg(Color::Black)),
);
let edit_mode = Box::new(Emacs::new(Self::init()));
let editor = Reedline::create()
.with_completer(Box::new(InputCompleter::new(env.cwd, manager)))
.with_history(history)
.with_hinter(Box::new(
DefaultHinter::default().with_style(Style::new().fg(Color::DarkGray)),
))
.with_menu(ReedlineMenu::EngineCompleter(completion_menu))
.with_edit_mode(edit_mode)
.with_quick_completions(true)
.with_ansi_colors(true)
.use_bracketed_paste(true);
Self { editor }
}
pub fn prompt(&mut self, prompt: &dyn Prompt) -> anyhow::Result<ReadResult> {
let signal = self.editor.read_line(prompt);
signal
.map(Into::into)
.map_err(|e| anyhow::anyhow!(ReadLineError(e)))
}
/// Sets the buffer content to be pre-filled on the next prompt
pub fn set_buffer(&mut self, content: String) {
self.editor
.run_edit_commands(&[EditCommand::InsertString(content)]);
}
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct ReadLineError(std::io::Error);
impl From<Signal> for ReadResult {
fn from(signal: Signal) -> Self {
match signal {
Signal::Success(buffer) => {
let trimmed = buffer.trim();
if trimmed.is_empty() {
ReadResult::Empty
} else {
ReadResult::Success(trimmed.to_string())
}
}
Signal::CtrlC => ReadResult::Continue,
Signal::CtrlD => ReadResult::Exit,
}
}
}