From a042deb4629700dcab9686ea9fe1f5ad848e1cfc Mon Sep 17 00:00:00 2001 From: Tom Marble Date: Thu, 5 Jan 2023 08:34:05 -0600 Subject: [PATCH] WIP #287: Adds /heap command to mtxcli Signed-off-by: Tom Marble --- apps/mtxcli/locales/i18n.json | 7 ++++++ apps/mtxcli/src/cmds.rs | 46 +++++++++++++++++++++++------------ apps/mtxcli/src/cmds/heap.rs | 25 +++++++++++++++++++ apps/mtxcli/src/cmds/help.rs | 4 +++ 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 apps/mtxcli/src/cmds/heap.rs diff --git a/apps/mtxcli/locales/i18n.json b/apps/mtxcli/locales/i18n.json index 19509beb7..4c79128ee 100644 --- a/apps/mtxcli/locales/i18n.json +++ b/apps/mtxcli/locales/i18n.json @@ -20,6 +20,13 @@ "ja": "シンプルなMTXCLIデモへようこそ", "zh": "简单的 mtxcli 演示活动" }, + "mtxcli.heap.help": { + "en": "/heap", + "en-tts": "heap", + "fr": "/heap", + "ja": "/heap", + "zh": "/heap" + }, "mtxcli.help.help": { "en": "/help [cmd]", "en-tts": "help [cmd]", diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index 2559f73ad..eee2c6f0b 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -26,6 +26,8 @@ const SERVER_KEY: &str = "server"; const TOKEN_KEY: &str = "_token"; const USER_KEY: &str = "user"; const USERNAME_KEY: &str = "username"; +const VERSION_KEY: &str = "_version"; +const CURRENT_VERSION_KEY: &str = "__version"; const HTTPS: &str = "https://"; const SERVER_MATRIX: &str = "https://matrix.org"; @@ -203,23 +205,33 @@ impl CommonEnv { } } + pub fn xous_version(&self) -> String { + // Currently breaks in hosted mode: + // self.ticktimer.get_version() + "v0.9.11".to_string() + } + pub fn get(&mut self, key: &str) -> Result, Error> { - let mut keypath = PathBuf::new(); - keypath.push(MTXCLI_DICT); - if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + if key.eq(CURRENT_VERSION_KEY) { + Ok(Some(self.xous_version())) } else { - log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); - std::fs::create_dir_all(&keypath)?; - } - keypath.push(key); - if let Ok(mut file)= File::open(keypath) { - let mut value = String::new(); - file.read_to_string(&mut value)?; - // log::info!("get '{}' = '{}'", key, value); - Ok(Some(value)) - } else { - Ok(None) + let mut keypath = PathBuf::new(); + keypath.push(MTXCLI_DICT); + if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict '{}' exists", MTXCLI_DICT); + } else { + log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); + std::fs::create_dir_all(&keypath)?; + } + keypath.push(key); + if let Ok(mut file)= File::open(keypath) { + let mut value = String::new(); + file.read_to_string(&mut value)?; + // log::info!("get '{}' = '{}'", key, value); + Ok(Some(value)) + } else { + Ok(None) + } } } @@ -422,6 +434,7 @@ impl CommonEnv { ///// 1. add your module here, and pull its namespace into the local crate mod get; use get::*; +mod heap; use heap::*; mod help; use help::*; mod login; use login::*; mod logout; use logout::*; @@ -434,6 +447,7 @@ pub struct CmdEnv { lastverb: XousString::<256>, ///// 2. declare storage for your command here. get_cmd: Get, + heap_cmd: Heap, help_cmd: Help, login_cmd: Login, logout_cmd: Logout, @@ -471,6 +485,7 @@ impl CmdEnv { lastverb: XousString::<256>::new(), ///// 3. initialize your storage, by calling new() get_cmd: Get::new(&xns), + heap_cmd: Heap::new(&xns), help_cmd: Help::new(&xns), login_cmd: Login::new(&xns), logout_cmd: Logout::new(&xns), @@ -487,6 +502,7 @@ impl CmdEnv { let commands: &mut [& mut dyn ShellCmdApi] = &mut [ ///// 4. add your command to this array, so that it can be looked up and dispatched &mut self.get_cmd, + &mut self.heap_cmd, &mut self.help_cmd, &mut self.login_cmd, &mut self.logout_cmd, diff --git a/apps/mtxcli/src/cmds/heap.rs b/apps/mtxcli/src/cmds/heap.rs new file mode 100644 index 000000000..b8d71ce02 --- /dev/null +++ b/apps/mtxcli/src/cmds/heap.rs @@ -0,0 +1,25 @@ +use crate::{ShellCmdApi,CommonEnv,heap_usage}; +use xous_ipc::String as XousString; +use core::fmt::Write; + +#[derive(Debug)] +pub struct Heap { +} +impl Heap { + pub fn new(_xns: &xous_names::XousNames) -> Self { + Heap { + } + } +} + +impl<'a> ShellCmdApi<'a> for Heap { + cmd_api!(heap); + + fn process(&mut self, _args: XousString::<1024>, _env: &mut CommonEnv) -> Result>, xous::Error> { + let mut ret = XousString::<1024>::new(); + let heap = heap_usage(); + write!(ret, "heap usage: {}", heap).unwrap(); + log::info!("heap usage: {}", heap); + Ok(Some(ret)) + } +} diff --git a/apps/mtxcli/src/cmds/help.rs b/apps/mtxcli/src/cmds/help.rs index aa7602646..69a386923 100644 --- a/apps/mtxcli/src/cmds/help.rs +++ b/apps/mtxcli/src/cmds/help.rs @@ -30,6 +30,9 @@ impl<'a> ShellCmdApi<'a> for Help { "get" => { write!(ret, "{}", t!("mtxcli.get.help", xous::LANG)).unwrap(); } + "heap" => { + write!(ret, "{}", t!("mtxcli.heap.help", xous::LANG)).unwrap(); + } "help" => { write!(ret, "{}", t!("mtxcli.help.help", xous::LANG)).unwrap(); } @@ -51,6 +54,7 @@ impl<'a> ShellCmdApi<'a> for Help { "" => { write!(ret, "{}\n", t!("mtxcli.help.overview", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.get.help", xous::LANG)).unwrap(); + write!(ret, "{}\n", t!("mtxcli.heap.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.help.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.login.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.logout.help", xous::LANG)).unwrap();