From 747da41fd3f192038c2e3194b4de041f7969f7a8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 10 Nov 2021 13:14:44 -0500 Subject: simplify --- src/readline.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/readline.rs (limited to 'src/readline.rs') diff --git a/src/readline.rs b/src/readline.rs new file mode 100644 index 0000000..8ff001c --- /dev/null +++ b/src/readline.rs @@ -0,0 +1,76 @@ +use textmode::Textmode as _; + +pub struct Readline { + prompt: String, + input_line: String, + action: async_std::channel::Sender, +} + +impl Readline { + pub fn new( + action: async_std::channel::Sender, + ) -> Self { + Self { + prompt: "$ ".into(), + input_line: "".into(), + action, + } + } + + pub async fn handle_key(&mut self, key: textmode::Key) -> bool { + match key { + textmode::Key::String(s) => self.add_input(&s), + textmode::Key::Char(c) => { + self.add_input(&c.to_string()); + } + textmode::Key::Ctrl(b'c') => self.clear_input(), + textmode::Key::Ctrl(b'd') => { + return true; + } + textmode::Key::Ctrl(b'm') => { + self.action + .send(crate::state::Action::Run(self.input())) + .await + .unwrap(); + self.clear_input(); + } + textmode::Key::Backspace => self.backspace(), + _ => {} + } + self.action + .send(crate::state::Action::Render) + .await + .unwrap(); + false + } + + pub fn lines(&self) -> usize { + 1 // XXX handle wrapping, multiline prompts + } + + pub async fn render( + &self, + out: &mut textmode::Output, + ) -> anyhow::Result<()> { + out.move_to(23, 0); + out.write_str(&self.prompt); + out.write_str(&self.input_line); + Ok(()) + } + + fn input(&self) -> String { + self.input_line.clone() + } + + fn add_input(&mut self, s: &str) { + self.input_line.push_str(s); + } + + fn backspace(&mut self) { + self.input_line.pop(); + } + + fn clear_input(&mut self) { + self.input_line.clear(); + } +} -- cgit v1.2.3-54-g00ecf