Implemented prompt expansion, and display for errors

This commit is contained in:
2025-03-18 15:54:06 -04:00
parent 464caa4517
commit 62c76e70a6
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)
}

View File

@@ -3,6 +3,7 @@ use std::borrow::Cow;
use rustyline::{completion::Completer, highlight::Highlighter, hint::{Hint, Hinter}, validate::{ValidationResult, Validator}, Helper};
use crate::{libsh::term::{Style, Styled}, parse::{lex::{LexFlags, LexStream}, ParseStream}};
use crate::prelude::*;
pub struct FernReadline {
}
@@ -67,7 +68,7 @@ impl Validator for FernReadline {
fn validate(&self, ctx: &mut rustyline::validate::ValidationContext) -> rustyline::Result<rustyline::validate::ValidationResult> {
return Ok(ValidationResult::Valid(None));
let mut tokens = vec![];
let tk_stream = LexStream::new(ctx.input(), LexFlags::empty());
let tk_stream = LexStream::new(Rc::new(ctx.input().to_string()), LexFlags::empty());
for tk in tk_stream {
if tk.is_err() {
return Ok(ValidationResult::Incomplete)