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.
new
Helper
to reduce parser overhead and support continuation prompt
- Loading branch information
1 parent
7465673
commit f53ba9e
Showing
24 changed files
with
434 additions
and
273 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use rustyline::highlight::Highlighter; | ||
use rustyline::validate::{ValidationContext, ValidationResult, Validator}; | ||
use rustyline::{Cmd, Editor, EventHandler, Helper, KeyCode, KeyEvent, Modifiers, Result}; | ||
use rustyline::{Completer, Hinter}; | ||
|
||
#[derive(Completer, Hinter)] | ||
struct InputValidator { | ||
bracket_level: i32, | ||
/// re-render only when input just changed | ||
/// not render after cursor moving | ||
need_render: bool, | ||
} | ||
|
||
impl Helper for InputValidator { | ||
fn update_after_edit(&mut self, line: &str, _pos: usize, _forced_refresh: bool) { | ||
self.bracket_level = line.chars().fold(0, |level, c| { | ||
if c == '(' { | ||
level + 1 | ||
} else if c == ')' { | ||
level - 1 | ||
} else { | ||
level | ||
} | ||
}); | ||
self.need_render = true; | ||
} | ||
} | ||
|
||
impl Validator for InputValidator { | ||
fn validate(&mut self, _ctx: &mut ValidationContext) -> Result<ValidationResult> { | ||
if self.bracket_level > 0 { | ||
Ok(ValidationResult::Incomplete(2)) | ||
} else if self.bracket_level < 0 { | ||
Ok(ValidationResult::Invalid(Some(format!( | ||
" - excess {} close bracket", | ||
-self.bracket_level | ||
)))) | ||
} else { | ||
Ok(ValidationResult::Valid(None)) | ||
} | ||
} | ||
} | ||
|
||
impl Highlighter for InputValidator { | ||
fn highlight_char(&mut self, _line: &str, _pos: usize, _forced: bool) -> bool { | ||
self.need_render | ||
} | ||
fn highlight_line<'l>( | ||
&mut self, | ||
line: &'l str, | ||
_pos: usize, | ||
) -> impl Iterator<Item = impl 'l + rustyline::highlight::StyledBlock> { | ||
use core::iter::once; | ||
let mut lines = line.split('\n'); | ||
self.need_render = false; | ||
once(((), lines.next().unwrap())) | ||
.chain(lines.flat_map(|line| once(((), "\n.. ")).chain(once(((), line))))) | ||
} | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let h = InputValidator { | ||
bracket_level: 0, | ||
need_render: true, | ||
}; | ||
let mut rl = Editor::new(h)?; | ||
rl.bind_sequence( | ||
KeyEvent(KeyCode::Char('s'), Modifiers::CTRL), | ||
EventHandler::Simple(Cmd::Newline), | ||
); | ||
|
||
let input = rl.readline(">> ")?; | ||
println!("Input: {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
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
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
Oops, something went wrong.