Implemented functions and aliases

This commit is contained in:
2025-03-02 22:49:36 -05:00
parent a9a9642a2a
commit 5dd9ee96ad
20 changed files with 424 additions and 95 deletions

View File

@@ -16,7 +16,7 @@ impl<'a> Highlighter for SynHelper<'a> {
let raw = token.to_string();
match token.rule() {
TkRule::Comment => {
let styled = style_text(&raw, Style::BrightBlack);
let styled = &raw.styled(Style::BrightBlack);
result.push_str(&styled);
}
TkRule::ErrPipeOp |
@@ -26,46 +26,70 @@ impl<'a> Highlighter for SynHelper<'a> {
TkRule::RedirOp |
TkRule::BgOp => {
is_command = true;
let styled = style_text(&raw, Style::Cyan);
let styled = &raw.styled(Style::Cyan);
result.push_str(&styled);
}
TkRule::FuncName => {
let name = raw.strip_suffix("()").unwrap_or(&raw);
let styled = name.styled(Style::Cyan);
let rebuilt = format!("{styled}()");
result.push_str(&rebuilt);
}
TkRule::Keyword => {
if &raw == "for" {
in_array = true;
}
let styled = style_text(&raw, Style::Yellow);
let styled = &raw.styled(Style::Yellow);
result.push_str(&styled);
}
TkRule::BraceGrp => {
let body = &raw[1..raw.len() - 1];
let highlighted = self.highlight(body, 0).to_string();
let styled_o_brace = "{".styled(Style::BrightBlue);
let styled_c_brace = "}".styled(Style::BrightBlue);
let rebuilt = format!("{styled_o_brace}{highlighted}{styled_c_brace}");
is_command = false;
result.push_str(&rebuilt);
}
TkRule::Subshell => {
let body = &raw[1..raw.len() - 1];
let highlighted = self.highlight(body, 0).to_string();
let styled_o_paren = style_text("(", Style::BrightBlue);
let styled_c_paren = style_text(")", Style::BrightBlue);
let styled_o_paren = "(".styled(Style::BrightBlue);
let styled_c_paren = ")".styled(Style::BrightBlue);
let rebuilt = format!("{styled_o_paren}{highlighted}{styled_c_paren}");
is_command = false;
result.push_str(&rebuilt);
}
TkRule::Ident => {
if in_array {
if &raw == "in" {
let styled = style_text(&raw, Style::Yellow);
let styled = &raw.styled(Style::Yellow);
result.push_str(&styled);
} else {
let styled = style_text(&raw, Style::Magenta);
let styled = &raw.styled(Style::Magenta);
result.push_str(&styled);
}
} else if &raw == "{" || &raw == "}" {
result.push_str(&raw);
} else if is_command {
if get_bin_path(&token.to_string(), self.shenv).is_some() ||
self.shenv.logic().get_alias(&raw).is_some() ||
self.shenv.logic().get_function(&raw).is_some() ||
BUILTINS.contains(&raw.as_str()) {
let styled = style_text(&raw, Style::Green);
let styled = &raw.styled(Style::Green);
result.push_str(&styled);
} else {
let styled = style_text(&raw, Style::Red | Style::Bold);
let styled = &raw.styled(Style::Red | Style::Bold);
result.push_str(&styled);
}
is_command = false;
} else {
result.push_str(&raw);
}

View File

@@ -1,6 +1,6 @@
use crate::prelude::*;
use readline::SynHelper;
use rustyline::{config::Configurer, history::DefaultHistory, ColorMode, CompletionType, Config, DefaultEditor, EditMode, Editor};
use rustyline::{config::Configurer, history::{DefaultHistory, History}, ColorMode, CompletionType, Config, DefaultEditor, EditMode, Editor};
pub mod readline;
pub mod highlight;
@@ -27,10 +27,19 @@ fn init_rl<'a>(shenv: &'a mut ShEnv) -> Editor<SynHelper<'a>, DefaultHistory> {
pub fn read_line<'a>(shenv: &'a mut ShEnv) -> ShResult<String> {
log!(TRACE, "Entering prompt");
let prompt = &style_text("$ ", Style::Green | Style::Bold);
let prompt = "$ ".styled(Style::Green | Style::Bold);
let mut editor = init_rl(shenv);
match editor.readline(prompt) {
Ok(line) => Ok(line),
match editor.readline(&prompt) {
Ok(line) => {
if !line.is_empty() {
let hist_path = std::env::var("FERN_HIST").ok();
editor.history_mut().add(&line).unwrap();
if let Some(path) = hist_path {
editor.history_mut().save(&PathBuf::from(path)).unwrap();
}
}
Ok(line)
},
Err(rustyline::error::ReadlineError::Eof) => {
kill(Pid::this(), Signal::SIGQUIT)?;
Ok(String::new())

View File

@@ -54,7 +54,7 @@ pub struct SynHint {
impl SynHint {
pub fn new(text: String) -> Self {
let styled = style_text(&text, Style::BrightBlack);
let styled = (&text).styled(Style::BrightBlack);
Self { text, styled }
}
pub fn empty() -> Self {
@@ -78,7 +78,6 @@ impl Hint for SynHint {
impl<'a> Hinter for SynHelper<'a> {
type Hint = SynHint;
fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<Self::Hint> {
return Some(SynHint::empty());
if line.is_empty() {
return None
}