Skip to content

Commit 4e32044

Browse files
lmneksxyazi
andauthored
feat: new casefy command for case conversion (#3235)
Co-authored-by: sxyazi <[email protected]>
1 parent 782ecb1 commit 4e32044

File tree

8 files changed

+70
-7
lines changed

8 files changed

+70
-7
lines changed

cspell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"0.2","flagWords":[],"language":"en","words":["Punct","KEYMAP","splitn","crossterm","YAZI","peekable","ratatui","syntect","pbpaste","pbcopy","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","Konsole","Überzug","pkgs","pdftoppm","poppler","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds","tcsi","Hyprland","Wayfire","SWAYSOCK","btime","nsec","codegen","gethostname","fchmod","fdfind","Rustc","rustc","ffprobe","vframes","luma","obase","outln","errln","tmtheme","twox","cfgs","fstype","objc","rdev","runloop","exfat","rclone","DECRQSS","DECSCUSR","libvterm","Uninit","lockin","rposition","resvg","foldhash","tilded","futs","chdir","hashbrown","JEMALLOC","RUSTFLAGS","RDONLY","GETPATH","fcntl","casefold","inodes","Splatable"]}
1+
{"language":"en","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","peekable","ratatui","syntect","pbpaste","pbcopy","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","Konsole","Überzug","pkgs","pdftoppm","poppler","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds","tcsi","Hyprland","Wayfire","SWAYSOCK","btime","nsec","codegen","gethostname","fchmod","fdfind","Rustc","rustc","ffprobe","vframes","luma","obase","outln","errln","tmtheme","twox","cfgs","fstype","objc","rdev","runloop","exfat","rclone","DECRQSS","DECSCUSR","libvterm","Uninit","lockin","rposition","resvg","foldhash","tilded","futs","chdir","hashbrown","JEMALLOC","RUSTFLAGS","RDONLY","GETPATH","fcntl","casefold","inodes","Splatable","casefied"],"version":"0.2"}

yazi-config/preset/keymap-default.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ keymap = [
301301
{ on = "p", run = "paste", desc = "Paste copied characters after the cursor" },
302302
{ on = "P", run = "paste --before", desc = "Paste copied characters before the cursor" },
303303

304-
# Undo/Redo
305-
{ on = "u", run = "undo", desc = "Undo the last operation" },
306-
{ on = "<C-r>", run = "redo", desc = "Redo the last operation" },
304+
# Undo/Redo/Casefy
305+
{ on = "u", run = [ "undo", "casefy lower" ], desc = "Undo, or lowercase if in visual mode" },
306+
{ on = "U", run = "casefy upper", desc = "Uppercase" },
307+
{ on = "<C-r>", run = "redo", desc = "Redo the last operation" },
307308

308309
# Help
309310
{ on = "~", run = "help", desc = "Open help" },

yazi-parser/src/input/casefy.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use mlua::{ExternalError, FromLua, IntoLua, Lua, Value};
2+
use yazi_shared::event::CmdCow;
3+
4+
#[derive(Debug)]
5+
pub struct CasefyOpt {
6+
pub upper: bool,
7+
}
8+
9+
impl From<CmdCow> for CasefyOpt {
10+
fn from(c: CmdCow) -> Self { Self { upper: c.str(0) == "upper" } }
11+
}
12+
13+
impl FromLua for CasefyOpt {
14+
fn from_lua(_: Value, _: &Lua) -> mlua::Result<Self> { Err("unsupported".into_lua_err()) }
15+
}
16+
17+
impl IntoLua for CasefyOpt {
18+
fn into_lua(self, _: &Lua) -> mlua::Result<Value> { Err("unsupported".into_lua_err()) }
19+
}
20+
21+
impl CasefyOpt {
22+
pub fn transform(&self, s: &str) -> String {
23+
if self.upper { s.to_ascii_uppercase() } else { s.to_ascii_lowercase() }
24+
}
25+
}

yazi-parser/src/input/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
yazi_macro::mod_flat!(backspace backward close complete delete forward insert kill paste r#move show);
1+
yazi_macro::mod_flat!(backspace backward casefy close complete delete forward insert kill paste r#move show);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::ops::Range;
2+
3+
use anyhow::Result;
4+
use yazi_macro::{act, render, succ};
5+
use yazi_parser::input::CasefyOpt;
6+
use yazi_shared::data::Data;
7+
8+
use crate::input::{Input, op::InputOp};
9+
10+
impl Input {
11+
pub fn casefy(&mut self, opt: CasefyOpt) -> Result<Data> {
12+
let snap = self.snap_mut();
13+
if !matches!(snap.op, InputOp::Select(_)) {
14+
succ!();
15+
}
16+
17+
let range = snap.op.range(snap.cursor, true).unwrap();
18+
let Range { start, end } = snap.idx(range.start)..snap.idx(range.end);
19+
20+
let (start, end) = (start.unwrap(), end.unwrap());
21+
let casefied = opt.transform(&snap.value[start..end]);
22+
23+
snap.value.replace_range(start..end, &casefied);
24+
snap.op = InputOp::None;
25+
snap.cursor = range.start;
26+
self.snaps.tag(self.limit).then(|| self.flush_value());
27+
28+
act!(r#move, self)?;
29+
succ!(render!());
30+
}
31+
}

yazi-widgets/src/input/commands/commands.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ impl Input {
3535

3636
on!(undo);
3737
on!(redo);
38+
39+
on!(casefy);
3840
}
3941
InputMode::Insert => {
4042
on!(visual);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
yazi_macro::mod_flat!(backspace backward commands complete delete escape forward insert kill paste r#move r#type redo replace undo visual yank);
1+
yazi_macro::mod_flat!(backspace backward casefy commands complete delete escape forward insert kill paste r#move r#type redo replace undo visual yank);

yazi-widgets/src/input/commands/undo.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ use yazi_macro::{act, render, succ};
33
use yazi_parser::VoidOpt;
44
use yazi_shared::data::Data;
55

6-
use crate::input::{Input, InputMode};
6+
use crate::input::{Input, InputMode, InputOp};
77

88
impl Input {
99
pub fn undo(&mut self, _: VoidOpt) -> Result<Data> {
10+
if self.snap().op != InputOp::None {
11+
succ!();
12+
}
13+
1014
if !self.snaps.undo() {
1115
succ!();
1216
}

0 commit comments

Comments
 (0)