Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into history_recent_index
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Oct 8, 2024
2 parents 99c6c31 + 34edadd commit 637ae71
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 245 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fd-lock = { version = "4.0.0", optional = true }
rusqlite = { version = "0.32.0", optional = true, default-features = false, features = ["bundled", "backup"] }
libc = "0.2.155"
log = "0.4.22"
unicode-width = "0.1.13"
unicode-width = "0.2.0"
unicode-segmentation = "1.0"
memchr = "2.7"
# For custom bindings
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ For all modes:

[Readline vi Editing Mode Cheat Sheet](http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf)

[Terminal codes (ANSI/VT100)](http://wiki.bash-hackers.org/scripting/terminalcodes)
[ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code)

## Wine

Expand Down Expand Up @@ -252,3 +252,7 @@ literal newline to be added to the input buffer.
The way to achieve multi-line editing is to implement the `Validator`
trait.
## Minimum supported Rust version (MSRV)
Latest stable Rust version at the time of release. It might compile with older versions.
6 changes: 3 additions & 3 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow::{self, Borrowed, Owned};

use rustyline::completion::FilenameCompleter;
use rustyline::error::ReadlineError;
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
use rustyline::highlight::{CmdKind, Highlighter, MatchingBracketHighlighter};
use rustyline::hint::HistoryHinter;
use rustyline::validate::MatchingBracketValidator;
use rustyline::{Cmd, CompletionType, Config, EditMode, Editor, KeyEvent};
Expand Down Expand Up @@ -41,8 +41,8 @@ impl Highlighter for MyHelper {
self.highlighter.highlight(line, pos)
}

fn highlight_char(&self, line: &str, pos: usize, forced: bool) -> bool {
self.highlighter.highlight_char(line, pos, forced)
fn highlight_char(&self, line: &str, pos: usize, kind: CmdKind) -> bool {
self.highlighter.highlight_char(line, pos, kind)
}
}

Expand Down
10 changes: 7 additions & 3 deletions examples/read_password.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow::{self, Borrowed, Owned};

use rustyline::config::Configurer;
use rustyline::highlight::Highlighter;
use rustyline::highlight::{CmdKind, Highlighter};
use rustyline::{ColorMode, Editor, Result};
use rustyline::{Completer, Helper, Hinter, Validator};

Expand All @@ -20,12 +20,16 @@ impl Highlighter for MaskingHighlighter {
}
}

fn highlight_char(&self, _line: &str, _pos: usize, _forced: bool) -> bool {
self.masking
fn highlight_char(&self, _line: &str, _pos: usize, kind: CmdKind) -> bool {
match kind {
CmdKind::MoveCursor => false,
_ => self.masking,
}
}
}

fn main() -> Result<()> {
env_logger::init();
println!("This is just a hack. Reading passwords securely requires more than that.");
let h = MaskingHighlighter { masking: false };
let mut rl = Editor::new()?;
Expand Down
4 changes: 2 additions & 2 deletions rustyline-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ pub fn highlighter_macro_derive(input: TokenStream) -> TokenStream {
::rustyline::highlight::Highlighter::highlight_candidate(&self.#field_name_or_index, candidate, completion)
}

fn highlight_char(&self, line: &str, pos: usize, forced: bool) -> bool {
::rustyline::highlight::Highlighter::highlight_char(&self.#field_name_or_index, line, pos, forced)
fn highlight_char(&self, line: &str, pos: usize, kind: ::rustyline::highlight::CmdKind) -> bool {
::rustyline::highlight::Highlighter::highlight_char(&self.#field_name_or_index, line, pos, kind)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::complete_hint_line;
use crate::config::Config;
use crate::edit::State;
use crate::error;
use crate::highlight::CmdKind;
use crate::history::SearchDirection;
use crate::keymap::{Anchor, At, Cmd, Movement, Word};
use crate::keymap::{InputState, Refresher};
Expand All @@ -28,9 +29,7 @@ pub fn execute<H: Helper>(
if s.has_hint() || !s.is_default_prompt() || s.highlight_char {
// Force a refresh without hints to leave the previous
// line as the user typed it after a newline.
s.forced_refresh = true;
s.refresh_line_with_msg(None)?;
s.forced_refresh = false;
s.refresh_line_with_msg(None, CmdKind::ForcedRefresh)?;
}
}
_ => {}
Expand Down Expand Up @@ -190,7 +189,7 @@ pub fn execute<H: Helper>(
}
Cmd::Move(Movement::EndOfBuffer) => {
// Move to the end of the buffer.
s.edit_move_buffer_end()?;
s.edit_move_buffer_end(CmdKind::MoveCursor)?;
}
Cmd::DowncaseWord => {
// lowercase word after point
Expand Down
71 changes: 20 additions & 51 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,13 @@ pub trait Candidate {
fn replacement(&self) -> &str;
}

impl Candidate for String {
impl<T: AsRef<str>> Candidate for T {
fn display(&self) -> &str {
self.as_str()
self.as_ref()
}

fn replacement(&self) -> &str {
self.as_str()
}
}

/// #[deprecated = "Unusable"]
impl Candidate for str {
fn display(&self) -> &str {
self
}

fn replacement(&self) -> &str {
self
}
}

impl Candidate for &'_ str {
fn display(&self) -> &str {
self
}

fn replacement(&self) -> &str {
self
}
}

impl Candidate for Rc<str> {
fn display(&self) -> &str {
self
}

fn replacement(&self) -> &str {
self
self.as_ref()
}
}

Expand Down Expand Up @@ -113,22 +82,6 @@ impl Completer for () {
}
}

impl<'c, C: ?Sized + Completer> Completer for &'c C {
type Candidate = C::Candidate;

fn complete(
&self,
line: &str,
pos: usize,
ctx: &Context<'_>,
) -> Result<(usize, Vec<Self::Candidate>)> {
(**self).complete(line, pos, ctx)
}

fn update(&self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) {
(**self).update(line, start, elected, cl);
}
}
macro_rules! box_completer {
($($id: ident)*) => {
$(
Expand Down Expand Up @@ -211,7 +164,6 @@ impl FilenameCompleter {
/// partial path to be completed.
pub fn complete_path(&self, line: &str, pos: usize) -> Result<(usize, Vec<Pair>)> {
let (start, mut matches) = self.complete_path_unsorted(line, pos)?;
#[allow(clippy::unnecessary_sort_by)]
matches.sort_by(|a, b| a.display().cmp(b.display()));
Ok((start, matches))
}
Expand Down Expand Up @@ -415,6 +367,7 @@ fn normalize(s: &str) -> Cow<str> {

/// Given a `line` and a cursor `pos`ition,
/// try to find backward the start of a word.
///
/// Return (0, `line[..pos]`) if no break char has been found.
/// Return the word and its start position (idx, `line[idx..pos]`) otherwise.
#[must_use]
Expand Down Expand Up @@ -647,4 +600,20 @@ mod tests {
pub fn normalize() {
assert_eq!(super::normalize("Windows"), "windows")
}

#[test]
pub fn candidate_impls() {
struct StrCmp;
impl super::Completer for StrCmp {
type Candidate = &'static str;
}
struct RcCmp;
impl super::Completer for RcCmp {
type Candidate = std::rc::Rc<str>;
}
struct ArcCmp;
impl super::Completer for ArcCmp {
type Candidate = std::sync::Arc<str>;
}
}
}
Loading

0 comments on commit 637ae71

Please sign in to comment.