Add configuration option to disable hinter (#2403) (#2405)

* Add configuration option to disable hinter

* Move hint configuration inside line_editor
This commit is contained in:
Amit Dev 2020-08-27 15:45:55 +10:00 committed by GitHub
parent 7702d683c7
commit 39f402c8cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View File

@ -820,7 +820,9 @@ pub async fn cli(
let cwd = context.shell_manager.path(); let cwd = context.shell_manager.path();
rl.set_helper(Some(crate::shell::Helper::new(context.clone()))); let hinter = init_hinter(&config);
rl.set_helper(Some(crate::shell::Helper::new(context.clone(), hinter)));
let colored_prompt = { let colored_prompt = {
if let Some(prompt) = config.get("prompt") { if let Some(prompt) = config.get("prompt") {
@ -967,6 +969,18 @@ pub async fn cli(
Ok(()) Ok(())
} }
fn init_hinter(config: &IndexMap<String, Value>) -> Option<rustyline::hint::HistoryHinter> {
// Show hints unless explicitly disabled in config
if let Some(line_editor_vars) = config.get("line_editor") {
for (idx, value) in line_editor_vars.row_entries() {
if idx == "show_hints" && value.expect_string() == "false" {
return None;
}
}
}
Some(rustyline::hint::HistoryHinter {})
}
fn chomp_newline(s: &str) -> &str { fn chomp_newline(s: &str) -> &str {
if s.ends_with('\n') { if s.ends_with('\n') {
&s[..s.len() - 1] &s[..s.len() - 1]

View File

@ -12,16 +12,16 @@ use crate::shell::palette::{DefaultPalette, Palette};
pub struct Helper { pub struct Helper {
completer: NuCompleter, completer: NuCompleter,
hinter: rustyline::hint::HistoryHinter, hinter: Option<rustyline::hint::HistoryHinter>,
context: Context, context: Context,
pub colored_prompt: String, pub colored_prompt: String,
} }
impl Helper { impl Helper {
pub(crate) fn new(context: Context) -> Helper { pub(crate) fn new(context: Context, hinter: Option<rustyline::hint::HistoryHinter>) -> Helper {
Helper { Helper {
completer: NuCompleter {}, completer: NuCompleter {},
hinter: rustyline::hint::HistoryHinter {}, hinter,
context, context,
colored_prompt: String::new(), colored_prompt: String::new(),
} }
@ -54,7 +54,7 @@ impl rustyline::completion::Completer for Helper {
impl rustyline::hint::Hinter for Helper { impl rustyline::hint::Hinter for Helper {
fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> { fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> {
self.hinter.hint(line, pos, &ctx) self.hinter.as_ref().and_then(|h| h.hint(line, pos, &ctx))
} }
} }