Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Jan 5, 2025
2 parents 1d10761 + 5910f9e commit b74696f
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 171 deletions.
25 changes: 20 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustyline"
version = "14.0.0"
version = "15.0.0"
authors = ["Katsu Kawakami <[email protected]>"]
edition = "2021"
description = "Rustyline, a readline implementation based on Antirez's Linenoise"
Expand Down Expand Up @@ -30,7 +30,10 @@ cfg-if = "1.0"
home = { version = "0.5.4", optional = true }
# For History
fd-lock = { version = "4.0.0", optional = true }
rusqlite = { version = "0.32.0", optional = true, default-features = false, features = ["bundled", "backup"] }
rusqlite = { version = "0.32.0", optional = true, default-features = false, features = [
"bundled",
"backup",
] }
libc = "0.2.155"
log = "0.4.22"
unicode-width = "0.2.0"
Expand All @@ -40,18 +43,30 @@ memchr = "2.7"
radix_trie = { version = "0.2", optional = true }
regex = { version = "1.10", optional = true }
# For derive
rustyline-derive = { version = "0.10.0", optional = true, path = "rustyline-derive" }
rustyline-derive = { version = "0.11.0", optional = true, path = "rustyline-derive" }

[target.'cfg(unix)'.dependencies]
nix = { version = "0.29", 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 = { version = "0.3.3", optional = true }
buffer-redux = { version = "1.0", optional = true, default-features = false }

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.59.0", features = ["Win32_Foundation", "Win32_System_Console", "Win32_Security", "Win32_System_Threading", "Win32_UI_Input_KeyboardAndMouse"] }
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]
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 = "14.0.0"
rustyline = "15.0.0"
```

## Features
Expand Down
9 changes: 7 additions & 2 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.10.0"
version = "0.11.0"
authors = ["gwenn"]
edition = "2018"
description = "Rustyline macros implementation of #[derive(Completer, Helper, Hinter, Highlighter)]"
Expand All @@ -19,6 +19,11 @@ maintenance = { status = "actively-developed" }
proc-macro = true

[dependencies]
syn = { version = "2.0.72", default-features = false, features = ["derive", "parsing", "printing", "proc-macro"] }
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 }
2 changes: 1 addition & 1 deletion rustyline-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn get_field_by_attr<'a>(data: &'a Data, ident: &str) -> Option<(usize, &'a Fiel
attr.path().is_ident("rustyline")
&& attr
.parse_args::<Path>()
.map_or(false, |arg| arg.is_ident(ident))
.is_ok_and(|arg| arg.is_ident(ident))
})
});

Expand Down
15 changes: 12 additions & 3 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ fn find_unclosed_quote(s: &str) -> Option<(usize, Quote)> {

#[cfg(test)]
mod tests {
use super::{Completer, FilenameCompleter};

#[test]
pub fn extract_word() {
let break_chars = super::default_break_chars;
Expand Down Expand Up @@ -604,16 +606,23 @@ mod tests {
#[test]
pub fn candidate_impls() {
struct StrCmp;
impl super::Completer for StrCmp {
impl Completer for StrCmp {
type Candidate = &'static str;
}
struct RcCmp;
impl super::Completer for RcCmp {
impl Completer for RcCmp {
type Candidate = std::rc::Rc<str>;
}
struct ArcCmp;
impl super::Completer for ArcCmp {
impl Completer for ArcCmp {
type Candidate = std::sync::Arc<str>;
}
}

#[test]
pub fn completer_impls() {
struct Wrapper<T: Completer>(T);
let boxed = Box::new(FilenameCompleter::new());
let _ = Wrapper(boxed);
}
}
20 changes: 10 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub struct Config {
/// Whether to use stdio or not
behavior: Behavior,
/// Horizontal space taken by a tab.
tab_stop: usize,
tab_stop: u8,
/// Indentation size for indent/dedent commands
indent_size: usize,
indent_size: u8,
/// Check if cursor position is at leftmost before displaying prompt
check_cursor_position: bool,
/// Bracketed paste on unix platform
Expand Down Expand Up @@ -160,11 +160,11 @@ impl Config {
///
/// By default, 8.
#[must_use]
pub fn tab_stop(&self) -> usize {
pub fn tab_stop(&self) -> u8 {
self.tab_stop
}

pub(crate) fn set_tab_stop(&mut self, tab_stop: usize) {
pub(crate) fn set_tab_stop(&mut self, tab_stop: u8) {
self.tab_stop = tab_stop;
}

Expand All @@ -180,11 +180,11 @@ impl Config {
///
/// By default, 2.
#[must_use]
pub fn indent_size(&self) -> usize {
pub fn indent_size(&self) -> u8 {
self.indent_size
}

pub(crate) fn set_indent_size(&mut self, indent_size: usize) {
pub(crate) fn set_indent_size(&mut self, indent_size: u8) {
self.indent_size = indent_size;
}

Expand Down Expand Up @@ -433,7 +433,7 @@ impl Builder {
///
/// By default, `8`
#[must_use]
pub fn tab_stop(mut self, tab_stop: usize) -> Self {
pub fn tab_stop(mut self, tab_stop: u8) -> Self {
self.set_tab_stop(tab_stop);
self
}
Expand All @@ -451,7 +451,7 @@ impl Builder {
///
/// By default, `2`
#[must_use]
pub fn indent_size(mut self, indent_size: usize) -> Self {
pub fn indent_size(mut self, indent_size: u8) -> Self {
self.set_indent_size(indent_size);
self
}
Expand Down Expand Up @@ -568,7 +568,7 @@ pub trait Configurer {
/// Horizontal space taken by a tab.
///
/// By default, `8`
fn set_tab_stop(&mut self, tab_stop: usize) {
fn set_tab_stop(&mut self, tab_stop: u8) {
self.config_mut().set_tab_stop(tab_stop);
}

Expand All @@ -581,7 +581,7 @@ pub trait Configurer {
/// Indentation size for indent/dedent commands
///
/// By default, `2`
fn set_indent_size(&mut self, size: usize) {
fn set_indent_size(&mut self, size: u8) {
self.config_mut().set_indent_size(size);
}

Expand Down
21 changes: 10 additions & 11 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use log::debug;
use std::fmt;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthChar;

use super::{Context, Helper, Result};
use crate::error::ReadlineError;
Expand All @@ -12,7 +11,7 @@ use crate::hint::Hint;
use crate::history::SearchDirection;
use crate::keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word};
use crate::keymap::{InputState, Invoke, Refresher};
use crate::layout::{Layout, Position};
use crate::layout::{cwidh, Layout, Position};
use crate::line_buffer::{
ChangeListener, DeleteListener, Direction, LineBuffer, NoListener, WordAction, MAX_LINE,
};
Expand Down Expand Up @@ -253,13 +252,13 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
}
}

impl<'out, 'prompt, H: Helper> Invoke for State<'out, 'prompt, H> {
impl<H: Helper> Invoke for State<'_, '_, H> {
fn input(&self) -> &str {
self.line.as_str()
}
}

impl<'out, 'prompt, H: Helper> Refresher for State<'out, 'prompt, H> {
impl<H: Helper> Refresher for State<'_, '_, H> {
fn refresh_line(&mut self) -> Result<()> {
let prompt_size = self.prompt_size;
self.hint();
Expand Down Expand Up @@ -325,7 +324,7 @@ impl<'out, 'prompt, H: Helper> Refresher for State<'out, 'prompt, H> {
}
}

impl<'out, 'prompt, H: Helper> fmt::Debug for State<'out, 'prompt, H> {
impl<H: Helper> fmt::Debug for State<'_, '_, H> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("State")
.field("prompt", &self.prompt)
Expand All @@ -338,7 +337,7 @@ impl<'out, 'prompt, H: Helper> fmt::Debug for State<'out, 'prompt, H> {
}
}

impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
impl<H: Helper> State<'_, '_, H> {
pub fn clear_screen(&mut self) -> Result<()> {
self.out.clear_screen()?;
self.layout.cursor = Position::default();
Expand All @@ -353,7 +352,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
let prompt_size = self.prompt_size;
let no_previous_hint = self.hint.is_none();
self.hint();
let width = ch.width().unwrap_or(0);
let width = cwidh(ch);
if n == 1
&& width != 0 // Ctrl-V + \t or \n ...
&& self.layout.cursor.col + width < self.out.get_columns()
Expand Down Expand Up @@ -382,7 +381,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
pub fn edit_replace_char(&mut self, ch: char, n: RepeatCount) -> Result<()> {
self.changes.begin();
let succeed = if let Some(chars) = self.line.delete(n, &mut self.changes) {
let count = chars.graphemes(true).count();
let count = RepeatCount::try_from(chars.graphemes(true).count()).unwrap();
self.line.insert(ch, count, &mut self.changes);
self.line.move_backward(1);
true
Expand Down Expand Up @@ -578,7 +577,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {

/// Moves the cursor to the same column in the line above
pub fn edit_move_line_up(&mut self, n: RepeatCount) -> Result<bool> {
if self.line.move_to_line_up(n) {
if self.line.move_to_line_up(n, &self.layout) {
self.move_cursor(CmdKind::MoveCursor)?;
Ok(true)
} else {
Expand All @@ -588,7 +587,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {

/// Moves the cursor to the same column in the line above
pub fn edit_move_line_down(&mut self, n: RepeatCount) -> Result<bool> {
if self.line.move_to_line_down(n) {
if self.line.move_to_line_down(n, &self.layout) {
self.move_cursor(CmdKind::MoveCursor)?;
Ok(true)
} else {
Expand Down Expand Up @@ -732,7 +731,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
}

/// Change the indentation of the lines covered by movement
pub fn edit_indent(&mut self, mvt: &Movement, amount: usize, dedent: bool) -> Result<()> {
pub fn edit_indent(&mut self, mvt: &Movement, amount: u8, dedent: bool) -> Result<()> {
if self.line.indent(mvt, amount, dedent, &mut self.changes) {
self.refresh_line()
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl MemHistory {
return true;
}
if line.is_empty()
|| (self.ignore_space && line.chars().next().map_or(true, char::is_whitespace))
|| (self.ignore_space && line.chars().next().is_none_or(char::is_whitespace))
{
return true;
}
Expand Down
11 changes: 8 additions & 3 deletions src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{Config, EditMode};
use crate::{Event, EventContext, EventHandler};

/// The number of times one command should be repeated.
pub type RepeatCount = usize;
pub type RepeatCount = u16;

/// Commands
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -183,7 +183,10 @@ impl Cmd {
let last_insert = wrt.last_insert();
if let Movement::ForwardChar(0) = mvt {
Self::Replace(
Movement::ForwardChar(last_insert.as_ref().map_or(0, String::len)),
Movement::ForwardChar(
RepeatCount::try_from(last_insert.as_ref().map_or(0, String::len))
.unwrap(),
),
last_insert,
)
} else {
Expand Down Expand Up @@ -441,6 +444,8 @@ impl<'b> InputState<'b> {
tty::Event::ExternalPrint(msg) => {
wrt.external_print(msg)?;
}
#[cfg(target_os = "macos")]
_ => {}
}
}
}
Expand Down Expand Up @@ -1125,7 +1130,7 @@ impl<'b> InputState<'b> {
}

#[cfg(feature = "custom-bindings")]
impl<'b> InputState<'b> {
impl InputState<'_> {
/// Application customized binding
fn custom_binding(
&self,
Expand Down
17 changes: 15 additions & 2 deletions src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
use std::cmp::Ordering;

/// Height, width
pub type Unit = u16;
/// Character width / number of columns
pub(crate) fn cwidh(c: char) -> Unit {
use unicode_width::UnicodeWidthChar;
Unit::try_from(c.width().unwrap_or(0)).unwrap()
}
/// String width / number of columns
pub(crate) fn swidth(s: &str) -> Unit {
use unicode_width::UnicodeWidthStr;
Unit::try_from(s.width()).unwrap()
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Position {
pub col: usize, // The leftmost column is number 0.
pub row: usize, // The highest row is number 0.
pub col: Unit, // The leftmost column is number 0.
pub row: Unit, // The highest row is number 0.
}

impl PartialOrd for Position {
Expand Down
Loading

0 comments on commit b74696f

Please sign in to comment.