| use std::path::PathBuf; |
| use std::sync::{Arc, Mutex}; |
|
|
| use forge_api::Environment; |
|
|
| use crate::editor::{ForgeEditor, ReadResult}; |
| use crate::model::{AppCommand, ForgeCommandManager}; |
| use crate::prompt::ForgePrompt; |
| use crate::tracker; |
|
|
| |
| pub struct Console { |
| command: Arc<ForgeCommandManager>, |
| editor: Mutex<ForgeEditor>, |
| } |
|
|
| impl 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: &mut ForgePrompt) -> anyhow::Result<AppCommand> { |
| 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(AppCommand::Exit), |
| ReadResult::Empty => continue, |
| ReadResult::Success(text) => { |
| tracker::prompt(text.clone()); |
| return self.command.parse(&text); |
| } |
| } |
| } |
| } |
|
|
| |
| pub fn set_buffer(&self, content: String) { |
| let mut editor = self.editor.lock().unwrap(); |
| editor.set_buffer(content); |
| } |
| } |
|
|