From a810e052882ad52cf54cb4b833c274829d60075a Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sun, 1 Dec 2019 11:31:35 +0100 Subject: [PATCH] src/cli/readline.rs: moved readline related code here --- src/cli.rs | 3 +++ src/cli/completion.rs | 42 ------------------------------------------ src/cli/readline.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 42 deletions(-) create mode 100644 src/cli/readline.rs diff --git a/src/cli.rs b/src/cli.rs index 15c8eb4b..b2049180 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -22,6 +22,9 @@ pub use getopts::*; mod command; pub use command::*; +mod readline; +pub use readline::*; + use std::collections::HashMap; use proxmox::api::ApiMethod; diff --git a/src/cli/completion.rs b/src/cli/completion.rs index 26c2ca63..c7538c1e 100644 --- a/src/cli/completion.rs +++ b/src/cli/completion.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use super::*; pub fn get_completions( @@ -34,43 +32,3 @@ pub fn get_completions( (start, completions) } - -pub struct CliHelper { - cmd_def: Arc, -} - -impl CliHelper { - - pub fn new(cmd_def: CommandLineInterface) -> Self { - Self { cmd_def: Arc::new(cmd_def) } - } - - pub fn cmd_def(&self) -> Arc { - self.cmd_def.clone() - } -} - -impl rustyline::completion::Completer for CliHelper { - type Candidate = String; - - fn complete( - &self, - line: &str, - pos: usize, - _ctx: &rustyline::Context<'_>, - ) -> rustyline::Result<(usize, Vec)> { - - let line = &line[..pos]; - - let (start, completions) = super::get_completions(&*self.cmd_def, line, false); - - return Ok((start, completions)); - } -} - -impl rustyline::hint::Hinter for CliHelper {} -impl rustyline::highlight::Highlighter for CliHelper {} - -impl rustyline::Helper for CliHelper { - -} diff --git a/src/cli/readline.rs b/src/cli/readline.rs new file mode 100644 index 00000000..073091a9 --- /dev/null +++ b/src/cli/readline.rs @@ -0,0 +1,40 @@ +use std::sync::Arc; + +use super::*; + +pub struct CliHelper { + cmd_def: Arc, +} + +impl CliHelper { + + pub fn new(cmd_def: CommandLineInterface) -> Self { + Self { cmd_def: Arc::new(cmd_def) } + } + + pub fn cmd_def(&self) -> Arc { + self.cmd_def.clone() + } +} + +impl rustyline::completion::Completer for CliHelper { + type Candidate = String; + + fn complete( + &self, + line: &str, + pos: usize, + _ctx: &rustyline::Context<'_>, + ) -> rustyline::Result<(usize, Vec)> { + + let line = &line[..pos]; + + let (start, completions) = super::get_completions(&*self.cmd_def, line, false); + + return Ok((start, completions)); + } +} + +impl rustyline::hint::Hinter for CliHelper {} +impl rustyline::highlight::Highlighter for CliHelper {} +impl rustyline::Helper for CliHelper {}