Skip to content

Commit

Permalink
core: add kill --big-word option
Browse files Browse the repository at this point in the history
  • Loading branch information
XOR-op committed Dec 23, 2023
1 parent 74ba02a commit 117c14e
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions yazi-core/src/input/commands/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ use yazi_shared::{event::Exec, CharKind};

use crate::input::Input;

#[derive(Debug, Copy, Clone)]
enum KillMode {
Default,
BigWord,
}

pub struct Opt<'a> {
kind: &'a str,
mode: KillMode,
}

impl<'a> From<&'a Exec> for Opt<'a> {
fn from(e: &'a Exec) -> Self {
Self { kind: e.args.first().map(|s| s.as_str()).unwrap_or_default() }
Self {
kind: e.args.first().map(|s| s.as_str()).unwrap_or_default(),
mode: e.named.get("big-word").map_or(KillMode::Default, |_| KillMode::BigWord),
}
}
}

Expand Down Expand Up @@ -45,7 +55,7 @@ impl Input {
///
/// Otherwise, returns how many characters to move to reach right *AFTER* the
/// word boundary, or the end of the iterator.
fn find_word_boundary(input: impl Iterator<Item = char> + Clone) -> usize {
fn find_word_boundary(input: impl Iterator<Item = char> + Clone, mode: KillMode) -> usize {
fn count_spaces(input: impl Iterator<Item = char>) -> usize {
// Move until we don't see any more whitespace.
input.take_while(|&c| CharKind::new(c) == CharKind::Space).count()
Expand All @@ -63,7 +73,7 @@ impl Input {
}

let space_or_trailing_slash = match input.clone().next() {
Some(std::path::MAIN_SEPARATOR) => 1,
Some(std::path::MAIN_SEPARATOR) if matches!(mode, KillMode::BigWord) => 1,
Some(c) if CharKind::new(c) == CharKind::Space => count_spaces(input.clone()),
_ => 0,
};
Expand All @@ -85,12 +95,12 @@ impl Input {
}
b"backward" => {
let end = snap.idx(snap.cursor).unwrap_or(snap.len());
let start = end - Self::find_word_boundary(snap.value[..end].chars().rev());
let start = end - Self::find_word_boundary(snap.value[..end].chars().rev(), opt.mode);
self.kill_range(start..end)
}
b"forward" => {
let start = snap.idx(snap.cursor).unwrap_or(snap.len());
let end = start + Self::find_word_boundary(snap.value[start..].chars());
let end = start + Self::find_word_boundary(snap.value[start..].chars(), opt.mode);
self.kill_range(start..end)
}
_ => false,
Expand Down

0 comments on commit 117c14e

Please sign in to comment.