multiline shell, yay!

This commit is contained in:
Ava Apples Affine 2023-03-15 21:55:10 -07:00
parent 5bdf409a1f
commit 67af8bbd47
Signed by: affine
GPG key ID: 3A4645B8CF806069
8 changed files with 76 additions and 48 deletions

View file

@ -14,18 +14,60 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use nu_ansi_term::{Color, Style};
use dirs::home_dir;
use relish::ast::{eval, lex, Ctr, Seg, SymTable};
use relish::aux::configure;
use relish::stdlib::{dynamic_stdlib, static_stdlib};
use rustyline::error::ReadlineError;
use rustyline::Editor;
use reedline::{
FileBackedHistory, DefaultHinter, DefaultValidator, Reedline, Signal,
Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus,
};
use std::borrow::Cow;
use std::env;
fn main() {
let mut rl = Editor::<()>::new();
#[derive(Clone)]
pub struct CustomPrompt<'a>(&'a str);
impl Prompt for CustomPrompt<'_> {
fn render_prompt_left(&self) -> Cow<str> {
{
Cow::Owned(self.0.to_string())
}
}
fn render_prompt_right(&self) -> Cow<str> {
{
Cow::Owned(format!(" <"))
}
}
fn render_prompt_indicator(&self, _edit_mode: PromptEditMode) -> Cow<str> {
Cow::Owned("> ".to_string())
}
fn render_prompt_multiline_indicator(&self) -> Cow<str> {
Cow::Borrowed("++++")
}
fn render_prompt_history_search_indicator(
&self,
history_search: PromptHistorySearch,
) -> Cow<str> {
let prefix = match history_search.status {
PromptHistorySearchStatus::Passing => "",
PromptHistorySearchStatus::Failing => "failing ",
};
Cow::Owned(format!(
"({}reverse-search: {}) ",
prefix, history_search.term
))
}
}
fn main() -> ! {
const HIST_FILE: &str = "/.relish_hist";
const CONFIG_FILE_DEFAULT: &str = "/.relishrc";
@ -33,11 +75,19 @@ fn main() {
let hist_file_name = home_dir.clone() + HIST_FILE;
let cfg_file_name = home_dir + CONFIG_FILE_DEFAULT;
let mut rl = Reedline::create();
let maybe_hist: Box<FileBackedHistory>;
if !hist_file_name.is_empty() {
rl.load_history(&hist_file_name)
.unwrap_or_else(|err: ReadlineError| eprintln!("{}", err));
maybe_hist = Box::new(FileBackedHistory::with_file(5, hist_file_name.into())
.expect("error reading history!"));
rl = rl.with_history(maybe_hist);
}
rl = rl.with_hinter(Box::new(
DefaultHinter::default()
.with_style(Style::new().italic().fg(Color::LightGray)),
)).with_validator(Box::new(DefaultValidator));
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap_or_else(|err: String| eprintln!("{}", err));
dynamic_stdlib(&mut syms).unwrap_or_else(|err: String| eprintln!("{}", err));
@ -56,12 +106,12 @@ fn main() {
eprintln!("{}", err);
Box::new(Ctr::String("<prompt broken!>".to_string()))
});
let readline_prompt = s.to_string();
let p_str = s.to_string();
let readline_prompt = CustomPrompt(p_str.as_str());
let user_doc = rl.readline(&readline_prompt);
let user_doc = rl.read_line(&readline_prompt).unwrap();
match user_doc {
Ok(line) => {
rl.add_history_entry(line.as_str());
Signal::Success(line) => {
let l = line.as_str().to_owned();
match lex(&l) {
@ -71,17 +121,16 @@ fn main() {
},
Err(s) => println!("{}", s),
}
}
},
Err(ReadlineError::Interrupted) => break,
Err(ReadlineError::Eof) => return,
Err(err) => {
eprintln!("Prompt error: {:?}", err);
break;
}
Signal::CtrlD => {
println!("EOF!");
panic!();
},
Signal::CtrlC => {
println!("Interrupted!");
},
}
}
if !hist_file_name.is_empty() {
rl.save_history(&hist_file_name).unwrap();
}
}

View file

@ -24,7 +24,7 @@ use std::io;
use std::rc::Rc;
fn prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
Ok(Ctr::Symbol("λ ".to_string()))
Ok(Ctr::Symbol("λ".to_string()))
}
/* loads defaults, evaluates config script */
@ -80,8 +80,6 @@ default value (<lambda>)"
config_document = "(".to_string() + &config_document;
let config_tree = lex(&config_document)?;
let config_result = eval(&config_tree, syms)?;
println!("config result: {config_result}");
eval(&config_tree, syms)?;
Ok(())
}

View file

@ -23,7 +23,6 @@ use crate::sym::{SymTable, call_lambda};
* representing the simplest possible form of the input
*/
pub fn eval(ast: &Seg, syms: &mut SymTable) -> Result<Box<Ctr>, String> {
println!("E: {}", ast);
// data to return
let mut ret = Box::from(Ctr::None);
let mut first = true;

View file

@ -101,7 +101,6 @@ pub const STORE_DOCSTRING: &str = "allows user to define functions and variables
(def useless-var)";
pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> {
println!("def: {}", ast);
let is_var = ast.len() == 3;
if let Ctr::Symbol(ref identifier) = *ast.car {
match &*ast.cdr {

View file

@ -281,7 +281,6 @@ impl Symbol {
evaluated_args = args;
}
println!("args: {}", evaluated_args);
self.args.validate_inputs(evaluated_args)?;
match &self.value {
ValueType::VarForm(ref f) => Ok(Box::new(*f.clone())),
@ -362,18 +361,15 @@ impl Symbol {
let value: ValueType;
if let Some(ref arg_syms) = arg_list {
println!("def a func form");
value = ValueType::FuncForm(UserFn{
ast: Box::new(ast.clone()),
arg_syms: arg_syms.clone(),
});
args = Args::Lazy(arg_syms.len() as u128);
} else if let Ctr::Lambda(ref l) = *ast.car {
println!("def a func form (lambda)");
args = Args::Lazy(l.arg_syms.len() as u128);
value = ValueType::FuncForm(l.clone());
} else {
println!("def a var form");
args = Args::None;
value = ValueType::VarForm(ast.car.clone());
}