-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from fuyufjh/eric/fix_window_prompt_display
fix prompt display under Windows
- Loading branch information
Showing
2 changed files
with
50 additions
and
3 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,44 @@ | ||
use std::borrow::Cow; | ||
|
||
use console::style; | ||
use rustyline::completion::Completer; | ||
use rustyline::highlight::Highlighter; | ||
use rustyline::hint::Hinter; | ||
use rustyline::validate::{ValidationContext, ValidationResult, Validator}; | ||
use rustyline::Helper; | ||
|
||
/// The rustyline helper for interactive mode. | ||
/// Currently it's mostly for highlighting the prompt correctly. | ||
#[derive(Clone, Debug, Default)] | ||
pub struct ReplHelper; | ||
|
||
impl Helper for ReplHelper {} | ||
|
||
impl Validator for ReplHelper { | ||
fn validate(&self, _ctx: &mut ValidationContext) -> rustyline::Result<ValidationResult> { | ||
Ok(ValidationResult::Valid(None)) | ||
} | ||
} | ||
|
||
impl Completer for ReplHelper { | ||
type Candidate = String; | ||
} | ||
|
||
impl Hinter for ReplHelper { | ||
type Hint = String; | ||
} | ||
|
||
impl Highlighter for ReplHelper { | ||
fn highlight_prompt<'b, 's: 'b, 'p: 'b>( | ||
&'s self, | ||
prompt: &'p str, | ||
_default: bool, | ||
) -> Cow<'b, str> { | ||
if let Some(idx) = prompt.find(" => ") { | ||
let (role, rest) = prompt.split_at(idx); | ||
Cow::Owned(format!("{}{}", style(role).bold().cyan(), rest)) | ||
} else { | ||
Cow::Borrowed(prompt) | ||
} | ||
} | ||
} |