Start making CommandTree abstraction include implementation
This commit is contained in:
parent
1c11fec803
commit
819c422cee
@ -1,6 +1,7 @@
|
||||
use std::fmt::Write as FmtWrite;
|
||||
use std::io::{Read, Write};
|
||||
use std::fs::File;
|
||||
use std::sync::Arc;
|
||||
|
||||
use colored::*;
|
||||
use itertools::Itertools;
|
||||
@ -76,7 +77,7 @@ impl Repl {
|
||||
let language_name = self.get_cur_language().get_language_name();
|
||||
let directives = self.get_directives();
|
||||
let tab_complete_handler = TabCompleteHandler::new(self.interpreter_directive_sigil, directives);
|
||||
self.line_reader.set_completer(::std::sync::Arc::new(tab_complete_handler));
|
||||
self.line_reader.set_completer(Arc::new(tab_complete_handler));
|
||||
|
||||
let prompt_str = format!("{} >> ", language_name);
|
||||
self.line_reader.set_prompt(&prompt_str).unwrap();
|
||||
@ -329,7 +330,7 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
|
||||
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
|
||||
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
|
||||
CommandTree::NonTerminal(_, children, _) => children.iter().find(|c| c.get_cmd() == s),
|
||||
CommandTree::Terminal(_, _) => None,
|
||||
CommandTree::Terminal { .. } => None,
|
||||
});
|
||||
command_tree = new_ptr;
|
||||
}
|
||||
@ -344,32 +345,36 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
|
||||
|
||||
#[derive(Clone)]
|
||||
enum CommandTree {
|
||||
Terminal(String, Option<String>),
|
||||
Terminal {
|
||||
name: String,
|
||||
help_msg: Option<String>,
|
||||
function: Option<Box<(fn() -> Option<String>)>>,
|
||||
},
|
||||
NonTerminal(String, Vec<CommandTree>, Option<String>),
|
||||
Top(Vec<CommandTree>),
|
||||
}
|
||||
|
||||
impl CommandTree {
|
||||
fn term(s: &str, help: Option<&str>) -> CommandTree {
|
||||
CommandTree::Terminal(s.to_string(), help.map(|x| x.to_string()))
|
||||
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function: None }
|
||||
}
|
||||
fn get_cmd(&self) -> &str {
|
||||
match self {
|
||||
CommandTree::Terminal(s, _) => s.as_str(),
|
||||
CommandTree::Terminal { name, .. } => name.as_str(),
|
||||
CommandTree::NonTerminal(s, _, _) => s.as_str(),
|
||||
CommandTree::Top(_) => "",
|
||||
}
|
||||
}
|
||||
fn get_help(&self) -> &str {
|
||||
match self {
|
||||
CommandTree::Terminal(_, h) => h.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
||||
CommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
||||
CommandTree::NonTerminal(_, _, h) => h.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
||||
CommandTree::Top(_) => ""
|
||||
}
|
||||
}
|
||||
fn get_children(&self) -> Vec<&str> {
|
||||
match self {
|
||||
CommandTree::Terminal(_, _) => vec![],
|
||||
CommandTree::Terminal { .. } => vec![],
|
||||
CommandTree::NonTerminal(_, children, _) => children.iter().map(|x| x.get_cmd()).collect(),
|
||||
CommandTree::Top(children) => children.iter().map(|x| x.get_cmd()).collect(),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user