From f88f2e8550c335f856c13e89fc85e4c74aedbac1 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 6 Jun 2019 22:36:44 -0700 Subject: [PATCH] More help cleanup --- schala-repl/src/repl/help.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/schala-repl/src/repl/help.rs b/schala-repl/src/repl/help.rs index 1e51cad..89cf33d 100644 --- a/schala-repl/src/repl/help.rs +++ b/schala-repl/src/repl/help.rs @@ -1,5 +1,6 @@ use std::fmt::Write as FmtWrite; +use super::command_tree::CommandTree; use super::{Repl, InterpreterDirectiveOutput}; pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { @@ -7,26 +8,28 @@ pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { [] => return global_help(repl), commands => { let dirs = repl.get_directives(); - let mut directive_list = dirs.get_children(); - let mut matched_directive = None; - for cmd in commands { - let found = directive_list.iter().find(|directive| directive.get_cmd() == *cmd); - if let Some(dir) = found { - directive_list = dir.get_children(); - } - - matched_directive = found; - } - - Some(if let Some(dir) = matched_directive { - format!("{}", dir.get_help()) - } else { - format!("Last command not found") + Some(match get_directive_from_commands(commands, &dirs) { + None => format!("Directive `{}` not found", commands.last().unwrap()), + Some(dir) => format!("`{}` - {}", dir.get_cmd(), dir.get_help()) }) } } } +fn get_directive_from_commands<'a>(commands: &[&str], dirs: &'a CommandTree) -> Option<&'a CommandTree> { + let mut directive_list = dirs.get_children(); + let mut matched_directive = None; + for cmd in commands { + let found = directive_list.iter().find(|directive| directive.get_cmd() == *cmd); + if let Some(dir) = found { + directive_list = dir.get_children(); + } + + matched_directive = found; + } + matched_directive +} + fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput { let mut buf = String::new();