:help command working

This commit is contained in:
greg 2019-06-06 22:21:50 -07:00
parent 32d082e119
commit 7097775a4a
3 changed files with 28 additions and 17 deletions

View File

@ -54,14 +54,17 @@ impl CommandTree {
CommandTree::Top(_) => "" CommandTree::Top(_) => ""
} }
} }
pub fn get_children(&self) -> Vec<&str> { pub fn get_children(&self) -> &Vec<CommandTree> {
use CommandTree::*; use CommandTree::*;
match self { match self {
Terminal { children, .. } | Terminal { children, .. } |
NonTerminal { children, .. } | NonTerminal { children, .. } |
Top(children) => children.iter().map(|x| x.get_cmd()).collect() Top(children) => children
} }
} }
pub fn get_subcommands(&self) -> Vec<&str> {
self.get_children().iter().map(|x| x.get_cmd()).collect()
}
pub fn perform(&self, repl: &mut Repl, arguments: &Vec<&str>) -> InterpreterDirectiveOutput { pub fn perform(&self, repl: &mut Repl, arguments: &Vec<&str>) -> InterpreterDirectiveOutput {
let mut dir_pointer: &CommandTree = self; let mut dir_pointer: &CommandTree = self;

View File

@ -1,31 +1,39 @@
use std::fmt::Write as FmtWrite; use std::fmt::Write as FmtWrite;
use super::{Repl, InterpreterDirectiveOutput}; use super::{Repl, InterpreterDirectiveOutput};
use crate::repl::command_tree::CommandTree;
pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
let mut buf = String::new();
let directives = match repl.get_directives() {
CommandTree::Top(children) => children,
_ => panic!("Top-level CommandTree not Top")
};
match arguments { match arguments {
[] => return global_help(repl, &directives), [] => return global_help(repl),
_ => { commands => {
writeln!(buf, "Command-specific help not available yet").unwrap(); 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(buf)
} }
fn global_help(repl: &mut Repl, directives: &Vec<CommandTree>) -> InterpreterDirectiveOutput { fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
let mut buf = String::new(); let mut buf = String::new();
writeln!(buf, "MetaInterpreter options").unwrap(); writeln!(buf, "MetaInterpreter options").unwrap();
writeln!(buf, "-----------------------").unwrap(); writeln!(buf, "-----------------------").unwrap();
for directive in directives { for directive in repl.get_directives().get_children() {
let trailer = " "; let trailer = " ";
writeln!(buf, "{}{}- {}", directive.get_cmd(), trailer, directive.get_help()).unwrap(); writeln!(buf, "{}{}- {}", directive.get_cmd(), trailer, directive.get_help()).unwrap();
} }

View File

@ -220,7 +220,7 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
_ => false _ => false
}; };
let word = if top { word.get(1..).unwrap() } else { word }; let word = if top { word.get(1..).unwrap() } else { word };
for cmd in command_tree.map(|x| x.get_children()).unwrap_or(vec![]).into_iter() { for cmd in command_tree.map(|x| x.get_subcommands()).unwrap_or(vec![]).into_iter() {
if cmd.starts_with(word) { if cmd.starts_with(word) {
completions.push(Completion { completions.push(Completion {
completion: format!("{}{}", if top { ":" } else { "" }, cmd), completion: format!("{}{}", if top { ":" } else { "" }, cmd),