schala/schala-repl/src/repl/help.rs

54 lines
1.7 KiB
Rust
Raw Normal View History

2019-06-04 15:02:51 -07:00
use std::fmt::Write as FmtWrite;
2019-06-06 23:50:08 -07:00
use colored::*;
2019-06-06 22:36:44 -07:00
use super::command_tree::CommandTree;
2019-06-04 15:02:51 -07:00
use super::{Repl, InterpreterDirectiveOutput};
pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
match arguments {
2019-06-06 22:21:50 -07:00
[] => return global_help(repl),
commands => {
let dirs = repl.get_directives();
2019-06-06 22:36:44 -07:00
Some(match get_directive_from_commands(commands, &dirs) {
None => format!("Directive `{}` not found", commands.last().unwrap()),
2019-06-06 23:50:08 -07:00
Some(dir) => {
let mut buf = String::new();
writeln!(buf, "`{}` - {}", dir.get_cmd(), dir.get_help()).unwrap();
buf
2019-06-06 22:21:50 -07:00
})
2019-06-04 15:02:51 -07:00
}
2019-06-06 22:21:50 -07:00
}
2019-06-04 15:02:51 -07:00
}
2019-06-06 22:36:44 -07:00
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
}
2019-06-06 22:21:50 -07:00
fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
2019-06-04 15:02:51 -07:00
let mut buf = String::new();
2019-06-06 23:50:08 -07:00
let sigil = repl.interpreter_directive_sigil;
2019-06-04 15:02:51 -07:00
2019-06-06 23:50:08 -07:00
writeln!(buf, "{} version {}", "Schala REPL".bright_red().bold(), crate::VERSION_STRING).unwrap();
2019-06-04 15:02:51 -07:00
writeln!(buf, "-----------------------").unwrap();
2019-06-06 22:21:50 -07:00
for directive in repl.get_directives().get_children() {
2019-06-06 23:50:08 -07:00
writeln!(buf, "{}{} - {}", sigil, directive.get_cmd(), directive.get_help()).unwrap();
2019-06-04 15:02:51 -07:00
}
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)
}