Refactor Helper trait
diff --git a/examples/example.rs b/examples/example.rs
index 4e9c407..51f9154 100644
--- a/examples/example.rs
+++ b/examples/example.rs
@@ -3,10 +3,10 @@
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
-use rustyline::completion::FilenameCompleter;
+use rustyline::completion::{Completer, FilenameCompleter};
use rustyline::error::ReadlineError;
use rustyline::hint::Hinter;
-use rustyline::{Cmd, CompletionType, Config, EditMode, Editor, KeyPress};
+use rustyline::{Cmd, CompletionType, Config, EditMode, Editor, Helper, KeyPress};
// On unix platforms you can use ANSI escape sequences
#[cfg(unix)]
@@ -17,9 +17,15 @@
#[cfg(windows)]
static PROMPT: &'static str = ">> ";
-struct Hints {}
+struct MyHelper(FilenameCompleter);
-impl Hinter for Hints {
+impl Completer for MyHelper {
+ fn complete(&self, line: &str, pos: usize) -> Result<(usize, Vec<String>), ReadlineError> {
+ self.0.complete(line, pos)
+ }
+}
+
+impl Hinter for MyHelper {
fn hint(&self, line: &str, _pos: usize) -> Option<String> {
if line == "hello" {
if cfg!(target_os = "windows") {
@@ -33,6 +39,9 @@
}
}
+impl Helper for MyHelper {
+}
+
fn main() {
init_logger().is_ok();
let config = Config::builder()
@@ -40,9 +49,9 @@
.completion_type(CompletionType::List)
.edit_mode(EditMode::Emacs)
.build();
- let c = FilenameCompleter::new();
+ let h = MyHelper(FilenameCompleter::new());
let mut rl = Editor::with_config(config);
- rl.set_helper(Some((c, Hints {})));
+ rl.set_helper(Some(h));
rl.bind_sequence(KeyPress::Meta('N'), Cmd::HistorySearchForward);
rl.bind_sequence(KeyPress::Meta('P'), Cmd::HistorySearchBackward);
if rl.load_history("history.txt").is_err() {
diff --git a/src/lib.rs b/src/lib.rs
index b4348b1..b37409e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -42,6 +42,7 @@
mod tty;
+use std::borrow::Cow::{self, Borrowed};
use std::collections::HashMap;
use std::fmt;
use std::io::{self, Write};
@@ -346,8 +347,8 @@
editor: &mut Editor<H>,
original_mode: &tty::Mode,
) -> Result<String> {
- let completer = editor.helper.as_ref().map(|h| h.completer());
- let hinter = editor.helper.as_ref().map(|h| h.hinter() as &Hinter);
+ let completer = editor.helper.as_ref();
+ let hinter = editor.helper.as_ref().map(|h| h as &Hinter);
let mut stdout = editor.term.create_writer();
@@ -620,37 +621,18 @@
/// Syntax specific helper.
///
-/// TODO Tokenizer/parser used for both completion, suggestion, highlighting
-pub trait Helper {
- type Completer: Completer;
- type Hinter: Hinter;
-
- fn completer(&self) -> &Self::Completer;
- fn hinter(&self) -> &Self::Hinter;
-}
-
-impl<C: Completer, H: Hinter> Helper for (C, H) {
- type Completer = C;
- type Hinter = H;
-
- fn completer(&self) -> &C {
- &self.0
- }
- fn hinter(&self) -> &H {
- &self.1
+/// TODO Tokenizer/parser used for both completion, suggestion, highlighting.
+/// (parse current line once)
+pub trait Helper where Self: Completer, Self: Hinter {
+ /// Decorate `line` with [ansi color](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters).
+ /// Rustyline will try to handle escape sequence for ansi color on windows when not supported natively (windows <10).
+ /// TODO to be used
+ fn highligh(line: &str) -> Cow<str> {
+ Borrowed(line)
}
}
-impl<C: Completer> Helper for C {
- type Completer = C;
- type Hinter = ();
- fn completer(&self) -> &C {
- self
- }
- fn hinter(&self) -> &() {
- &()
- }
-}
+impl Helper for () {}
/// Line editor
pub struct Editor<H: Helper> {