replace reedline with rustyline

This commit is contained in:
Chance 2024-12-02 15:43:39 -05:00 committed by BitSyndicate
parent 8eea0680e8
commit 1898e6ff42
Signed by: bitsyndicate
GPG key ID: 443E4198D6BBA6DE
3 changed files with 29 additions and 62 deletions

View file

@ -12,8 +12,8 @@ lazy_static = "1.5.0"
log = "0.4.22" log = "0.4.22"
once_cell = "1.20.2" once_cell = "1.20.2"
parking_lot = "0.12.3" parking_lot = "0.12.3"
reedline = "0.37.0"
regex = "1.11.1" regex = "1.11.1"
rustyline = "15.0.0"
thiserror = "2.0.3" thiserror = "2.0.3"
tokio = { version = "1.41.1", features = ["macros", "rt", "rt-multi-thread"] } tokio = { version = "1.41.1", features = ["macros", "rt", "rt-multi-thread"] }
wgpu = "23.0.1" wgpu = "23.0.1"

View file

@ -1,7 +1,10 @@
use super::{commands, Callable, COMMAND_LIST}; use super::{commands, Callable, COMMAND_LIST};
use anyhow::Result;
use chrono::Local; use chrono::Local;
use reedline::{Prompt, Reedline, Signal}; use colored::Colorize;
use log::debug;
use regex::Regex; use regex::Regex;
use rustyline::DefaultEditor;
fn register_commands() { fn register_commands() {
COMMAND_LIST.add_command( COMMAND_LIST.add_command(
@ -44,46 +47,7 @@ fn register_commands() {
COMMAND_LIST.add_alias("clear".to_string(), "cls".to_string()); COMMAND_LIST.add_alias("clear".to_string(), "cls".to_string());
} }
struct ZPrompt {
left_text: String,
right_text: String,
}
impl Prompt for ZPrompt {
fn render_prompt_left(&self) -> std::borrow::Cow<str> {
std::borrow::Cow::Borrowed(&self.left_text)
}
fn render_prompt_right(&self) -> std::borrow::Cow<str> {
std::borrow::Cow::Borrowed(&self.right_text)
}
fn render_prompt_history_search_indicator(
&self,
_history_search: reedline::PromptHistorySearch,
) -> std::borrow::Cow<str> {
std::borrow::Cow::Borrowed("")
}
fn render_prompt_indicator(
&self,
prompt_mode: reedline::PromptEditMode,
) -> std::borrow::Cow<str> {
match prompt_mode {
reedline::PromptEditMode::Default => std::borrow::Cow::Borrowed(">>"),
reedline::PromptEditMode::Emacs => {
let timestamp = Local::now().format("[%H:%M:%S.%3f/SHELL] >>\t").to_string();
std::borrow::Cow::Owned(timestamp)
}
reedline::PromptEditMode::Vi(_) => std::borrow::Cow::Borrowed("vi>>"),
reedline::PromptEditMode::Custom(_) => std::borrow::Cow::Borrowed("custom>>"),
}
}
fn render_prompt_multiline_indicator(&self) -> std::borrow::Cow<str> {
std::borrow::Cow::Borrowed("><")
}
}
fn evaluate_command(input: &str) { fn evaluate_command(input: &str) {
if input.trim().is_empty() { if input.trim().is_empty() {
@ -115,30 +79,35 @@ fn evaluate_command(input: &str) {
} }
} }
pub async fn handle_repl() { pub async fn handle_repl() -> rustyline::Result<()> {
let mut line_editor = Reedline::create(); let mut line_editor = DefaultEditor::new()?;
if line_editor.load_history("history.txt").is_err() {
debug!("No previous history.");
}
let time = Local::now().format("%H:%M:%S.%3f").to_string();
let prompt = format!("[{}/{}] {}", time,"SHELL", ">>\t");
register_commands(); register_commands();
loop { loop {
let sig = line_editor.read_line(&ZPrompt { let sig = line_editor.readline(
left_text: String::new(), &prompt.bright_white()
right_text: "<<".to_string(), );
});
match sig { match sig {
Ok(Signal::Success(buffer)) => { Ok(line) => {
if buffer == "exit" { line_editor.add_history_entry(line.as_str())?;
std::process::exit(0); evaluate_command(line.as_str());
} else {
evaluate_command(&buffer);
}
} }
Ok(Signal::CtrlC) => { Err(rustyline::error::ReadlineError::Interrupted) => {
println!("\nCONTROL+C RECEIVED, TERMINATING"); println!("CTRL+C received, exiting...");
std::process::exit(0); std::process::exit(0);
} }
err => { Err(rustyline::error::ReadlineError::Eof) => {
eprintln!("Error: {:?}", err); println!("Error: CTRL+D pressed. Exiting...");
std::process::exit(0);
}
Err(err) => {
println!("Error: {}", err);
} }
} }
} }

View file

@ -24,24 +24,22 @@ async fn main() -> Result<()> {
if cli.log { if cli.log {
info!("Initializing Engine with logging to stdout enabled"); info!("Initializing Engine with logging to stdout enabled");
warn!("REPL cannot be used with logging enabled due to ReedLine not supporting writing to stdout");
core::init_renderer()?; core::init_renderer()?;
} else { } else {
LOGGER.write_to_stdout(); LOGGER.write_to_stdout();
info!("Initializing Engine with logging to stdout disabled"); info!("Initializing Engine with logging to stdout disabled");
warn!("REPL cannot be used with logging enabled due to ReedLine not supporting writing to stdout");
info!("Writing all logs to file z.log"); info!("Writing all logs to file z.log");
LOGGER.write_to_file("z.log"); LOGGER.write_to_file("z.log");
info!("Logging back to file z.log"); info!("Logging back to file z.log");
let shell_thread = tokio::task::spawn(async { let shell_thread = tokio::task::spawn(async {
core::repl::repl::handle_repl().await; core::repl::repl::handle_repl().await
}); });
core::init_renderer()?; core::init_renderer()?;
shell_thread.await?; shell_thread.await??; // LOL - Caz
} }
Ok(()) Ok(())