Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into paste_refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Sep 1, 2024
2 parents a0e589b + fcbca98 commit bf2a1c0
Show file tree
Hide file tree
Showing 31 changed files with 783 additions and 435 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2.2.0
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- name: Build
run: cargo build --workspace --all-targets
- name: Run tests
Expand All @@ -48,3 +48,16 @@ jobs:
run: cargo check --workspace --no-default-features
env:
RUSTFLAGS: "-D warnings"

direct-minimal-versions:
name: Test min versions
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: hecrj/setup-rust-action@v2
with:
rust-version: nightly
- run: |
cargo update -Z direct-minimal-versions
cargo test --workspace --all-targets
30 changes: 15 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustyline"
version = "12.0.0"
version = "14.0.0"
authors = ["Katsu Kawakami <[email protected]>"]
edition = "2021"
description = "Rustyline, a readline implementation based on Antirez's Linenoise"
Expand All @@ -23,39 +23,39 @@ maintenance = { status = "actively-developed" }
members = ["rustyline-derive"]

[dependencies]
bitflags = "2.0"
bitflags = "2.6"
cfg-if = "1.0"
# For file completion
home = { version = "0.5.4", optional = true }
# For History
fd-lock = { version = "4.0.0", optional = true }
rusqlite = { version = "0.29.0", optional = true, default-features = false, features = ["bundled", "backup"] }
libc = "0.2"
log = "0.4"
unicode-width = "0.1"
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-segmentation = "1.0"
memchr = "2.0"
memchr = "2.7"
# For custom bindings
radix_trie = { version = "0.2", optional = true }
regex = { version = "1.5.5", optional = true }
regex = { version = "1.10", optional = true }
# For derive
rustyline-derive = { version = "0.9.0", optional = true, path = "rustyline-derive" }
rustyline-derive = { version = "0.10.0", optional = true, path = "rustyline-derive" }

[target.'cfg(unix)'.dependencies]
nix = { version = "0.27", default-features = false, features = ["fs", "ioctl", "poll", "signal", "term"] }
nix = { version = "0.29", default-features = false, features = ["fs", "ioctl", "poll", "signal", "term"] }
utf8parse = "0.2"
skim = { version = "0.10", optional = true, default-features = false }
signal-hook = { version = "0.3", optional = true, default-features = false }
termios = "0.3.3"
termios = { version = "0.3.3", optional = true }
buffer-redux = { version = "1.0", optional = true, default-features = false }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["consoleapi", "handleapi", "synchapi", "minwindef", "processenv", "std", "synchapi", "winbase", "wincon", "winerror", "winuser"] }
scopeguard = "1.1"
clipboard-win = "4.5"
windows-sys = { version = "0.59.0", features = ["Win32_Foundation", "Win32_System_Console", "Win32_Security", "Win32_System_Threading", "Win32_UI_Input_KeyboardAndMouse"] }
clipboard-win = "5.0"

[dev-dependencies]
doc-comment = "0.3"
env_logger = { version = "0.10", default-features = false }
env_logger = { version = "0.11", default-features = false }
tempfile = "3.1.0"
rand = "0.8"
assert_matches = "1.2"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ to your `Cargo.toml`:

```toml
[dependencies]
rustyline = "12.0.0"
rustyline = "14.0.0"
```

## Features
Expand Down
3 changes: 1 addition & 2 deletions examples/custom_key_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ impl ConditionalEventHandler for TabEventHandler {
debug_assert_eq!(*evt, Event::from(KeyEvent::from('\t')));
if ctx.line()[..ctx.pos()]
.chars()
.rev()
.next()
.next_back()
.filter(|c| c.is_whitespace())
.is_some()
{
Expand Down
8 changes: 4 additions & 4 deletions examples/diy_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ impl Hint for CommandHint {
}

impl CommandHint {
fn new(text: &str, complete_up_to: &str) -> CommandHint {
fn new(text: &str, complete_up_to: &str) -> Self {
assert!(text.starts_with(complete_up_to));
CommandHint {
Self {
display: text.into(),
complete_up_to: complete_up_to.len(),
}
}

fn suffix(&self, strip_chars: usize) -> CommandHint {
CommandHint {
fn suffix(&self, strip_chars: usize) -> Self {
Self {
display: self.display[strip_chars..].to_owned(),
complete_up_to: self.complete_up_to.saturating_sub(strip_chars),
}
Expand Down
4 changes: 4 additions & 0 deletions examples/input_multiline.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustyline::highlight::MatchingBracketHighlighter;
use rustyline::validate::MatchingBracketValidator;
use rustyline::{Cmd, Editor, EventHandler, KeyCode, KeyEvent, Modifiers, Result};
use rustyline::{Completer, Helper, Highlighter, Hinter, Validator};
Expand All @@ -6,11 +7,14 @@ use rustyline::{Completer, Helper, Highlighter, Hinter, Validator};
struct InputValidator {
#[rustyline(Validator)]
brackets: MatchingBracketValidator,
#[rustyline(Highlighter)]
highlighter: MatchingBracketHighlighter,
}

fn main() -> Result<()> {
let h = InputValidator {
brackets: MatchingBracketValidator::new(),
highlighter: MatchingBracketHighlighter::new(),
};
let mut rl = Editor::new()?;
rl.set_helper(Some(h));
Expand Down
6 changes: 4 additions & 2 deletions examples/read_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ impl Highlighter for MaskingHighlighter {
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
use unicode_width::UnicodeWidthStr;
if self.masking {
Owned("*".repeat(line.width()))
Owned(" ".repeat(line.width()))
} else {
Borrowed(line)
}
}

fn highlight_char(&self, line: &str, pos: usize, forced: bool) -> bool {
fn highlight_char(&self, _line: &str, _pos: usize, _forced: bool) -> bool {
self.masking
}
}
Expand All @@ -37,7 +37,9 @@ fn main() -> Result<()> {
rl.helper_mut().expect("No helper").masking = true;
rl.set_color_mode(ColorMode::Forced); // force masking
rl.set_auto_add_history(false); // make sure password is not added to history
let mut guard = rl.set_cursor_visibility(false)?;
let passwd = rl.readline("Password:")?;
guard.take();
println!("Secret: {passwd}");
Ok(())
}
34 changes: 34 additions & 0 deletions linenoise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Mapping between linenoise API and rustyline API

| linenoise | rustyline | Remarks |
|--------------------------------|------------------------------|---------------------------|
| linenoiseState | State | |
| *Blocking API* |
| linenoise | Editor::readline |
| linenoiseFree | _ | RAII |
| *Non blocking API* | |
| linenoiseEditStart | _ |
| linenoiseEditFeed | _ |
| linenoiseEditStop | _ |
| linenoiseHide | Renderer::clear_rows |
| linenoiseShow | State::refresh_line |
| *Completion API* |
| linenoiseCompletions | Vec<Candidate> |
| linenoiseCompletionCallback | Completer |
| linenoiseAddCompletion | _ | std Vec::add |
| linenoiseSetCompletionCallback | Editor::set_helper |
| linenoiseHintsCallback | Hinter |
| linenoiseSetHintsCallback | Editor::set_helper |
| linenoiseFreeHintsCallback | _ | RAII |
| linenoiseSetFreeHintsCallback | _ | RAII |
| *History API* |
| linenoiseHistoryAdd | Editor::add_history_entry |
| linenoiseHistorySetMaxLen | Editor::set_max_history_size |
| linenoiseHistorySave | Editor::save_history |
| linenoiseHistoryLoad | Editor::load_history |
| *Other utilities* |
| linenoiseClearScreen | Editor::clear_screen |
| linenoiseSetMultiLine | _ | Always activated |
| linenoisePrintKeyCodes | _ | debug logs |
| linenoiseMaskModeEnable | _ | see read_password example |
| linenoiseMaskModeDisable | _ |
8 changes: 4 additions & 4 deletions rustyline-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustyline-derive"
version = "0.9.0"
version = "0.10.0"
authors = ["gwenn"]
edition = "2018"
description = "Rustyline macros implementation of #[derive(Completer, Helper, Hinter, Highlighter)]"
Expand All @@ -19,6 +19,6 @@ maintenance = { status = "actively-developed" }
proc-macro = true

[dependencies]
syn = { version = "2.0", default-features = false, features = ["derive", "parsing", "printing", "proc-macro"] }
quote = { version = "1.0", default-features = false }
proc-macro2 = { version = "1.0", default-features = false }
syn = { version = "2.0.72", default-features = false, features = ["derive", "parsing", "printing", "proc-macro"] }
quote = { version = "1.0.36", default-features = false }
proc-macro2 = { version = "1.0.86", default-features = false }
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) -> bool {
::rustyline::highlight::Highlighter::highlight_char(&self.#field_name_or_index, line, pos)
fn highlight_char(&self, line: &str, pos: usize, forced: bool) -> bool {
::rustyline::highlight::Highlighter::highlight_char(&self.#field_name_or_index, line, pos, forced)
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum Event {
impl Event {
/// See [`KeyEvent::normalize`]
pub(crate) fn normalize(mut self) -> Self {
if let Event::KeySeq(ref mut keys) = self {
if let Self::KeySeq(ref mut keys) = self {
for key in keys.iter_mut() {
*key = KeyEvent::normalize(*key);
}
Expand All @@ -32,7 +32,7 @@ impl Event {
/// Return `i`th key event
#[must_use]
pub fn get(&self, i: usize) -> Option<&KeyEvent> {
if let Event::KeySeq(ref ks) = self {
if let Self::KeySeq(ref ks) = self {
ks.get(i)
} else {
None
Expand All @@ -41,8 +41,8 @@ impl Event {
}

impl From<KeyEvent> for Event {
fn from(k: KeyEvent) -> Event {
Event::KeySeq(vec![k])
fn from(k: KeyEvent) -> Self {
Self::KeySeq(vec![k])
}
}

Expand Down Expand Up @@ -107,15 +107,15 @@ impl KeyEvent {
impl TrieKey for Event {
fn encode_bytes(&self) -> Vec<u8> {
match self {
Event::Any => ANY.to_be_bytes().to_vec(),
Event::KeySeq(keys) => {
Self::Any => ANY.to_be_bytes().to_vec(),
Self::KeySeq(keys) => {
let mut dst = Vec::with_capacity(keys.len() * 4);
for key in keys {
dst.extend_from_slice(&key.encode().to_be_bytes());
}
dst
}
Event::Mouse() => MOUSE.to_be_bytes().to_vec(),
Self::Mouse() => MOUSE.to_be_bytes().to_vec(),
}
}
}
Expand All @@ -132,8 +132,8 @@ pub enum EventHandler {
}

impl From<Cmd> for EventHandler {
fn from(c: Cmd) -> EventHandler {
EventHandler::Simple(c)
fn from(c: Cmd) -> Self {
Self::Simple(c)
}
}

Expand All @@ -147,7 +147,7 @@ pub struct EventContext<'r> {

impl<'r> EventContext<'r> {
pub(crate) fn new(is: &InputState, wrt: &'r dyn Refresher) -> Self {
EventContext {
Self {
mode: is.mode,
input_mode: is.input_mode,
wrt,
Expand Down Expand Up @@ -216,8 +216,6 @@ pub trait ConditionalEventHandler: Send + Sync {

#[cfg(test)]
mod test {
use core::mem::size_of;

use super::{Event, EventHandler};
use crate::{Cmd, KeyCode, KeyEvent, Modifiers};
use radix_trie::Trie;
Expand Down Expand Up @@ -251,7 +249,10 @@ mod test {
}

#[test]
#[ignore]
#[cfg(target_arch = "x86_64")]
fn size_of_event() {
use core::mem::size_of;
assert_eq!(size_of::<Event>(), 32);
}
}
3 changes: 3 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ pub fn execute<H: Helper>(
Cmd::Newline => {
s.edit_insert('\n', 1)?;
}
Cmd::Repaint => {
s.refresh_line()?;
}
Cmd::AcceptLine | Cmd::AcceptOrInsertLine { .. } => {
let validation_result = s.validate()?;
let valid = validation_result.is_valid();
Expand Down
13 changes: 5 additions & 8 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ impl Completer for FilenameCompleter {
/// Remove escape char
#[must_use]
pub fn unescape(input: &str, esc_char: Option<char>) -> Cow<'_, str> {
let esc_char = if let Some(c) = esc_char {
c
} else {
let Some(esc_char) = esc_char else {
return Borrowed(input);
};
if !input.chars().any(|c| c == esc_char) {
Expand Down Expand Up @@ -310,9 +308,7 @@ pub fn escape(
if n == 0 {
return input; // no need to escape
}
let esc_char = if let Some(c) = esc_char {
c
} else {
let Some(esc_char) = esc_char else {
if cfg!(windows) && quote == Quote::None {
input.insert(0, '"'); // force double quote
return input;
Expand Down Expand Up @@ -375,7 +371,7 @@ fn filename_complete(
dir_path.to_path_buf()
};

let mut entries: Vec<Pair> = Vec::new();
let mut entries: Vec<Pair> = vec![];

// if dir doesn't exist, then don't offer any completions
if !dir.exists() {
Expand Down Expand Up @@ -419,6 +415,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 @@ -617,7 +614,7 @@ mod tests {
assert_eq!(Some(s), lcp);
}

let c3 = String::from("");
let c3 = String::new();
candidates.push(c3);
{
let lcp = super::longest_common_prefix(&candidates);
Expand Down
Loading

0 comments on commit bf2a1c0

Please sign in to comment.