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

43 lines
1.3 KiB
Rust
Raw Normal View History

2019-06-04 15:02:51 -07:00
use std::fmt::Write as FmtWrite;
use super::{Repl, InterpreterDirectiveOutput};
use crate::repl::command_tree::CommandTree;
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 {
[] => return global_help(repl),
_ => {
writeln!(buf, "Command-specific help not available yet").unwrap();
}
};
Some(buf)
}
fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
let mut buf = String::new();
let directives = match repl.get_directives() {
CommandTree::Top(children) => children,
_ => panic!("Top-level CommandTree not Top")
};
writeln!(buf, "MetaInterpreter options").unwrap();
writeln!(buf, "-----------------------").unwrap();
for directive in directives {
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)
}