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::fmt::Write as FmtWrite;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use colored::*;
|
use colored::*;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@ -76,7 +77,7 @@ impl Repl {
|
|||||||
let language_name = self.get_cur_language().get_language_name();
|
let language_name = self.get_cur_language().get_language_name();
|
||||||
let directives = self.get_directives();
|
let directives = self.get_directives();
|
||||||
let tab_complete_handler = TabCompleteHandler::new(self.interpreter_directive_sigil, 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);
|
let prompt_str = format!("{} >> ", language_name);
|
||||||
self.line_reader.set_prompt(&prompt_str).unwrap();
|
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 {
|
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
|
||||||
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
|
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
|
||||||
CommandTree::NonTerminal(_, 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;
|
command_tree = new_ptr;
|
||||||
}
|
}
|
||||||
@ -344,32 +345,36 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
enum CommandTree {
|
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>),
|
NonTerminal(String, Vec<CommandTree>, Option<String>),
|
||||||
Top(Vec<CommandTree>),
|
Top(Vec<CommandTree>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandTree {
|
impl CommandTree {
|
||||||
fn term(s: &str, help: Option<&str>) -> 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 {
|
fn get_cmd(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
CommandTree::Terminal(s, _) => s.as_str(),
|
CommandTree::Terminal { name, .. } => name.as_str(),
|
||||||
CommandTree::NonTerminal(s, _, _) => s.as_str(),
|
CommandTree::NonTerminal(s, _, _) => s.as_str(),
|
||||||
CommandTree::Top(_) => "",
|
CommandTree::Top(_) => "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn get_help(&self) -> &str {
|
fn get_help(&self) -> &str {
|
||||||
match self {
|
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::NonTerminal(_, _, h) => h.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
||||||
CommandTree::Top(_) => ""
|
CommandTree::Top(_) => ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn get_children(&self) -> Vec<&str> {
|
fn get_children(&self) -> Vec<&str> {
|
||||||
match self {
|
match self {
|
||||||
CommandTree::Terminal(_, _) => vec![],
|
CommandTree::Terminal { .. } => vec![],
|
||||||
CommandTree::NonTerminal(_, children, _) => children.iter().map(|x| x.get_cmd()).collect(),
|
CommandTree::NonTerminal(_, children, _) => children.iter().map(|x| x.get_cmd()).collect(),
|
||||||
CommandTree::Top(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