diff options
| -rw-r--r-- | src/main.rs | 132 | ||||
| -rw-r--r-- | src/run/builtin.rs | 14 |
2 files changed, 47 insertions, 99 deletions
diff --git a/src/main.rs b/src/main.rs index a21b313..5ec5a56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,7 +143,11 @@ impl Session { } fn prompt(&self) -> String { - format!("[{}]# ", self.pretty_cwd()) + #[cfg(debug_assertions)] + let dev = "dev "; + #[cfg(not(debug_assertions))] + let dev = ""; + format!("{dev}[{}]# ", self.pretty_cwd()) } fn clear_prompt(&mut self) { @@ -238,6 +242,10 @@ impl Session { io::stdout().flush().unwrap(); } + fn del_left_word(&mut self) { + todo!() + } + fn del_right_word(&mut self) { let mut del = 0; while let Some(x) = self.line.get_right() @@ -331,6 +339,34 @@ impl Session { } } + fn try_submit_command(session: Arc<Mutex<Session>>) { + let mut se = session.lock().unwrap(); + let line = se.line.into_bytes(); + + if !line.is_empty() { + let parsed = match parse::do_parse(&line) { + Ok(p) => p, + Err((crate::parse::ParseError::Eof, _)) => { + se.line.add(b'\n'); + print!("\r\n> "); + return; + } + Err(e) => { + println!("{e:?}\n{}", se.prompt()); + return; + } + }; + print!("\r\n"); + let entry = HistoryEntry::new(line.clone()); + history::persist(&entry); + se.history.push(entry); + se.history_visit = 0; + se.line.dump(); + drop(se); + run::run(session.clone(), parsed); + } + } + fn screen_clear(&mut self) { clear_screen(); self.reprint_prompt(); @@ -432,37 +468,6 @@ fn event_loop() { } match key { - // TODO: make simple characters also keybinds and do not specially handle tab or newline here. - ansi::KbInput::Key([b'\t']) => { - drop(se); - Session::complete(session.clone()); - } - ansi::KbInput::Key([b'\r' | b'\n']) => { - let line = se.line.into_bytes(); - - if !line.is_empty() { - let parsed = match parse::do_parse(&line) { - Ok(p) => p, - Err((crate::parse::ParseError::Eof, _)) => { - se.line.add(b'\n'); - print!("\r\n> "); - continue; - } - Err(e) => { - println!("{e:?}\n{}", se.prompt()); - continue; - } - }; - print!("\r\n"); - let entry = HistoryEntry::new(line.clone()); - history::persist(&entry); - se.history.push(entry); - se.history_visit = 0; - se.line.dump(); - drop(se); - run::run(session.clone(), parsed); - } - } ansi::KbInput::Key([x]) => se.type_byte(x), ansi::KbInput::Escape(escape) => { for terminfo_key in escape.keys.iter() { @@ -474,72 +479,9 @@ fn event_loop() { continue 'repl; } } - - if matches!(&escape.value[..], b"\r" | b"\n") { - let line = se.line.into_bytes(); - - if !line.is_empty() { - let parsed = match parse::do_parse(&line) { - Ok(p) => p, - Err((crate::parse::ParseError::Eof, _)) => { - se.line.add(b'\n'); - print!("\r\n> "); - continue; - } - Err(e) => { - println!("{e:?}\n{}", se.prompt()); - continue; - } - }; - print!("\r\n"); - let entry = HistoryEntry::new(line.clone()); - history::persist(&entry); - se.history.push(entry); - se.history_visit = 0; - se.line.dump(); - drop(se); - run::run(session.clone(), parsed); - } - } } ansi::KbInput::InvalidEscape(_) => continue, } - - /* - match key { - Kb::Arrow(dir) => match dir { - Direction::Up => se.history_up(), - Direction::Down => se.history_down(), - Direction::Left => { - if se.line.left() { - move_cursor(Direction::Left, 1); - io::stdout().lock().flush().unwrap(); - } - } - Direction::Right => { - if se.line.right() { - move_cursor(Direction::Right, 1); - io::stdout().lock().flush().unwrap(); - } - } - }, - Kb::CtrlArrow(dir) => match dir { - Direction::Left => se.move_left_word(), - Direction::Right => se.move_right_word(), - _ => { - println!(" Ctrl+{dir:?} not implemented"); - se.reprint_prompt(); - } - }, - Kb::DeleteLeft => { - } - Kb::DeleteRight => se.del_right(), - Kb::CtrlDeleteRight => se.del_right_word(), - Kb::Home => se.move_to_begin(), - Kb::End => se.move_to_end(), - Kb::Key(x) => se.type_byte(x), - } - */ } session.lock().unwrap().raw.disable(); diff --git a/src/run/builtin.rs b/src/run/builtin.rs index 56dda97..4a7e332 100644 --- a/src/run/builtin.rs +++ b/src/run/builtin.rs @@ -810,15 +810,21 @@ impl Builtin for ct { b"cursor_left_word" => se.cursor_left_word(), b"prompt_clear" => se.prompt_clear(), b"screen_clear" => se.screen_clear(), - b"complete" => { - drop(se); - Session::complete(session) - } b"history_previous" => se.history_up(), b"history_next" => se.history_down(), b"prompt_del_left" => se.del_left(), b"prompt_del_right" => se.del_right(), + b"prompt_del_left_word" => se.del_left_word(), + b"prompt_del_right_word" => se.del_right_word(), b"prompt_del_left_or_previous" => se.del_left_or_previous(), + b"complete" => { + drop(se); + Session::complete(session) + } + b"try_submit_command" => { + drop(se); + Session::try_submit_command(session); + } _ => return Err(Error::Exit(-2)), } |
