diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs new file mode 100644 index 000000000..e6734c7f2 --- /dev/null +++ b/services/shellchat/src/cmds.rs @@ -0,0 +1,78 @@ +mod echo; +use echo::*; + +use xous::String; +use core::fmt::Write; + +pub trait ShellCmdApi { + // checks if the command matches the current verb in question + fn matches(&self, verb: &str) -> bool; + // called to process the command with the remainder of the string attached + fn process(&self, rest: String::<1024>) -> Result>, xous::Error>; + // returns my verb + fn verb(&self) -> &str; +} + +pub fn tokenize(line: &mut String::<1024>) -> Option> { + let mut token = String::<1024>::new(); + let mut retline = String::<1024>::new(); + + let lineiter = line.as_str().unwrap().chars(); + let mut foundspace = false; + let mut foundrest = false; + for ch in lineiter { + if ch != ' ' && !foundspace { + token.push(ch).unwrap(); + } else if foundspace && foundrest { + retline.push(ch).unwrap(); + } else if foundspace && ch != ' ' { + // handle case of multiple spaces in a row + foundrest = true; + retline.push(ch).unwrap(); + } else { + foundspace = true; + // consume the space + } + } + line.clear(); + write!(line, "{}", retline.as_str().unwrap()).unwrap(); + if token.len() > 0 { + Some(token) + } else { + None + } +} + +#[derive(Debug)] +pub struct CmdEnv { +} + +impl CmdEnv { + pub fn new() -> CmdEnv { + CmdEnv { + + } + } + + pub fn dispatch(&self, cmdline: &mut String::<1024>) -> Result>, xous::Error> { + let mut ret = String::<1024>::new(); + + let echo = Echo {}; + + let maybe_verb = tokenize(cmdline); + + if let Some(verb_string) = maybe_verb { + let verb = verb_string.to_str(); + if echo.matches(verb) { + echo.process(*cmdline) + } else { + write!(ret, "Commands: ").unwrap(); + ret.append(echo.verb())?; + + Ok(Some(ret)) + } + } else { + Ok(None) + } + } +} \ No newline at end of file diff --git a/services/shellchat/src/cmds/echo.rs b/services/shellchat/src/cmds/echo.rs new file mode 100644 index 000000000..8365692c8 --- /dev/null +++ b/services/shellchat/src/cmds/echo.rs @@ -0,0 +1,25 @@ +use crate::ShellCmdApi; +use xous::String; + +pub struct Echo { +} + +const VERB: &str = "echo"; + +impl ShellCmdApi for Echo { + fn matches(&self, verb: &str) -> bool { + if verb == VERB { + true + } else { + false + } + } + + fn process(&self, rest: String::<1024>) -> Result>, xous::Error> { + Ok(Some(rest)) + } + + fn verb(&self) -> &str { + VERB + } +} \ No newline at end of file diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index 5691accfa..cecd1f839 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -16,6 +16,9 @@ use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, Gl use heapless::spsc::Queue; use heapless::consts::U16; +mod cmds; +use cmds::*; + #[derive(Debug)] struct History { // the history record @@ -43,6 +46,9 @@ struct Repl { bubble_margin: Point, // margin of text in bubbles bubble_radius: u16, bubble_space: i16, // spacing between text bubbles + + // command environment + env: CmdEnv, } impl Repl { fn new(my_server_name: &str) -> Self { @@ -60,6 +66,7 @@ impl Repl { bubble_margin: Point::new(4, 4), bubble_radius: 4, bubble_space: 4, + env: CmdEnv::new(), } } @@ -93,26 +100,25 @@ impl Repl { } // AT THIS POINT: if we have other inputs, update accordingly - // (PLACEHOLDER) + // other inputs might be, for example, events that came in from other servers that would + // side effect our commands - // redraw UI once upon accepting input + // redraw UI once upon accepting all input self.redraw().expect("SHCH: can't redraw"); - - ////// AT THIS POINT // take the input and pass it on to the various command parsers, and attach result - // (PLACEHOLDER) - if let Some(local) = self.input { - let mut response = xous::String::<1024>::new(); - write!(response, "Command not recognized: {}", local.as_str().unwrap()).unwrap(); - let output_history = History { - text: response, - is_input: false - }; - self.circular_push(output_history); + if let Some(mut local) = self.input { + if let Some(res) = self.env.dispatch(&mut local).expect("SHCH: command dispatch failed") { + let mut response = xous::String::<1024>::new(); + write!(response, "{}", res).expect("SHCH: can't copy result to history"); + let output_history = History { + text: response, + is_input: false + }; + self.circular_push(output_history); + } } - // clear all the inputs to the loop, so we don't process them twice self.input = None; // redraw UI now that we've responded @@ -123,11 +129,10 @@ impl Repl { info!("SHCH: command history is_input: {}, text:{}", h.is_input, h.text); } } - Ok(()) } - pub fn clear_area(&self) { + fn clear_area(&self) { gam::draw_rectangle(self.gam, self.content, Rectangle::new_with_style(Point::new(0, 0), self.screensize, DrawStyle {