Repository navigation
Expand file tree
/
Copy pathinput.rs
More file actions
52 lines (46 loc) · 1.57 KB
/
Copy pathinput.rs
File metadata and controls
52 lines (46 loc) · 1.57 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
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use forge_api::Environment;
use crate::editor::{ForgeEditor, ReadResult};
use crate::model::{ForgeCommandManager, SlashCommand};
use crate::prompt::ForgePrompt;
use crate::tracker;
/// Console implementation for handling user input via command line.
pub struct Console {
command: Arc<ForgeCommandManager>,
editor: Mutex<ForgeEditor>,
}
impl Console {
/// Creates a new instance of `Console`.
pub fn new(
env: Environment,
custom_history_path: Option<PathBuf>,
command: Arc<ForgeCommandManager>,
) -> Self {
let editor = Mutex::new(ForgeEditor::new(env, custom_history_path, command.clone()));
Self { command, editor }
}
}
impl Console {
pub async fn prompt(&self, prompt: ForgePrompt) -> anyhow::Result<SlashCommand> {
loop {
let mut forge_editor = self.editor.lock().unwrap();
let user_input = forge_editor.prompt(&prompt)?;
drop(forge_editor);
match user_input {
ReadResult::Continue => continue,
ReadResult::Exit => return Ok(SlashCommand::Exit),
ReadResult::Empty => continue,
ReadResult::Success(text) => {
tracker::prompt(text.clone());
return self.command.parse(&text);
}
}
}
}
/// Sets the buffer content for the next prompt
pub fn set_buffer(&self, content: String) {
let mut editor = self.editor.lock().unwrap();
editor.set_buffer(content);
}
}