summaryrefslogtreecommitdiffstats
path: root/src/repl.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-11-10 01:03:11 -0500
committerJesse Luehrs <doy@tozt.net>2021-11-10 01:03:11 -0500
commitd0c26780d93a14be4d70bf2c784b79b3b0f06186 (patch)
tree9008ea7fa0b7aad9e5d19515dbcf758fd0436c11 /src/repl.rs
parentfbc4db18ecea9755199b945368c02f32c0481c95 (diff)
downloadnbsh-d0c26780d93a14be4d70bf2c784b79b3b0f06186.tar.gz
nbsh-d0c26780d93a14be4d70bf2c784b79b3b0f06186.zip
refactor
Diffstat (limited to 'src/repl.rs')
-rw-r--r--src/repl.rs58
1 files changed, 43 insertions, 15 deletions
diff --git a/src/repl.rs b/src/repl.rs
index 258718e..223aa80 100644
--- a/src/repl.rs
+++ b/src/repl.rs
@@ -3,30 +3,42 @@ use textmode::Textmode as _;
pub struct Repl {
prompt: String,
input_line: String,
+ action: async_std::channel::Sender<crate::nbsh::Action>,
}
impl Repl {
- pub fn new() -> Self {
+ pub fn new(
+ action: async_std::channel::Sender<crate::nbsh::Action>,
+ ) -> Self {
Self {
prompt: "$ ".into(),
input_line: "".into(),
+ action,
}
}
- pub fn input(&self) -> String {
- self.input_line.clone()
- }
-
- pub fn add_input(&mut self, s: &str) {
- self.input_line.push_str(s);
- }
-
- pub fn backspace(&mut self) {
- self.input_line.pop();
- }
-
- pub fn clear_input(&mut self) {
- self.input_line.clear();
+ 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::nbsh::Action::Run(self.input()))
+ .await
+ .unwrap();
+ self.clear_input();
+ }
+ textmode::Key::Backspace => self.backspace(),
+ _ => {}
+ }
+ self.action.send(crate::nbsh::Action::Render).await.unwrap();
+ false
}
pub fn lines(&self) -> usize {
@@ -42,4 +54,20 @@ impl Repl {
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();
+ }
}