summaryrefslogtreecommitdiffstats
path: root/src/readline.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-11-10 13:14:44 -0500
committerJesse Luehrs <doy@tozt.net>2021-11-10 13:14:44 -0500
commit747da41fd3f192038c2e3194b4de041f7969f7a8 (patch)
tree5436c8b77a9bfcbefc90885513ea4ed0ec5b5d51 /src/readline.rs
parent847f8834ae051c1c6177e9166cac855ccff113f0 (diff)
downloadnbsh-747da41fd3f192038c2e3194b4de041f7969f7a8.tar.gz
nbsh-747da41fd3f192038c2e3194b4de041f7969f7a8.zip
simplify
Diffstat (limited to 'src/readline.rs')
-rw-r--r--src/readline.rs76
1 files changed, 76 insertions, 0 deletions
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<crate::state::Action>,
+}
+
+impl Readline {
+ pub fn new(
+ action: async_std::channel::Sender<crate::state::Action>,
+ ) -> 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();
+ }
+}