aboutsummaryrefslogtreecommitdiff
path: root/src/console_utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/console_utils.rs')
-rw-r--r--src/console_utils.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/console_utils.rs b/src/console_utils.rs
new file mode 100644
index 0000000..3e673fc
--- /dev/null
+++ b/src/console_utils.rs
@@ -0,0 +1,64 @@
+pub mod debug_console {
+ use clap::{Command, FromArgMatches};
+ use clap::ColorChoice::Auto;
+ use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
+
+ pub async fn read_cli<Cmd>(prefix: &str, entry: String, cmd: Command) -> Option<Cmd>
+ where
+ Cmd: FromArgMatches,
+ {
+ let input: String = {
+ let mut buffer = String::new();
+ let mut stdin = tokio::io::BufReader::new(tokio::io::stdin());
+ let mut stdout = tokio::io::stdout();
+
+ stdout.write_all(prefix.as_bytes()).await.ok().unwrap();
+ stdout.flush().await.ok().unwrap();
+
+ stdin.read_line(&mut buffer).await.ok().unwrap();
+ buffer.trim().to_string()
+ };
+
+ process_debug_cli(entry, input, cmd).await
+ }
+
+ async fn process_debug_cli<Cmd>(entry: String, input: String, cmd: Command) -> Option<Cmd>
+ where
+ Cmd: FromArgMatches
+ {
+ if input.trim().is_empty() {
+ return None;
+ }
+
+ let cmd = cmd
+ .color(Auto)
+ .help_template(
+ "{subcommands}{options}"
+ )
+ .disable_help_flag(true)
+ .disable_version_flag(true);
+
+ let args = shell_words::split(input.as_str()).unwrap_or_else(|_e| {
+ ["".to_string()].to_vec()
+ });
+
+ let full_args = std::iter::once(entry.into()).chain(args);
+
+ match cmd.try_get_matches_from(full_args) {
+ Ok(matches) => {
+ match Cmd::from_arg_matches(&matches) {
+ Ok(cmd) => {
+ Some(cmd)
+ }
+ Err(_err) => {
+ None
+ }
+ }
+ }
+ Err(err) => {
+ println!("{}", err);
+ None
+ }
+ }
+ }
+}