mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
add framework for modular commands
I am not happy with this. I think the framework could be better but I also can't figure out how.
This commit is contained in:
parent
fbcb991e06
commit
e829c0bd06
3 changed files with 124 additions and 16 deletions
78
services/shellchat/src/cmds.rs
Normal file
78
services/shellchat/src/cmds.rs
Normal file
|
|
@ -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<Option<String::<1024>>, xous::Error>;
|
||||||
|
// returns my verb
|
||||||
|
fn verb(&self) -> &str;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tokenize(line: &mut String::<1024>) -> Option<String::<1024>> {
|
||||||
|
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<Option<String::<1024>>, 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
services/shellchat/src/cmds/echo.rs
Normal file
25
services/shellchat/src/cmds/echo.rs
Normal file
|
|
@ -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<Option<String::<1024>>, xous::Error> {
|
||||||
|
Ok(Some(rest))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verb(&self) -> &str {
|
||||||
|
VERB
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,6 +16,9 @@ use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, Gl
|
||||||
use heapless::spsc::Queue;
|
use heapless::spsc::Queue;
|
||||||
use heapless::consts::U16;
|
use heapless::consts::U16;
|
||||||
|
|
||||||
|
mod cmds;
|
||||||
|
use cmds::*;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct History {
|
struct History {
|
||||||
// the history record
|
// the history record
|
||||||
|
|
@ -43,6 +46,9 @@ struct Repl {
|
||||||
bubble_margin: Point, // margin of text in bubbles
|
bubble_margin: Point, // margin of text in bubbles
|
||||||
bubble_radius: u16,
|
bubble_radius: u16,
|
||||||
bubble_space: i16, // spacing between text bubbles
|
bubble_space: i16, // spacing between text bubbles
|
||||||
|
|
||||||
|
// command environment
|
||||||
|
env: CmdEnv,
|
||||||
}
|
}
|
||||||
impl Repl {
|
impl Repl {
|
||||||
fn new(my_server_name: &str) -> Self {
|
fn new(my_server_name: &str) -> Self {
|
||||||
|
|
@ -60,6 +66,7 @@ impl Repl {
|
||||||
bubble_margin: Point::new(4, 4),
|
bubble_margin: Point::new(4, 4),
|
||||||
bubble_radius: 4,
|
bubble_radius: 4,
|
||||||
bubble_space: 4,
|
bubble_space: 4,
|
||||||
|
env: CmdEnv::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -93,26 +100,25 @@ impl Repl {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AT THIS POINT: if we have other inputs, update accordingly
|
// 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");
|
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
|
// take the input and pass it on to the various command parsers, and attach result
|
||||||
// (PLACEHOLDER)
|
if let Some(mut local) = self.input {
|
||||||
if let Some(local) = self.input {
|
if let Some(res) = self.env.dispatch(&mut local).expect("SHCH: command dispatch failed") {
|
||||||
let mut response = xous::String::<1024>::new();
|
let mut response = xous::String::<1024>::new();
|
||||||
write!(response, "Command not recognized: {}", local.as_str().unwrap()).unwrap();
|
write!(response, "{}", res).expect("SHCH: can't copy result to history");
|
||||||
let output_history = History {
|
let output_history = History {
|
||||||
text: response,
|
text: response,
|
||||||
is_input: false
|
is_input: false
|
||||||
};
|
};
|
||||||
self.circular_push(output_history);
|
self.circular_push(output_history);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// clear all the inputs to the loop, so we don't process them twice
|
// clear all the inputs to the loop, so we don't process them twice
|
||||||
self.input = None;
|
self.input = None;
|
||||||
// redraw UI now that we've responded
|
// 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);
|
info!("SHCH: command history is_input: {}, text:{}", h.is_input, h.text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_area(&self) {
|
fn clear_area(&self) {
|
||||||
gam::draw_rectangle(self.gam, self.content,
|
gam::draw_rectangle(self.gam, self.content,
|
||||||
Rectangle::new_with_style(Point::new(0, 0), self.screensize,
|
Rectangle::new_with_style(Point::new(0, 0), self.screensize,
|
||||||
DrawStyle {
|
DrawStyle {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue