Implemented prompt expansion, and display for errors

This commit is contained in:
2025-03-18 15:54:06 -04:00
parent d673bb692f
commit 0d286ba006
14 changed files with 1022 additions and 288 deletions

View File

@@ -6,9 +6,9 @@ use std::path::Path;
use readline::FernReadline;
use rustyline::{error::ReadlineError, history::FileHistory, Editor};
use crate::{libsh::{error::ShResult, term::{Style, Styled}}, prelude::*};
use crate::{expand::expand_prompt, libsh::{error::ShResult, term::{Style, Styled}}, prelude::*};
fn init_rl<'s>() -> ShResult<Editor<FernReadline,FileHistory>> {
fn init_rl() -> ShResult<Editor<FernReadline,FileHistory>> {
let rl = FernReadline::new();
let mut editor = Editor::new()?;
editor.set_helper(Some(rl));
@@ -16,15 +16,35 @@ fn init_rl<'s>() -> ShResult<Editor<FernReadline,FileHistory>> {
Ok(editor)
}
pub fn read_line<'s>() -> ShResult<String> {
fn get_prompt() -> ShResult<String> {
let Ok(prompt) = env::var("PS1") else {
return Ok("$ ".styled(Style::Green | Style::Bold))
};
Ok(format!("\n{}",expand_prompt(&prompt)?))
}
fn get_hist_path() -> ShResult<PathBuf> {
if let Ok(path) = env::var("FERN_HIST") {
Ok(PathBuf::from(path))
} else {
let home = env::var("HOME")?;
Ok(PathBuf::from(format!("{home}/.fernhist")))
}
}
pub fn read_line() -> ShResult<String> {
assert!(isatty(STDIN_FILENO).unwrap());
let mut editor = init_rl()?;
let prompt = "$ ".styled(Style::Green | Style::Bold);
let prompt = get_prompt()?;
match editor.readline(&prompt) {
Ok(line) => {
if !line.is_empty() {
let hist_path = get_hist_path()?;
flog!(DEBUG, hist_path);
editor.add_history_entry(&line)?;
editor.save_history(&Path::new("/home/pagedmov/.fernhist"))?;
editor.save_history(&hist_path)?;
}
Ok(line)
}