rustyline/highlight.rs
1//! Syntax highlighting
2
3use config::CompletionType;
4use std::borrow::Cow::{self, Borrowed};
5
6/// Syntax highlighter with [ansi color](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters).
7/// Rustyline will try to handle escape sequence for ansi color on windows
8/// when not supported natively (windows <10).
9///
10/// Currently, the highlighted version *must* have the same display width as
11/// the original input.
12pub trait Highlighter {
13 /// Takes the currently edited `line` with the cursor `pos`ition and
14 /// returns the highlighted version (with ANSI color).
15 ///
16 /// For example, you can implement
17 /// [blink-matching-paren](https://www.gnu.org/software/bash/manual/html_node/Readline-Init-File-Syntax.html).
18 fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
19 let _ = pos;
20 Borrowed(line)
21 }
22 /// Takes the `prompt` and
23 /// returns the highlighted version (with ANSI color).
24 fn highlight_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> {
25 Borrowed(prompt)
26 }
27 /// Takes the dynamic `prompt` and
28 /// returns the highlighted version (with ANSI color).
29 #[deprecated(
30 since = "2.0.1",
31 note = "please use `highlight_prompt` instead"
32 )]
33 fn highlight_dynamic_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> {
34 Borrowed(prompt)
35 }
36 /// Takes the `hint` and
37 /// returns the highlighted version (with ANSI color).
38 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
39 Borrowed(hint)
40 }
41 /// Takes the completion `canditate` and
42 /// returns the highlighted version (with ANSI color).
43 ///
44 /// Currently, used only with `CompletionType::List`.
45 fn highlight_candidate<'c>(
46 &self,
47 candidate: &'c str,
48 completion: CompletionType,
49 ) -> Cow<'c, str> {
50 let _ = completion;
51 Borrowed(candidate)
52 }
53 /// Tells if the `ch`ar needs to be highlighted when typed or when cursor
54 /// is moved under.
55 ///
56 /// Used to optimize refresh when a character is inserted or the cursor is
57 /// moved.
58 fn highlight_char(&self, grapheme: &str) -> bool {
59 let _ = grapheme;
60 false
61 }
62}
63
64impl Highlighter for () {}