diff options
| author | Jonas Maier <> | 2026-05-24 09:47:29 +0200 |
|---|---|---|
| committer | Jonas Maier <> | 2026-05-24 09:47:29 +0200 |
| commit | 36cbfcaafcb8a3b1c47650439531a35c99b203ea (patch) | |
| tree | 975b49ab8893a1ec1069d0a775f1703576247dd3 /src/lib.rs | |
| parent | 7cd15fe67ca0118520dcc4e9e189d513b0375e10 (diff) | |
| download | pish-36cbfcaafcb8a3b1c47650439531a35c99b203ea.tar.gz | |
terminal prompt from ps1 module now
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 46 |
1 files changed, 36 insertions, 10 deletions
@@ -45,6 +45,7 @@ use crate::ctrlc::CtrlC; use crate::history::HistoryEntry; use crate::line::Line; use crate::parse::{Block, ExpString, Parse, PostExpansion}; +use crate::ps1::Prompt; macro_rules! print { ($($x:tt)*) => {{ @@ -112,24 +113,26 @@ pub struct Session { ascii_keybinds: HashMap<BString, parse::Command<PostExpansion>>, debug_keystrokes: bool, - loud: bool, /// n before end of history.len() /// 0 == not checking history history_visit: usize, highlighter: syntax_highlighting::Highlighter, + prompt: Prompt, } impl Session { pub fn new_noninteractive() -> Self { + let mut vars = run::Vars::default(); + let prompt = Prompt::new(&mut vars); + Self { raw: None, line: Line::new(), history: Vec::new(), prev_path: b".".into(), builtins: HashMap::new(), - vars: run::Vars::default(), funs: HashMap::new(), aliases: run::Aliases::new(), socket_running: None, @@ -138,9 +141,10 @@ impl Session { ti_keybinds: HashMap::new(), ascii_keybinds: HashMap::new(), debug_keystrokes: false, - loud: false, history_visit: 0, highlighter: syntax_highlighting::Highlighter::new(), + prompt, + vars, } } } @@ -183,17 +187,14 @@ fn pretty_cwd() -> BString { } impl Session { - fn prompt(_this: Arc<Mutex<Self>>) -> BString { - todo!("replace with ps1 module"); - } - fn prompt_clear(&mut self) { self.line.clear_prompt().unwrap(); self.history_visit = 0; } fn reprint_prompt(this: Arc<Mutex<Self>>) { - io::stdout().write_all(&Self::prompt(this.clone())).unwrap(); + let prompt = this.lock().unwrap().prompt.prompt().to_vec(); + io::stdout().write_all(&prompt).unwrap(); let mut this = this.lock().unwrap(); this.line.display_pre(); this.line.display_post(b""); @@ -271,6 +272,23 @@ impl Session { } } + fn reevaluate_prompt_inner(&mut self) -> bool { + if self.prompt.requires_update(&self.vars) { + self.prompt.load_prompt(&mut self.vars); + true + } else { + false + } + } + + fn reevaluate_prompt(se: Arc<Mutex<Session>>) { + if se.lock().unwrap().reevaluate_prompt_inner() { + let mut prompt = se.lock().unwrap().prompt.clone(); + prompt.eval_prompt(se.clone()); + se.lock().unwrap().prompt = prompt; + } + } + fn try_submit_command(session: Arc<Mutex<Session>>) { let mut se = session.lock().unwrap(); let line = se.line.into_bytes(); @@ -291,7 +309,14 @@ impl Session { se.history_visit = 0; se.line.dump(); drop(se); - run::run(session.clone(), parsed); + + let status_string = run::run(session.clone(), parsed); + if !status_string.is_empty() { + println!("{status_string}"); + } + Self::reevaluate_prompt(session.clone()); + Self::reprint_prompt(session); + let _ = std::io::stdout().lock().flush(); } } @@ -371,7 +396,8 @@ pub fn event_loop() { let session = Arc::new(Mutex::new(se)); exec_rc_file(session.clone()); - session.lock().unwrap().loud = true; + Session::reevaluate_prompt(session.clone()); + Session::reprint_prompt(session.clone()); completion::populate_path_cache(session.clone()); |
