From 819c422ceee087c3e4e68289a5557c13b55df5fa Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 2 Oct 2018 00:46:23 -0700 Subject: [PATCH] Start making CommandTree abstraction include implementation --- schala-repl/src/repl.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/schala-repl/src/repl.rs b/schala-repl/src/repl.rs index 4ff75ef..058b7e7 100644 --- a/schala-repl/src/repl.rs +++ b/schala-repl/src/repl.rs @@ -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 Completer 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 Completer for TabCompleteHandler { #[derive(Clone)] enum CommandTree { - Terminal(String, Option), + Terminal { + name: String, + help_msg: Option, + function: Option Option)>>, + }, NonTerminal(String, Vec, Option), Top(Vec), } 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(), }