forked from kkawakam/rustyline
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize
split-highlight
feature gating logic
- Loading branch information
1 parent
e42bda3
commit 94a5899
Showing
9 changed files
with
265 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use rustyline::highlight::StyledBlock; | ||
use rustyline::{Cmd, Editor, EventHandler, KeyCode, KeyEvent, Modifiers, Result}; | ||
use rustyline::{Completer, Helper, Hinter, Validator}; | ||
use syntect::highlighting::{Theme, ThemeSet}; | ||
use syntect::parsing::SyntaxSet; | ||
|
||
use std::borrow::Cow; | ||
#[derive(Completer, Helper, Hinter, Validator)] | ||
struct InputHelper { | ||
theme: Theme, | ||
syntax_set: SyntaxSet, | ||
} | ||
|
||
impl rustyline::highlight::Highlighter for InputHelper { | ||
fn highlight_char(&self, _line: &str, _pos: usize, _forced: bool) -> bool { | ||
true | ||
} | ||
#[cfg(feature = "continuation-prompt")] | ||
fn continuation_prompt<'b, 's: 'b, 'p: 'b>( | ||
&'s self, | ||
_prompt: &'p str, | ||
_default: bool, | ||
) -> Cow<'b, str> { | ||
Cow::Borrowed("... ") | ||
} | ||
#[cfg(all(feature = "split-highlight", feature = "continuation-prompt",))] | ||
fn highlight_lines<'l>( | ||
&self, | ||
lines: &'l str, | ||
_pos: usize, | ||
) -> impl Iterator<Item = impl Iterator<Item = impl 'l + StyledBlock>> { | ||
use syntect::easy::HighlightLines; | ||
use syntect::highlighting::Style; | ||
let syntax = self.syntax_set.find_syntax_by_extension("py").unwrap(); | ||
let mut highlighter = HighlightLines::new(syntax, &self.theme); | ||
lines.split('\n').map(move |line| { | ||
highlighter | ||
.highlight_line(line, &self.syntax_set) | ||
.unwrap_or(vec![(Style::default(), line)]) | ||
.into_iter() | ||
.map(|(mut s, token)| { | ||
s.background.a = 0; | ||
(s, token) | ||
}) | ||
}) | ||
} | ||
} | ||
fn main() -> Result<()> { | ||
let h = InputHelper { | ||
theme: ThemeSet::load_defaults() | ||
.themes | ||
.remove("base16-ocean.dark") | ||
.unwrap(), | ||
syntax_set: SyntaxSet::load_defaults_newlines(), | ||
}; | ||
let mut rl = Editor::new()?; | ||
rl.set_helper(Some(h)); | ||
rl.bind_sequence( | ||
KeyEvent(KeyCode::Char('s'), Modifiers::CTRL), | ||
EventHandler::Simple(Cmd::Newline), | ||
); | ||
let input = rl.readline("python REPL example\n>>> ")?; | ||
println!("Input: \n{input}"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.