src/cli/readline.rs: moved readline related code here

This commit is contained in:
Dietmar Maurer 2019-12-01 11:31:35 +01:00
parent 5d64a0d083
commit a810e05288
3 changed files with 43 additions and 42 deletions

View File

@ -22,6 +22,9 @@ pub use getopts::*;
mod command; mod command;
pub use command::*; pub use command::*;
mod readline;
pub use readline::*;
use std::collections::HashMap; use std::collections::HashMap;
use proxmox::api::ApiMethod; use proxmox::api::ApiMethod;

View File

@ -1,5 +1,3 @@
use std::sync::Arc;
use super::*; use super::*;
pub fn get_completions( pub fn get_completions(
@ -34,43 +32,3 @@ pub fn get_completions(
(start, completions) (start, completions)
} }
pub struct CliHelper {
cmd_def: Arc<CommandLineInterface>,
}
impl CliHelper {
pub fn new(cmd_def: CommandLineInterface) -> Self {
Self { cmd_def: Arc::new(cmd_def) }
}
pub fn cmd_def(&self) -> Arc<CommandLineInterface> {
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<Self::Candidate>)> {
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 {
}

40
src/cli/readline.rs Normal file
View File

@ -0,0 +1,40 @@
use std::sync::Arc;
use super::*;
pub struct CliHelper {
cmd_def: Arc<CommandLineInterface>,
}
impl CliHelper {
pub fn new(cmd_def: CommandLineInterface) -> Self {
Self { cmd_def: Arc::new(cmd_def) }
}
pub fn cmd_def(&self) -> Arc<CommandLineInterface> {
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<Self::Candidate>)> {
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 {}