use std::fmt::Write as FmtWrite; use super::{Repl, InterpreterDirectiveOutput}; pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { match arguments { [] => 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") }) } } } fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput { let mut buf = String::new(); writeln!(buf, "MetaInterpreter options").unwrap(); writeln!(buf, "-----------------------").unwrap(); for directive in repl.get_directives().get_children() { let trailer = " "; writeln!(buf, "{}{}- {}", directive.get_cmd(), trailer, directive.get_help()).unwrap(); } let ref lang = repl.get_cur_language_state(); writeln!(buf, "").unwrap(); writeln!(buf, "Language-specific help for {}", lang.get_language_name()).unwrap(); writeln!(buf, "-----------------------").unwrap(); Some(buf) }