2023-03-01 11:14:42 -08:00
|
|
|
/* relish: versatile lisp shell
|
|
|
|
|
* Copyright (C) 2021 Aidan Hahn
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2023-03-15 21:55:10 -07:00
|
|
|
use nu_ansi_term::{Color, Style};
|
2023-03-01 11:14:42 -08:00
|
|
|
use dirs::home_dir;
|
2023-03-20 19:00:30 -07:00
|
|
|
use relish::ast::{eval, lex, Ctr, Seg, SymTable, run, load_defaults, load_environment};
|
2023-03-01 11:38:02 -08:00
|
|
|
use relish::stdlib::{dynamic_stdlib, static_stdlib};
|
2023-03-15 21:55:10 -07:00
|
|
|
use reedline::{
|
|
|
|
|
FileBackedHistory, DefaultHinter, DefaultValidator, Reedline, Signal,
|
|
|
|
|
Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus,
|
|
|
|
|
};
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
|
|
|
2023-03-01 11:14:42 -08:00
|
|
|
use std::env;
|
|
|
|
|
|
2023-03-15 21:55:10 -07:00
|
|
|
#[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())
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-01 11:14:42 -08:00
|
|
|
|
2023-03-15 21:55:10 -07:00
|
|
|
fn render_prompt_right(&self) -> Cow<str> {
|
|
|
|
|
{
|
2023-03-20 23:11:29 -07:00
|
|
|
Cow::Owned(format!(""))
|
2023-03-15 21:55:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 19:00:30 -07:00
|
|
|
fn main() {
|
2023-03-01 11:14:42 -08:00
|
|
|
const HIST_FILE: &str = "/.relish_hist";
|
|
|
|
|
const CONFIG_FILE_DEFAULT: &str = "/.relishrc";
|
|
|
|
|
|
2023-03-20 19:00:30 -07:00
|
|
|
// default config file dirs
|
2023-03-01 11:14:42 -08:00
|
|
|
let home_dir = home_dir().unwrap().to_str().unwrap().to_owned();
|
|
|
|
|
let hist_file_name = home_dir.clone() + HIST_FILE;
|
|
|
|
|
let cfg_file_name = home_dir + CONFIG_FILE_DEFAULT;
|
|
|
|
|
|
2023-03-20 19:00:30 -07:00
|
|
|
// setup symtable
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
load_defaults(&mut syms);
|
|
|
|
|
load_environment(&mut syms);
|
|
|
|
|
static_stdlib(&mut syms).unwrap_or_else(|err: String| eprintln!("{}", err));
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap_or_else(|err: String| eprintln!("{}", err));
|
|
|
|
|
|
|
|
|
|
// if there are args those are scripts, run them and exit
|
|
|
|
|
if env::args().count() > 1 {
|
|
|
|
|
let mut iter = env::args();
|
|
|
|
|
iter.next();
|
|
|
|
|
for i in iter {
|
|
|
|
|
run(i, &mut syms).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this is a user shell. attempt to load configuration
|
|
|
|
|
{
|
|
|
|
|
// scope the below borrow of syms
|
|
|
|
|
let cfg_file = env::var("RELISH_CFG_FILE").unwrap_or(cfg_file_name);
|
|
|
|
|
run(cfg_file.clone(), &mut syms)
|
|
|
|
|
.unwrap_or_else(|err: String| eprintln!("failed to load script {}\n{}", cfg_file, err));
|
|
|
|
|
}
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap_or_else(|err: String| eprintln!("{}", err));
|
|
|
|
|
|
|
|
|
|
// setup readline
|
2023-03-15 21:55:10 -07:00
|
|
|
let mut rl = Reedline::create();
|
|
|
|
|
let maybe_hist: Box<FileBackedHistory>;
|
2023-03-01 11:14:42 -08:00
|
|
|
if !hist_file_name.is_empty() {
|
2023-03-17 13:06:27 -07:00
|
|
|
maybe_hist = Box::new(FileBackedHistory::with_file(5000, hist_file_name.into())
|
2023-03-15 21:55:10 -07:00
|
|
|
.expect("error reading history!"));
|
|
|
|
|
rl = rl.with_history(maybe_hist);
|
2023-03-01 11:14:42 -08:00
|
|
|
}
|
2023-03-15 21:55:10 -07:00
|
|
|
rl = rl.with_hinter(Box::new(
|
|
|
|
|
DefaultHinter::default()
|
|
|
|
|
.with_style(Style::new().italic().fg(Color::LightGray)),
|
|
|
|
|
)).with_validator(Box::new(DefaultValidator));
|
|
|
|
|
|
2023-03-20 19:00:30 -07:00
|
|
|
// repl :)
|
2023-03-01 11:14:42 -08:00
|
|
|
loop {
|
2023-03-01 11:38:02 -08:00
|
|
|
let s = *syms
|
|
|
|
|
.call_symbol(&"CFG_RELISH_PROMPT".to_string(), &Seg::new(), true)
|
2023-03-01 11:14:42 -08:00
|
|
|
.unwrap_or_else(|err: String| {
|
|
|
|
|
eprintln!("{}", err);
|
|
|
|
|
Box::new(Ctr::String("<prompt broken!>".to_string()))
|
|
|
|
|
});
|
2023-03-20 23:02:33 -07:00
|
|
|
let p_str: String;
|
|
|
|
|
if let Ctr::String(s) = s {
|
|
|
|
|
p_str = s;
|
|
|
|
|
} else {
|
|
|
|
|
p_str = s.to_string();
|
|
|
|
|
}
|
2023-03-15 21:55:10 -07:00
|
|
|
let readline_prompt = CustomPrompt(p_str.as_str());
|
2023-03-01 11:14:42 -08:00
|
|
|
|
2023-03-15 21:55:10 -07:00
|
|
|
let user_doc = rl.read_line(&readline_prompt).unwrap();
|
2023-03-01 11:14:42 -08:00
|
|
|
match user_doc {
|
2023-03-15 21:55:10 -07:00
|
|
|
Signal::Success(line) => {
|
2023-03-16 15:14:24 -07:00
|
|
|
println!(""); // add a new line before output gets printed
|
2023-03-05 22:21:18 -08:00
|
|
|
let l = line.as_str().to_owned();
|
2023-03-01 11:14:42 -08:00
|
|
|
match lex(&l) {
|
|
|
|
|
Ok(a) => match eval(&a, &mut syms) {
|
|
|
|
|
Ok(a) => println!("{}", a),
|
|
|
|
|
Err(s) => println!("{}", s),
|
|
|
|
|
},
|
|
|
|
|
Err(s) => println!("{}", s),
|
|
|
|
|
}
|
2023-03-15 21:55:10 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
Signal::CtrlD => {
|
|
|
|
|
println!("EOF!");
|
|
|
|
|
panic!();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
Signal::CtrlC => {
|
|
|
|
|
println!("Interrupted!");
|
|
|
|
|
},
|
2023-03-01 11:14:42 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|