mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
refactor command API
should be a bit more convenient now to add/modify command lists as time goes on
This commit is contained in:
parent
ace4ea7be9
commit
9566355f9a
3 changed files with 92 additions and 18 deletions
|
|
@ -1,18 +1,19 @@
|
|||
mod echo;
|
||||
use echo::*;
|
||||
|
||||
use xous::String;
|
||||
use core::fmt::Write;
|
||||
|
||||
pub trait ShellCmdApi {
|
||||
pub trait ShellCmdApi<'a> {
|
||||
// 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>;
|
||||
fn process(&mut self, rest: String::<1024>) -> Result<Option<String::<1024>>, xous::Error>;
|
||||
// returns my verb
|
||||
fn verb(&self) -> &str;
|
||||
fn verb(&self) -> &'static str;
|
||||
}
|
||||
|
||||
/// extract the first token, as delimited by spaces
|
||||
/// modifies the incoming line by removing the token and returning the remainder
|
||||
/// returns the found token
|
||||
/// note: we don't have split() because of nostd
|
||||
pub fn tokenize(line: &mut String::<1024>) -> Option<String::<1024>> {
|
||||
let mut token = String::<1024>::new();
|
||||
let mut retline = String::<1024>::new();
|
||||
|
|
@ -43,33 +44,68 @@ pub fn tokenize(line: &mut String::<1024>) -> Option<String::<1024>> {
|
|||
}
|
||||
}
|
||||
|
||||
mod echo;
|
||||
use echo::*;
|
||||
mod test;
|
||||
use test::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CmdEnv {
|
||||
// placeholder for any environment variables relevant to interpreting commands
|
||||
}
|
||||
|
||||
impl CmdEnv {
|
||||
pub fn new() -> CmdEnv {
|
||||
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 {};
|
||||
/*
|
||||
To add a new command:
|
||||
- ensure that the command implements the ShellCmdApi
|
||||
- mod/use the new command (above)
|
||||
- create a mutable version of the command's data structure
|
||||
- add the structure to the array all_commands below
|
||||
*/
|
||||
let mut echo_cmd = Echo {};
|
||||
let mut test_cmd = Test::new();
|
||||
let all_commands: &mut [&mut dyn ShellCmdApi] = &mut [
|
||||
&mut echo_cmd,
|
||||
&mut test_cmd,
|
||||
];
|
||||
|
||||
let maybe_verb = tokenize(cmdline);
|
||||
|
||||
let mut cmd_ret: Result<Option<String::<1024>>, xous::Error> = Ok(None);
|
||||
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())?;
|
||||
|
||||
// search through the list of commands linearly until one matches,
|
||||
// then run it.
|
||||
let mut match_found = false;
|
||||
for cmd in all_commands.iter_mut() {
|
||||
if cmd.matches(verb) {
|
||||
match_found = true;
|
||||
cmd_ret = cmd.process(*cmdline);
|
||||
};
|
||||
}
|
||||
|
||||
// if none match, create a list of available commands
|
||||
if !match_found {
|
||||
let mut first = true;
|
||||
write!(ret, "Commands: ").unwrap();
|
||||
for cmd in all_commands.iter() {
|
||||
if !first {
|
||||
ret.append(", ")?;
|
||||
first = false;
|
||||
}
|
||||
ret.append(cmd.verb())?;
|
||||
}
|
||||
Ok(Some(ret))
|
||||
} else {
|
||||
cmd_ret
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ pub struct Echo {
|
|||
|
||||
const VERB: &str = "echo";
|
||||
|
||||
impl ShellCmdApi for Echo {
|
||||
impl<'a> ShellCmdApi<'a> for Echo {
|
||||
fn matches(&self, verb: &str) -> bool {
|
||||
if verb == VERB {
|
||||
true
|
||||
|
|
@ -15,11 +15,11 @@ impl ShellCmdApi for Echo {
|
|||
}
|
||||
}
|
||||
|
||||
fn process(&self, rest: String::<1024>) -> Result<Option<String::<1024>>, xous::Error> {
|
||||
fn process(&mut self, rest: String::<1024>) -> Result<Option<String::<1024>>, xous::Error> {
|
||||
Ok(Some(rest))
|
||||
}
|
||||
|
||||
fn verb(&self) -> &str {
|
||||
fn verb(&self) -> &'static str {
|
||||
VERB
|
||||
}
|
||||
}
|
||||
38
services/shellchat/src/cmds/test.rs
Normal file
38
services/shellchat/src/cmds/test.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use crate::ShellCmdApi;
|
||||
use xous::String;
|
||||
|
||||
pub struct Test {
|
||||
state: u32
|
||||
}
|
||||
impl Test {
|
||||
pub fn new() -> Self {
|
||||
Test {
|
||||
state: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const VERB: &str = "test";
|
||||
|
||||
impl<'a> ShellCmdApi<'a> for Test {
|
||||
fn matches(&self, verb: &str) -> bool {
|
||||
if verb == VERB {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn process(&mut self, _rest: String::<1024>) -> Result<Option<String::<1024>>, xous::Error> {
|
||||
use core::fmt::Write;
|
||||
|
||||
self.state += 1;
|
||||
let mut ret = String::<1024>::new();
|
||||
write!(ret, "Test has run {} times.", self.state).unwrap();
|
||||
Ok(Some(ret))
|
||||
}
|
||||
|
||||
fn verb(&self) -> &'static str {
|
||||
VERB
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue