mirror of
https://github.com/sxyazi/yazi.git
synced 2024-11-24 01:48:00 +03:00
feat: add random
option to the sort
command (#1291)
Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
parent
f5598ebb8a
commit
2cf722ed48
@ -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"}
|
||||
{"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"}
|
@ -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" },
|
||||
|
@ -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",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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::*;
|
||||
|
27
yazi-shared/src/rand.rs
Normal file
27
yazi-shared/src/rand.rs
Normal file
@ -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::Item> {
|
||||
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 }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user