aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs49
1 files changed, 43 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs
index 87fcfb4..83796b6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,8 +3,8 @@
use std::collections::HashMap;
use std::ffi::OsStr;
-use std::fs;
-use std::io::{self, IsTerminal, Read, Write};
+use std::fs::{self, File};
+use std::io::{self, IsTerminal, Read, Write, stdout};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::AsRawFd;
use std::path::Path;
@@ -39,7 +39,7 @@ use crate::completion::{PathCache, completion};
use crate::ctrlc::CtrlC;
use crate::cursor::{Direction, move_cursor};
use crate::history::HistoryEntry;
-use crate::parse::{Block, ExpString};
+use crate::parse::{Block, ExpString, Parse};
macro_rules! print {
($($x:tt)*) => {{
@@ -90,6 +90,7 @@ pub struct Session {
ctrlc: CtrlC,
debug_keystrokes: bool,
+ loud: bool,
/// n before end of history.len()
/// 0 == not checking history
@@ -274,6 +275,37 @@ impl Session {
}
}
+fn exec_rc_file(se: Arc<Mutex<Session>>) {
+ let rcfile = basedir::config_dir().join(".pishrc");
+ let mut rc = Vec::new();
+ if !rcfile.exists() {
+ return;
+ }
+
+ let mut f = match File::open(&rcfile) {
+ Ok(f) => f,
+ Err(e) => {
+ println!("failed to open {rcfile:?}: {e:?}");
+ return;
+ }
+ };
+
+ if let Err(e) = f.read_to_end(&mut rc) {
+ println!("failed to read {rcfile:?}: {e:?}");
+ return;
+ }
+
+ let script = match parse::Script::parse_from_bytes(&rc) {
+ Ok(s) => s,
+ Err(e) => {
+ println!("failed to parse rc file: {e:?}");
+ return;
+ }
+ };
+
+ run::run_script(se, script);
+}
+
fn event_loop() {
history::setup();
@@ -300,11 +332,16 @@ fn event_loop() {
path_cache: Default::default(),
ctrlc: Default::default(),
debug_keystrokes: false,
+ loud: false,
};
- print!("{}", se.prompt());
-
let session = Arc::new(Mutex::new(se));
+ exec_rc_file(session.clone());
+
+ session.lock().unwrap().loud = true;
+
+ print!("{}", session.lock().unwrap().prompt());
+
completion::populate_path_cache(session.clone());
let _sock_dropper = export_fun::listen(session.clone());
@@ -319,7 +356,7 @@ fn event_loop() {
Kb::CtrlB => {
println!(" Ctrl+B is not yet implemented");
se.reprint_prompt();
- },
+ }
Kb::CtrlC => {
se.clear_prompt();
se.history_visit = 0;