Refactor the code

This commit is contained in:
sxyazi 2024-07-14 00:47:05 +08:00
parent 8b6f28f187
commit 1994b21897
No known key found for this signature in database
5 changed files with 34 additions and 30 deletions

View file

@ -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"}

View file

@ -120,7 +120,6 @@ keymap = [
{ on = [ ",", "s" ], run = "sort size --reverse=no", desc = "Sort by size" }, { on = [ ",", "s" ], run = "sort size --reverse=no", desc = "Sort by size" },
{ on = [ ",", "S" ], run = "sort size --reverse", desc = "Sort by size (reverse)" }, { on = [ ",", "S" ], run = "sort size --reverse", desc = "Sort by size (reverse)" },
{ on = [ ",", "r" ], run = "sort random --reverse=no", desc = "Sort randomly" }, { on = [ ",", "r" ], run = "sort random --reverse=no", desc = "Sort randomly" },
{ on = [ ",", "R" ], run = "sort random --reverse", desc = "Sort randomly (reverse)" },
# Tabs # Tabs
{ on = "t", run = "tab_create --current", desc = "Create a new tab with CWD" }, { on = "t", run = "tab_create --current", desc = "Create a new tab with CWD" },

View file

@ -1,24 +1,7 @@
use std::{cmp::Ordering, collections::HashMap, mem, time::{SystemTime, UNIX_EPOCH}}; use std::{cmp::Ordering, collections::HashMap, mem};
use yazi_config::manager::SortBy; use yazi_config::manager::SortBy;
use yazi_shared::{fs::{File, Url}, natsort, Transliterator}; use yazi_shared::{fs::{File, Url}, natsort, LcgRng, Transliterator};
struct LcgRng {
seed: u64,
}
impl LcgRng {
const A: u64 = 6364136223846793005;
const C: u64 = 1;
const M: u64 = u64::MAX;
fn new(seed: u64) -> Self { LcgRng { seed } }
fn next(&mut self) -> u64 {
self.seed = (Self::A.wrapping_mul(self.seed).wrapping_add(Self::C)) % Self::M;
self.seed
}
}
#[derive(Clone, Copy, Default, PartialEq)] #[derive(Clone, Copy, Default, PartialEq)]
pub struct FilesSorter { pub struct FilesSorter {
@ -78,15 +61,8 @@ impl FilesSorter {
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
}), }),
SortBy::Random => { SortBy::Random => {
let start = SystemTime::now(); let mut rng = LcgRng::default();
let since_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards"); items.sort_unstable_by(|a, b| self.cmp(rng.next(), rng.next(), self.promote(a, b)))
let seed = since_epoch.as_secs() ^ since_epoch.subsec_nanos() as u64;
let mut rng = LcgRng::new(seed);
items.sort_unstable_by(|a, b| {
let aa = rng.next();
let bb = rng.next();
self.cmp(aa, bb, self.promote(a, b))
})
} }
} }
} }

View file

@ -11,6 +11,7 @@ mod layer;
mod natsort; mod natsort;
mod number; mod number;
mod os; mod os;
mod rand;
mod ro_cell; mod ro_cell;
mod terminal; mod terminal;
pub mod theme; pub mod theme;
@ -29,6 +30,7 @@ pub use natsort::*;
pub use number::*; pub use number::*;
#[cfg(unix)] #[cfg(unix)]
pub use os::*; pub use os::*;
pub use rand::*;
pub use ro_cell::*; pub use ro_cell::*;
pub use terminal::*; pub use terminal::*;
pub use throttle::*; pub use throttle::*;

27
yazi-shared/src/rand.rs Normal file
View 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 }
}
}