From d3d9cf61def08b207041cfaa8cf44218bb0c7349 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 10 Aug 2024 20:32:10 +0800 Subject: [PATCH] Rebase --- cspell.json | 2 +- yazi-boot/src/boot.rs | 64 ++++++++--------------------------- yazi-core/src/manager/tabs.rs | 21 ++++++------ 3 files changed, 26 insertions(+), 61 deletions(-) diff --git a/cspell.json b/cspell.json index ee4662c7..f858aecd 100644 --- a/cspell.json +++ b/cspell.json @@ -1 +1 @@ -{"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","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek"],"version":"0.2","language":"en","flagWords":[]} \ 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","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds"],"version":"0.2"} \ No newline at end of file diff --git a/yazi-boot/src/boot.rs b/yazi-boot/src/boot.rs index b9dc26b8..9bbb142c 100644 --- a/yazi-boot/src/boot.rs +++ b/yazi-boot/src/boot.rs @@ -1,4 +1,4 @@ -use std::{collections::HashSet, ffi::OsString, path::{Path, PathBuf}}; +use std::{collections::HashSet, ffi::OsString, path::PathBuf}; use serde::Serialize; use yazi_shared::{fs::{current_cwd, expand_path}, Xdg}; @@ -6,7 +6,7 @@ use yazi_shared::{fs::{current_cwd, expand_path}, Xdg}; #[derive(Debug, Default, Serialize)] pub struct Boot { pub cwds: Vec, - pub files: Vec>, + pub files: Vec, pub local_events: HashSet, pub remote_events: HashSet, @@ -18,22 +18,20 @@ pub struct Boot { } impl Boot { - fn parse_entries(entries: Vec<&Path>) -> (Vec, Vec>) { - if entries.len() == 0 { - return (vec![current_cwd().unwrap()], vec![None]); + fn parse_entries(entries: &[PathBuf]) -> (Vec, Vec) { + if entries.is_empty() { + return (vec![current_cwd().unwrap()], vec![OsString::new()]); } - let mut cwds = vec![]; - let mut files = vec![]; - for entry in entries { - let _entry = expand_path(entry); - let parent = _entry.parent(); - if parent.is_none() || _entry.is_dir() { - cwds.push(_entry); - files.push(None); + let mut cwds = Vec::with_capacity(entries.len()); + let mut files = Vec::with_capacity(entries.len()); + for entry in entries.iter().map(expand_path) { + if let Some(p) = entry.parent().filter(|_| !entry.is_dir()) { + cwds.push(p.to_owned()); + files.push(entry.file_name().unwrap().to_owned()); } else { - cwds.push(parent.unwrap().to_owned()); - files.push(Some(_entry.file_name().unwrap().to_owned())); + cwds.push(entry); + files.push(OsString::new()); } } @@ -44,8 +42,7 @@ impl Boot { impl From<&crate::Args> for Boot { fn from(args: &crate::Args) -> Self { let config_dir = Xdg::config_dir(); - let entries = args.entries.iter().map(PathBuf::as_path).collect(); - let (cwds, files) = Self::parse_entries(entries); + let (cwds, files) = Self::parse_entries(&args.entries); let local_events = args .local_events @@ -72,36 +69,3 @@ impl From<&crate::Args> for Boot { } } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_parse_entries() { - use std::{env::temp_dir, fs}; - - let foo_dir = temp_dir().join(&Path::new("foo")); - let bar_dir = temp_dir().join(&Path::new("bar")); - let poem_path = &foo_dir.join(&Path::new("poem.txt")); - - let _ = fs::create_dir_all(&foo_dir); - let _ = fs::create_dir_all(&bar_dir); - let _ = - fs::OpenOptions::new().create(true).write(true).open(foo_dir.join(&Path::new("poem.txt"))); - - assert_eq!(Boot::parse_entries(vec![]), (vec![current_cwd().unwrap()], vec![None])); - assert_eq!(Boot::parse_entries(vec![&foo_dir]), (vec![foo_dir.clone()], vec![None])); - assert_eq!( - Boot::parse_entries(vec![&poem_path]), - (vec![foo_dir.clone()], vec![Some(OsString::from("poem.txt"))]) - ); - assert_eq!( - Boot::parse_entries(vec![&foo_dir, &bar_dir]), - (vec![foo_dir.clone(), bar_dir.clone()], vec![None, None]) - ); - - let _ = fs::remove_dir_all(&foo_dir); - let _ = fs::remove_dir_all(&bar_dir); - } -} diff --git a/yazi-core/src/manager/tabs.rs b/yazi-core/src/manager/tabs.rs index 56fd2e18..a005e571 100644 --- a/yazi-core/src/manager/tabs.rs +++ b/yazi-core/src/manager/tabs.rs @@ -13,17 +13,18 @@ pub struct Tabs { impl Tabs { pub fn make() -> Self { - let mut tabs = Self { cursor: 0, items: vec![] }; - for (i, file) in BOOT.files.iter().enumerate() { - let mut tab = Tab::default(); - if let Some(f) = file { - tab.reveal(Url::from(BOOT.cwds[i].join(f))); - } else { - tab.cd(Url::from(&BOOT.cwds[i])); - } - tabs.push(tab); - } + let mut tabs = + Self { cursor: 0, items: (0..BOOT.cwds.len()).map(|_| Tab::default()).collect() }; + tabs.reorder(); + for (i, tab) in tabs.iter_mut().enumerate() { + let file = &BOOT.files[i]; + if file.is_empty() { + tab.cd(Url::from(&BOOT.cwds[i])); + } else { + tab.reveal(Url::from(BOOT.cwds[i].join(file))); + } + } tabs }