elementary shell behavior: can kick off a foreground process and

handle signals
This commit is contained in:
Ava Apples Affine 2023-03-24 18:14:33 -07:00
parent 3b1ae0efd5
commit 99cb9e5a2e
Signed by: affine
GPG key ID: 3A4645B8CF806069
17 changed files with 619 additions and 167 deletions

View file

@ -14,21 +14,27 @@
* 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, run, load_defaults, load_environment};
use relish::stdlib::{dynamic_stdlib, static_stdlib};
use relish::aux::ShellState;
use std::cell::RefCell;
use std::rc::Rc;
use std::borrow::Cow;
use std::env;
use nix::unistd;
use nu_ansi_term::{Color, Style};
use dirs::home_dir;
use reedline::{
FileBackedHistory, DefaultHinter, DefaultValidator, Reedline, Signal,
Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus,
};
use std::borrow::Cow;
use std::env;
#[derive(Clone)]
pub struct CustomPrompt(String, String, String);
impl Prompt for CustomPrompt {
fn render_prompt_left(&self) -> Cow<str> {
{
@ -75,12 +81,20 @@ fn main() {
let hist_file_name = home_dir.clone() + HIST_FILE;
let cfg_file_name = home_dir + CONFIG_FILE_DEFAULT;
let shell_state_bindings = Rc::new(RefCell::from(ShellState {
parent_pid: unistd::Pid::from_raw(0),
parent_pgid: unistd::Pid::from_raw(0),
children: vec![],
last_exit_code: 0,
}));
// 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));
// reload this later with the state bindings
dynamic_stdlib(&mut syms, None).unwrap_or_else(|err: String| eprintln!("{}", err));
// if there are args those are scripts, run them and exit
if env::args().count() > 1 {
@ -100,7 +114,7 @@ fn main() {
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));
dynamic_stdlib(&mut syms, Some(shell_state_bindings)).unwrap_or_else(|err: String| eprintln!("{}", err));
// setup readline
let mut rl = Reedline::create();
@ -134,7 +148,7 @@ fn main() {
Signal::CtrlD => {
println!("EOF!");
panic!();
return
},
Signal::CtrlC => {