diff --git a/cspell.json b/cspell.json index cc37ffa4..adef6e12 100644 --- a/cspell.json +++ b/cspell.json @@ -1 +1 @@ -{"flagWords":[],"language":"en","words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","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","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","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","imagesize","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"],"version":"0.2"} \ No newline at end of file +{"language":"en","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","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","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","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","imagesize","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","subsec"],"version":"0.2"} \ No newline at end of file diff --git a/yazi-config/preset/keymap.toml b/yazi-config/preset/keymap.toml index daa6b442..5139472d 100644 --- a/yazi-config/preset/keymap.toml +++ b/yazi-config/preset/keymap.toml @@ -119,6 +119,7 @@ keymap = [ { on = [ ",", "N" ], run = "sort natural --reverse", desc = "Sort naturally (reverse)" }, { on = [ ",", "s" ], run = "sort size --reverse=no", desc = "Sort by size" }, { on = [ ",", "S" ], run = "sort size --reverse", desc = "Sort by size (reverse)" }, + { on = [ ",", "r" ], run = "sort random --reverse=no", desc = "Sort randomly" }, # Tabs { on = "t", run = "tab_create --current", desc = "Create a new tab with CWD" }, diff --git a/yazi-config/src/manager/sorting.rs b/yazi-config/src/manager/sorting.rs index 6d060090..648efb87 100644 --- a/yazi-config/src/manager/sorting.rs +++ b/yazi-config/src/manager/sorting.rs @@ -14,6 +14,7 @@ pub enum SortBy { Alphabetical, Natural, Size, + Random, } impl FromStr for SortBy { @@ -28,6 +29,7 @@ impl FromStr for SortBy { "alphabetical" => Self::Alphabetical, "natural" => Self::Natural, "size" => Self::Size, + "random" => Self::Random, _ => bail!("invalid sort_by value: {s}"), }) } @@ -49,6 +51,7 @@ impl Display for SortBy { Self::Alphabetical => "alphabetical", Self::Natural => "natural", Self::Size => "size", + Self::Random => "random", }) } } diff --git a/yazi-core/src/folder/sorter.rs b/yazi-core/src/folder/sorter.rs index eb7079b7..e3bae173 100644 --- a/yazi-core/src/folder/sorter.rs +++ b/yazi-core/src/folder/sorter.rs @@ -1,7 +1,7 @@ use std::{cmp::Ordering, collections::HashMap, mem}; use yazi_config::manager::SortBy; -use yazi_shared::{fs::{File, Url}, natsort, Transliterator}; +use yazi_shared::{fs::{File, Url}, natsort, LcgRng, Transliterator}; #[derive(Clone, Copy, Default, PartialEq)] pub struct FilesSorter { @@ -60,6 +60,10 @@ impl FilesSorter { let ord = self.cmp(aa.unwrap_or(a.len), bb.unwrap_or(b.len), self.promote(a, b)); if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } }), + SortBy::Random => { + let mut rng = LcgRng::default(); + items.sort_unstable_by(|a, b| self.cmp(rng.next(), rng.next(), self.promote(a, b))) + } } } diff --git a/yazi-shared/src/lib.rs b/yazi-shared/src/lib.rs index dc11a9b3..2d7d86ea 100644 --- a/yazi-shared/src/lib.rs +++ b/yazi-shared/src/lib.rs @@ -11,6 +11,7 @@ mod layer; mod natsort; mod number; mod os; +mod rand; mod ro_cell; mod terminal; pub mod theme; @@ -29,6 +30,7 @@ pub use natsort::*; pub use number::*; #[cfg(unix)] pub use os::*; +pub use rand::*; pub use ro_cell::*; pub use terminal::*; pub use throttle::*; diff --git a/yazi-shared/src/rand.rs b/yazi-shared/src/rand.rs new file mode 100644 index 00000000..20271405 --- /dev/null +++ b/yazi-shared/src/rand.rs @@ -0,0 +1,27 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +pub struct LcgRng { + seed: u64, +} + +impl LcgRng { + const A: u64 = 6364136223846793005; + const C: u64 = 1; + const M: u64 = u64::MAX; +} + +impl Iterator for LcgRng { + type Item = u64; + + fn next(&mut self) -> Option { + self.seed = Self::A.wrapping_mul(self.seed).wrapping_add(Self::C) % Self::M; + Some(self.seed) + } +} + +impl Default for LcgRng { + fn default() -> Self { + let time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards"); + Self { seed: time.as_secs() ^ time.subsec_nanos() as u64 } + } +}