86 lines
2.4 KiB
Rust
86 lines
2.4 KiB
Rust
use std::fmt::Write as FmtWrite;
|
|
|
|
use crate::command_tree::CommandTree;
|
|
use crate::language::ProgrammingLanguageInterface;
|
|
use crate::{InterpreterDirectiveOutput, Repl};
|
|
use colored::*;
|
|
|
|
pub fn help<L: ProgrammingLanguageInterface>(
|
|
repl: &mut Repl<L>,
|
|
arguments: &[&str],
|
|
) -> InterpreterDirectiveOutput {
|
|
match arguments {
|
|
[] => global_help(repl),
|
|
commands => {
|
|
let dirs = repl.get_directives();
|
|
Some(match get_directive_from_commands(commands, &dirs) {
|
|
None => format!("Directive `{}` not found", commands.last().unwrap()),
|
|
Some(dir) => {
|
|
let mut buf = String::new();
|
|
let cmd = dir.get_cmd();
|
|
let children = dir.get_children();
|
|
writeln!(buf, "`{}` - {}", cmd, dir.get_help()).unwrap();
|
|
for sub in children.iter() {
|
|
writeln!(buf, "\t`{} {}` - {}", cmd, sub.get_cmd(), sub.get_help())
|
|
.unwrap();
|
|
}
|
|
buf
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
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<L: ProgrammingLanguageInterface>(repl: &mut Repl<L>) -> InterpreterDirectiveOutput {
|
|
let mut buf = String::new();
|
|
|
|
writeln!(
|
|
buf,
|
|
"{} version {}",
|
|
"Schala REPL".bright_red().bold(),
|
|
crate::VERSION_STRING
|
|
)
|
|
.unwrap();
|
|
writeln!(buf, "-----------------------").unwrap();
|
|
|
|
for directive in repl.get_directives().get_children() {
|
|
writeln!(
|
|
buf,
|
|
"{}{} - {}",
|
|
repl.sigil,
|
|
directive.get_cmd(),
|
|
directive.get_help()
|
|
)
|
|
.unwrap();
|
|
}
|
|
|
|
writeln!(buf).unwrap();
|
|
writeln!(
|
|
buf,
|
|
"Language-specific help for {}",
|
|
<L as ProgrammingLanguageInterface>::language_name()
|
|
)
|
|
.unwrap();
|
|
writeln!(buf, "-----------------------").unwrap();
|
|
Some(buf)
|
|
}
|