diff --git a/Cargo.lock b/Cargo.lock index d7d4c172..e99e9f20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,6 +60,55 @@ dependencies = [ "thiserror", ] +[[package]] +name = "anstream" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd" + +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", +] + [[package]] name = "anyhow" version = "1.0.72" @@ -193,9 +242,9 @@ checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" [[package]] name = "cc" -version = "1.0.81" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c6b2562119bf28c3439f7f02db99faf0aa1a8cdfe5772a2ee155d32227239f0" +checksum = "305fe645edc1442a0fa8b6726ba61d422798d37a52e12eaecf4b022ebbb88f01" dependencies = [ "libc", ] @@ -218,12 +267,59 @@ dependencies = [ "winapi", ] +[[package]] +name = "clap" +version = "4.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c27cdf28c0f604ba3f512b0c9a409f8de8513e4816705deb0498b627e7c3a3fd" +dependencies = [ + "clap_builder", + "clap_derive", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08a9f1ab5e9f01a9b81f202e8562eb9a10de70abf9eaeac1be465c28b75aa4aa" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" + [[package]] name = "color_quant" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "concurrent-queue" version = "2.2.0" @@ -238,6 +334,7 @@ name = "config" version = "0.1.0" dependencies = [ "anyhow", + "clap", "crossterm 0.27.0", "futures", "glob", @@ -419,6 +516,27 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys 0.48.0", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "event-listener" version = "2.5.3" @@ -664,6 +782,12 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.2" @@ -705,9 +829,9 @@ dependencies = [ [[package]] name = "image" -version = "0.24.6" +version = "0.24.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "527909aa81e20ac3a44803521443a765550f09b5130c2c2fa1ea59c2f8f50a3a" +checksum = "6f3dfdbdd72063086ff443e297b61695500514b1e41095b6fb9a5ab48a70a711" dependencies = [ "bytemuck", "byteorder", @@ -768,6 +892,17 @@ dependencies = [ "libc", ] +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "itoa" version = "1.0.9" @@ -845,6 +980,12 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" +[[package]] +name = "linux-raw-sys" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" + [[package]] name = "lock_api" version = "0.4.10" @@ -1254,6 +1395,19 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustix" +version = "0.38.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "172891ebdceb05aa0005f533a6cbfca599ddd7d966f6f5d4d9b2e70478e70399" +dependencies = [ + "bitflags 2.3.3", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.48.0", +] + [[package]] name = "ryu" version = "1.0.15" @@ -1283,18 +1437,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.182" +version = "1.0.183" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdb30a74471f5b7a1fa299f40b4bf1be93af61116df95465b2b5fc419331e430" +checksum = "32ac8da02677876d532745a130fc9d8e6edfa81a269b107c5b00829b91d8eb3c" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.182" +version = "1.0.183" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f4c2c6ea4bc09b5c419012eafcdb0fcef1d9119d626c8f3a0708a5b92d38a70" +checksum = "aafe972d60b0b9bee71a91b92fee2d4fb3c9d7e8f6b179aa99f27203d99a4816" dependencies = [ "proc-macro2", "quote", @@ -1424,6 +1578,12 @@ dependencies = [ "lock_api", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "2.0.28" @@ -1488,9 +1648,9 @@ dependencies = [ [[package]] name = "tiff" -version = "0.8.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7449334f9ff2baf290d55d73983a7d6fa15e01198faef72af07e2a8db851e471" +checksum = "6d172b0f4d3fba17ba89811858b9d3d97f928aece846475bbda076ca46736211" dependencies = [ "flate2", "jpeg-decoder", @@ -1741,6 +1901,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "valuable" version = "0.1.0" diff --git a/README.md b/README.md index b4d2cb15..291b4a16 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,19 @@ yazi If you want to use your own config, copy the [config folder](https://github.com/sxyazi/yazi/tree/main/config/preset) to `~/.config/yazi`, and modify it as you like. +There is a wrapper of yazi that provides the ability to change the current shell's working directory when yazi exited, feel free to use it: + +```bash +function ya() { + tmp="$(mktemp -t "yazi-cwd")" + yazi --cwd-file="$tmp" + if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then + cd -- "$cwd" + fi + rm -f -- "$tmp" +} +``` + ## Image Preview | Platform | Protocol | Support | diff --git a/adaptor/src/adaptor.rs b/adaptor/src/adaptor.rs index f5a96de0..1604fdae 100644 --- a/adaptor/src/adaptor.rs +++ b/adaptor/src/adaptor.rs @@ -11,6 +11,7 @@ use crate::{Image, Sixel}; static IMAGE_SHOWN: AtomicBool = AtomicBool::new(false); +#[allow(clippy::type_complexity)] static UEBERZUG: Lazy>>> = Lazy::new(|| if PREVIEW.adaptor.needs_ueberzug() { Ueberzug::start().ok() } else { None }); diff --git a/adaptor/src/image.rs b/adaptor/src/image.rs index 1647a342..b1d6cbc3 100644 --- a/adaptor/src/image.rs +++ b/adaptor/src/image.rs @@ -1,7 +1,7 @@ use std::{path::{Path, PathBuf}, sync::Arc}; use anyhow::Result; -use config::{MANAGER, PREVIEW}; +use config::{BOOT, PREVIEW}; use image::{imageops::FilterType, DynamicImage, ImageFormat}; use md5::{Digest, Md5}; use shared::tty_ratio; @@ -57,8 +57,8 @@ impl Image { #[inline] pub fn cache(path: &Path) -> PathBuf { - MANAGER - .cache + BOOT + .cache_dir .join(format!("{:x}", Md5::new_with_prefix(path.to_string_lossy().as_bytes()).finalize())) } } diff --git a/adaptor/src/lib.rs b/adaptor/src/lib.rs index 8bf2d3c1..8e6f2bdc 100644 --- a/adaptor/src/lib.rs +++ b/adaptor/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(clippy::unit_arg)] + mod adaptor; mod image; mod iterm2; @@ -5,9 +7,9 @@ mod kitty; mod sixel; mod ueberzug; -pub use crate::adaptor::*; -pub use crate::image::*; -pub(self) use iterm2::*; -pub(self) use kitty::*; -pub(self) use sixel::*; -pub(self) use ueberzug::*; +use iterm2::*; +use kitty::*; +use sixel::*; +use ueberzug::*; + +pub use crate::{adaptor::*, image::*}; diff --git a/adaptor/src/sixel.rs b/adaptor/src/sixel.rs index 15714240..5a733f4e 100644 --- a/adaptor/src/sixel.rs +++ b/adaptor/src/sixel.rs @@ -55,7 +55,7 @@ impl Sixel { } for y in 0..img.height() { - let c = (b'?' + (1 << (y % 6))) as u8 as char; + let c = (b'?' + (1 << (y % 6))) as char; let mut last = 0; let mut repeat = 0usize; diff --git a/app/src/app.rs b/app/src/app.rs index cd0993d2..b7722650 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -1,7 +1,8 @@ use core::{emit, files::FilesOp, input::InputMode, Event}; +use std::os::unix::prelude::OsStrExt; use anyhow::{Ok, Result}; -use config::keymap::{Control, Key, KeymapLayer}; +use config::{keymap::{Control, Key, KeymapLayer}, BOOT}; use crossterm::event::KeyEvent; use shared::{absolute_path, Term}; use tokio::sync::oneshot; @@ -24,7 +25,10 @@ impl App { while let Some(event) = app.signals.recv().await { match event { - Event::Quit => break, + Event::Quit => { + app.dispatch_quit(); + break; + } Event::Key(key) => app.dispatch_key(key), Event::Paste(str) => app.dispatch_paste(str), Event::Render(_) => app.dispatch_render(), @@ -37,6 +41,13 @@ impl App { Ok(()) } + fn dispatch_quit(&mut self) { + if let Some(p) = &BOOT.cwd_file { + let cwd = self.cx.manager.cwd().as_os_str(); + std::fs::write(p, cwd.as_bytes()).ok(); + } + } + fn dispatch_key(&mut self, key: KeyEvent) { let key = Key::from(key); if Executor::handle(&mut self.cx, key) { @@ -56,7 +67,7 @@ impl App { fn dispatch_render(&mut self) { if let Some(term) = &mut self.term { let _ = term.draw(|f| { - f.render_widget(Root::new(&mut self.cx), f.size()); + f.render_widget(Root::new(&self.cx), f.size()); if let Some((x, y)) = self.cx.cursor() { f.set_cursor(x, y); diff --git a/app/src/context.rs b/app/src/context.rs index 3c047e5c..18dc162f 100644 --- a/app/src/context.rs +++ b/app/src/context.rs @@ -14,7 +14,7 @@ pub struct Ctx { impl Ctx { pub(super) fn new() -> Self { Self { - manager: Manager::new(), + manager: Manager::make(), which: Default::default(), select: Default::default(), input: Default::default(), @@ -47,11 +47,7 @@ impl Ctx { #[inline] pub(super) fn image_layer(&self) -> bool { - match self.layer() { - KeymapLayer::Which => false, - KeymapLayer::Tasks => false, - _ => true, - } + !matches!(self.layer(), KeymapLayer::Which | KeymapLayer::Tasks) } pub(super) fn position(&self, pos: Position) -> Position { diff --git a/app/src/executor.rs b/app/src/executor.rs index 5bbc8b48..437bcef4 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -66,7 +66,7 @@ impl Executor { "back" => cx.manager.active_mut().back(), "forward" => cx.manager.active_mut().forward(), "cd" => { - let path = exec.args.get(0).map(|s| PathBuf::from(s)).unwrap_or_default(); + let path = exec.args.get(0).map(PathBuf::from).unwrap_or_default(); emit!(Cd(path)); false } @@ -86,7 +86,7 @@ impl Executor { "open" => cx.manager.open(exec.named.contains_key("interactive")), "yank" => cx.manager.yank(exec.named.contains_key("cut")), "paste" => { - let dest = cx.manager.current().cwd.clone(); + let dest = cx.manager.cwd().to_owned(); let (cut, src) = cx.manager.yanked(); let force = exec.named.contains_key("force"); @@ -133,7 +133,7 @@ impl Executor { // Tabs "tab_create" => { let path = if exec.named.contains_key("current") { - cx.manager.current().cwd.clone() + cx.manager.cwd().to_owned() } else { exec.args.get(0).map(|p| p.into()).unwrap_or("/".into()) }; @@ -182,7 +182,7 @@ impl Executor { "arrow" => { let step: isize = exec.args.get(0).and_then(|s| s.parse().ok()).unwrap_or(0); - if step > 0 { cx.select.next(step as usize) } else { cx.select.prev(step.abs() as usize) } + if step > 0 { cx.select.next(step as usize) } else { cx.select.prev(step.unsigned_abs()) } } _ => false, diff --git a/app/src/header/layout.rs b/app/src/header/layout.rs index 60978d01..a704bf4f 100644 --- a/app/src/header/layout.rs +++ b/app/src/header/layout.rs @@ -23,7 +23,7 @@ impl<'a> Widget for Layout<'a> { let location = if current.in_search { format!("{} (search)", readable_home(¤t.cwd)) } else { - format!("{}", readable_home(¤t.cwd)) + readable_home(¤t.cwd) }; Paragraph::new(location).style(Style::new().fg(Color::Cyan)).render(chunks[0], buf); diff --git a/app/src/header/mod.rs b/app/src/header/mod.rs index 6587c464..bf8f0116 100644 --- a/app/src/header/mod.rs +++ b/app/src/header/mod.rs @@ -2,4 +2,4 @@ mod layout; mod tabs; pub(super) use layout::*; -pub(self) use tabs::*; +use tabs::*; diff --git a/app/src/logs.rs b/app/src/logs.rs index 49604dd0..0c52703b 100644 --- a/app/src/logs.rs +++ b/app/src/logs.rs @@ -1,5 +1,5 @@ use anyhow::{Context, Result}; -use config::LOG; +use config::BOOT; use tracing_appender::non_blocking::WorkerGuard; use tracing_subscriber::{fmt, prelude::__tracing_subscriber_SubscriberExt, Registry}; @@ -7,7 +7,7 @@ pub(super) struct Logs; impl Logs { pub(super) fn init() -> Result { - let appender = tracing_appender::rolling::never(&LOG.root, "yazi.log"); + let appender = tracing_appender::rolling::never(&BOOT.state_dir, "yazi.log"); let (handle, guard) = tracing_appender::non_blocking(appender); // let filter = EnvFilter::from_default_env(); diff --git a/app/src/main.rs b/app/src/main.rs index 049ddd3f..77ae1bec 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -1,3 +1,5 @@ +#![allow(clippy::module_inception)] + mod app; mod context; mod executor; @@ -12,12 +14,12 @@ mod status; mod tasks; mod which; -pub(self) use app::*; -pub(self) use context::*; -pub(self) use executor::*; -pub(self) use logs::*; -pub(self) use root::*; -pub(self) use signals::*; +use app::*; +use context::*; +use executor::*; +use logs::*; +use root::*; +use signals::*; #[tokio::main] async fn main() -> anyhow::Result<()> { diff --git a/app/src/manager/folder.rs b/app/src/manager/folder.rs index ddc20bb6..1ca704f3 100644 --- a/app/src/manager/folder.rs +++ b/app/src/manager/folder.rs @@ -38,7 +38,7 @@ impl<'a> Folder<'a> { .iter() .find(|x| x.matches(&file.path, mimetype.get(&file.path).cloned(), file.meta.is_dir())) .map(|x| x.style.get()) - .unwrap_or_else(|| Style::new()) + .unwrap_or_else(Style::new) } } diff --git a/app/src/manager/layout.rs b/app/src/manager/layout.rs index 57e174ec..5d401da6 100644 --- a/app/src/manager/layout.rs +++ b/app/src/manager/layout.rs @@ -31,7 +31,7 @@ impl<'a> Widget for Layout<'a> { // Parent let block = Block::new().borders(Borders::RIGHT).padding(Padding::new(1, 0, 0, 0)); - if let Some(ref parent) = manager.parent() { + if let Some(parent) = manager.parent() { Folder::new(self.cx, parent).render(block.inner(chunks[0]), buf); } block.render(chunks[0], buf); diff --git a/app/src/manager/mod.rs b/app/src/manager/mod.rs index 3a32499f..8e077468 100644 --- a/app/src/manager/mod.rs +++ b/app/src/manager/mod.rs @@ -2,6 +2,6 @@ mod folder; mod layout; mod preview; -pub(self) use folder::*; +use folder::*; pub(super) use layout::*; -pub(self) use preview::*; +use preview::*; diff --git a/app/src/status/mod.rs b/app/src/status/mod.rs index a020bb96..35d6b256 100644 --- a/app/src/status/mod.rs +++ b/app/src/status/mod.rs @@ -4,6 +4,6 @@ mod progress; mod right; pub(super) use layout::*; -pub(self) use left::*; -pub(self) use progress::*; -pub(self) use right::*; +use left::*; +use progress::*; +use right::*; diff --git a/app/src/status/right.rs b/app/src/status/right.rs index ef415ba6..ba7906cb 100644 --- a/app/src/status/right.rs +++ b/app/src/status/right.rs @@ -34,7 +34,7 @@ impl<'a> Right<'a> { .collect() } - fn position<'b>(&self) -> Vec { + fn position(&self) -> Vec { // Colors let mode = self.cx.manager.active().mode(); let primary = mode.color(&THEME.status.primary); diff --git a/app/src/tasks/mod.rs b/app/src/tasks/mod.rs index 0bf8cc59..712c459e 100644 --- a/app/src/tasks/mod.rs +++ b/app/src/tasks/mod.rs @@ -1,5 +1,5 @@ mod clear; mod layout; -pub(self) use clear::*; +use clear::*; pub(super) use layout::*; diff --git a/app/src/which/mod.rs b/app/src/which/mod.rs index 2b637f9b..2ed246ae 100644 --- a/app/src/which/mod.rs +++ b/app/src/which/mod.rs @@ -2,4 +2,4 @@ mod layout; mod side; pub(super) use layout::*; -pub(self) use side::*; +use side::*; diff --git a/config/Cargo.toml b/config/Cargo.toml index f259b1ef..a2c2e90a 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -8,6 +8,7 @@ shared = { path = "../shared" } # External dependencies anyhow = "^1" +clap = { version = "^4", features = [ "derive" ] } crossterm = "^0" futures = "^0" glob = "^0" diff --git a/config/src/boot/boot.rs b/config/src/boot/boot.rs new file mode 100644 index 00000000..dc41c5d0 --- /dev/null +++ b/config/src/boot/boot.rs @@ -0,0 +1,39 @@ +use std::{env, fs, path::PathBuf}; + +use clap::Parser; + +#[derive(Debug)] +pub struct Boot { + pub cwd: PathBuf, + pub cwd_file: Option, + pub cache_dir: PathBuf, + pub state_dir: PathBuf, +} + +#[derive(Debug, Parser)] +struct Args { + /// Write the cwd on exit to this file + #[arg(long)] + cwd_file: Option, +} + +impl Default for Boot { + fn default() -> Self { + let args = Args::parse(); + let boot = Self { + cwd: env::current_dir().unwrap_or("/".into()), + cwd_file: args.cwd_file, + cache_dir: env::temp_dir().join("yazi"), + state_dir: xdg::BaseDirectories::with_prefix("yazi").unwrap().get_state_home(), + }; + + if !boot.cache_dir.is_dir() { + fs::create_dir(&boot.cache_dir).unwrap(); + } + if !boot.state_dir.is_dir() { + fs::create_dir_all(&boot.state_dir).unwrap(); + } + + boot + } +} diff --git a/config/src/boot/mod.rs b/config/src/boot/mod.rs new file mode 100644 index 00000000..5922b49a --- /dev/null +++ b/config/src/boot/mod.rs @@ -0,0 +1,3 @@ +mod boot; + +pub use boot::*; diff --git a/config/src/keymap/exec.rs b/config/src/keymap/exec.rs index e64abd28..64a79f36 100644 --- a/config/src/keymap/exec.rs +++ b/config/src/keymap/exec.rs @@ -13,11 +13,11 @@ impl From<&str> for Exec { fn from(value: &str) -> Self { let mut exec = Self::default(); for x in value.split_whitespace() { - if x.starts_with("--") { - let mut it = x[2..].splitn(2, '='); - let name = it.next().unwrap(); + if let Some(kv) = x.strip_prefix("--") { + let mut it = kv.splitn(2, '='); + let key = it.next().unwrap(); let value = it.next().unwrap_or(""); - exec.named.insert(name.to_string(), value.to_string()); + exec.named.insert(key.to_string(), value.to_string()); } else if exec.cmd.is_empty() { exec.cmd = x.to_string(); } else { diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index a5d71d07..94e96fa7 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -122,7 +122,6 @@ impl ToString for Key { } let code = match self.code { - KeyCode::Char(' ') => "Space", KeyCode::Backspace => "Backspace", KeyCode::Enter => "Enter", KeyCode::Left => "Left", @@ -150,7 +149,7 @@ impl ToString for Key { KeyCode::F(12) => "F12", KeyCode::Esc => "Esc", - KeyCode::Char(c) if c == ' ' => "Space", + KeyCode::Char(' ') => "Space", KeyCode::Char(c) => { s.push(if self.shift { c.to_ascii_uppercase() } else { c }); "" diff --git a/config/src/keymap/keymap.rs b/config/src/keymap/keymap.rs index 862d6b43..3c864368 100644 --- a/config/src/keymap/keymap.rs +++ b/config/src/keymap/keymap.rs @@ -54,9 +54,11 @@ impl<'de> Deserialize<'de> for Keymap { } } -impl Keymap { - pub fn new() -> Self { toml::from_str(&MERGED_KEYMAP).unwrap() } +impl Default for Keymap { + fn default() -> Self { toml::from_str(&MERGED_KEYMAP).unwrap() } +} +impl Keymap { #[inline] pub fn get(&self, layer: KeymapLayer) -> &Vec { match layer { diff --git a/config/src/lib.rs b/config/src/lib.rs index 1758c142..af5a5fa7 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -1,5 +1,8 @@ +#![allow(clippy::module_inception)] + use once_cell::sync::Lazy; +mod boot; pub mod keymap; mod log; pub mod manager; @@ -12,18 +15,20 @@ pub mod theme; pub(crate) use pattern::*; pub(crate) use preset::*; -static MERGED_KEYMAP: Lazy = Lazy::new(|| Preset::keymap()); -static MERGED_THEME: Lazy = Lazy::new(|| Preset::theme()); -static MERGED_YAZI: Lazy = Lazy::new(|| Preset::yazi()); +static MERGED_KEYMAP: Lazy = Lazy::new(Preset::keymap); +static MERGED_THEME: Lazy = Lazy::new(Preset::theme); +static MERGED_YAZI: Lazy = Lazy::new(Preset::yazi); -pub static KEYMAP: Lazy = Lazy::new(|| keymap::Keymap::new()); -pub static LOG: Lazy = Lazy::new(|| log::Log::new()); -pub static MANAGER: Lazy = Lazy::new(|| manager::Manager::new()); -pub static OPEN: Lazy = Lazy::new(|| open::Open::new()); -pub static PREVIEW: Lazy = Lazy::new(|| preview::Preview::new()); -pub static THEME: Lazy = Lazy::new(|| theme::Theme::new()); +pub static BOOT: Lazy = Lazy::new(Default::default); +pub static KEYMAP: Lazy = Lazy::new(Default::default); +pub static LOG: Lazy = Lazy::new(Default::default); +pub static MANAGER: Lazy = Lazy::new(Default::default); +pub static OPEN: Lazy = Lazy::new(Default::default); +pub static PREVIEW: Lazy = Lazy::new(Default::default); +pub static THEME: Lazy = Lazy::new(Default::default); pub fn init() { + Lazy::force(&BOOT); Lazy::force(&KEYMAP); Lazy::force(&LOG); Lazy::force(&MANAGER); diff --git a/config/src/log/log.rs b/config/src/log/log.rs index f112cf1a..74433fdf 100644 --- a/config/src/log/log.rs +++ b/config/src/log/log.rs @@ -1,17 +1,14 @@ -use std::path::PathBuf; - -use serde::{de, Deserialize, Deserializer}; +use serde::{Deserialize, Deserializer}; use crate::MERGED_YAZI; #[derive(Debug)] pub struct Log { pub enabled: bool, - pub root: PathBuf, } -impl Log { - pub fn new() -> Self { toml::from_str(&MERGED_YAZI).unwrap() } +impl Default for Log { + fn default() -> Self { toml::from_str(&MERGED_YAZI).unwrap() } } impl<'de> Deserialize<'de> for Log { @@ -30,13 +27,6 @@ impl<'de> Deserialize<'de> for Log { let outer = Outer::deserialize(deserializer)?; - let root = xdg::BaseDirectories::with_prefix("yazi") - .map_err(|e| de::Error::custom(e.to_string()))? - .get_state_home(); - if !root.is_dir() { - std::fs::create_dir_all(&root).map_err(|e| de::Error::custom(e.to_string()))?; - } - - Ok(Self { enabled: outer.log.enabled, root }) + Ok(Self { enabled: outer.log.enabled }) } } diff --git a/config/src/manager/manager.rs b/config/src/manager/manager.rs index 49440da9..319ef7bd 100644 --- a/config/src/manager/manager.rs +++ b/config/src/manager/manager.rs @@ -1,5 +1,3 @@ -use std::{env, fs, path::PathBuf}; - use serde::Deserialize; use super::SortBy; @@ -7,11 +5,6 @@ use crate::MERGED_YAZI; #[derive(Debug, Deserialize)] pub struct Manager { - #[serde(skip)] - pub cwd: PathBuf, - #[serde(skip)] - pub cache: PathBuf, - // Sorting pub sort_by: SortBy, pub sort_reverse: bool, @@ -20,21 +13,13 @@ pub struct Manager { pub show_hidden: bool, } -impl Manager { - pub fn new() -> Self { +impl Default for Manager { + fn default() -> Self { #[derive(Deserialize)] struct Outer { manager: Manager, } - let mut manager = toml::from_str::(&MERGED_YAZI).unwrap().manager; - - manager.cwd = env::current_dir().unwrap_or("/".into()); - manager.cache = env::temp_dir().join("yazi"); - if !manager.cache.is_dir() { - fs::create_dir(&manager.cache).unwrap(); - } - - manager + toml::from_str::(&MERGED_YAZI).unwrap().manager } } diff --git a/config/src/open/open.rs b/config/src/open/open.rs index 68b43d91..f5280cec 100644 --- a/config/src/open/open.rs +++ b/config/src/open/open.rs @@ -20,9 +20,11 @@ struct OpenRule { use_: String, } -impl Open { - pub fn new() -> Self { toml::from_str(&MERGED_YAZI).unwrap() } +impl Default for Open { + fn default() -> Self { toml::from_str(&MERGED_YAZI).unwrap() } +} +impl Open { pub fn openers(&self, path: P, mime: M) -> Option> where P: AsRef, @@ -39,11 +41,8 @@ impl Open { }) } - pub fn common_openers<'a>( - &self, - targets: &[(impl AsRef, impl AsRef)], - ) -> Vec<&Opener> { - let grouped = targets.into_iter().filter_map(|(p, m)| self.openers(p, m)).collect::>(); + pub fn common_openers(&self, targets: &[(impl AsRef, impl AsRef)]) -> Vec<&Opener> { + let grouped = targets.iter().filter_map(|(p, m)| self.openers(p, m)).collect::>(); let flat = grouped.iter().flatten().cloned().collect::>(); flat.into_iter().filter(|o| grouped.iter().all(|g| g.contains(o))).collect() } diff --git a/config/src/preview/adaptor.rs b/config/src/preview/adaptor.rs index 2f5580e7..24bc1916 100644 --- a/config/src/preview/adaptor.rs +++ b/config/src/preview/adaptor.rs @@ -28,12 +28,13 @@ impl Default for PreviewAdaptor { match env::var("TERM_PROGRAM").unwrap_or_default().as_str() { "iTerm.app" => return Self::Iterm2, "WezTerm" => return Self::Kitty, + "vscode" => return Self::Sixel, "Hyper" => return Self::Sixel, _ => {} } match env::var("XDG_SESSION_TYPE").unwrap_or_default().as_str() { - "x11" => return Self::X11, - "wayland" => return Self::Wayland, + "x11" => Self::X11, + "wayland" => Self::Wayland, _ => Self::Chafa, } } @@ -56,11 +57,6 @@ impl ToString for PreviewAdaptor { impl PreviewAdaptor { #[inline] pub fn needs_ueberzug(&self) -> bool { - match self { - PreviewAdaptor::Kitty => false, - PreviewAdaptor::Iterm2 => false, - PreviewAdaptor::Sixel => false, - _ => true, - } + !matches!(self, PreviewAdaptor::Kitty | PreviewAdaptor::Iterm2 | PreviewAdaptor::Sixel) } } diff --git a/config/src/preview/preview.rs b/config/src/preview/preview.rs index d5180bc2..80058a4d 100644 --- a/config/src/preview/preview.rs +++ b/config/src/preview/preview.rs @@ -13,8 +13,8 @@ pub struct Preview { pub max_height: u32, } -impl Preview { - pub fn new() -> Self { +impl Default for Preview { + fn default() -> Self { #[derive(Deserialize)] struct Outer { preview: Preview, diff --git a/config/src/theme/theme.rs b/config/src/theme/theme.rs index 8b8adf19..34adc49e 100644 --- a/config/src/theme/theme.rs +++ b/config/src/theme/theme.rs @@ -69,8 +69,8 @@ pub struct Theme { pub icons: Vec, } -impl Theme { - pub fn new() -> Self { +impl Default for Theme { + fn default() -> Self { let mut theme: Self = toml::from_str(&MERGED_THEME).unwrap(); theme.preview.syntect_theme = futures::executor::block_on(absolute_path(&theme.preview.syntect_theme)); diff --git a/core/src/external/file.rs b/core/src/external/file.rs index 5b2c2913..e52771ee 100644 --- a/core/src/external/file.rs +++ b/core/src/external/file.rs @@ -19,13 +19,13 @@ pub async fn file(files: &[impl AsRef]) -> Result) -> Result<()> { if !output.status.success() { bail!("failed to generate PDF thumbnail: {}", String::from_utf8_lossy(&output.stderr)); } - Ok(Image::precache_anyway(output.stdout.into(), dest).await?) + Image::precache_anyway(output.stdout.into(), dest).await } diff --git a/core/src/external/rg.rs b/core/src/external/rg.rs index 9705f9c3..c63aa322 100644 --- a/core/src/external/rg.rs +++ b/core/src/external/rg.rs @@ -13,7 +13,7 @@ pub struct RgOpt { pub fn rg(opt: RgOpt) -> Result>> { let mut child = Command::new("rg") .current_dir(&opt.cwd) - .args(&["--color=never", "--files-with-matches", "--smart-case"]) + .args(["--color=never", "--files-with-matches", "--smart-case"]) .arg(if opt.hidden { "--hidden" } else { "--no-hidden" }) .arg(&opt.subject) .kill_on_drop(true) diff --git a/core/src/external/unar.rs b/core/src/external/unar.rs index b893b982..77270ba6 100644 --- a/core/src/external/unar.rs +++ b/core/src/external/unar.rs @@ -5,8 +5,8 @@ use tokio::{io::{AsyncReadExt, BufReader}, process::Command}; pub async fn unar_head(path: &Path, target: &Path) -> Result> { let mut child = Command::new("unar") - .args(&[path, target]) - .args(&["-o", "-"]) + .args([path, target]) + .args(["-o", "-"]) .stdout(Stdio::piped()) .stderr(Stdio::null()) .kill_on_drop(true) diff --git a/core/src/external/zoxide.rs b/core/src/external/zoxide.rs index 5bcb0657..fbd57671 100644 --- a/core/src/external/zoxide.rs +++ b/core/src/external/zoxide.rs @@ -9,7 +9,7 @@ pub struct ZoxideOpt { pub fn zoxide(opt: ZoxideOpt) -> Result>> { let child = Command::new("zoxide") - .args(&["query", "-i", "--exclude"]) + .args(["query", "-i", "--exclude"]) .arg(opt.cwd) .kill_on_drop(true) .stdout(Stdio::piped()) diff --git a/core/src/input/input.rs b/core/src/input/input.rs index 9f25fc44..67b7f2c1 100644 --- a/core/src/input/input.rs +++ b/core/src/input/input.rs @@ -103,7 +103,7 @@ impl Input { let snap = self.snap(); let b = self.handle_op( if step <= 0 { - snap.cursor.saturating_sub(step.abs() as usize) + snap.cursor.saturating_sub(step.unsigned_abs()) } else { snap.count().min(snap.cursor + step as usize) }, @@ -333,11 +333,7 @@ impl Input { pub fn selected(&self) -> Option { let snap = self.snap(); - let start = if let Some(s) = snap.op.start() { - s - } else { - return None; - }; + let start = snap.op.start()?; let (start, end) = if start < snap.cursor { (start, snap.cursor) } else { (snap.cursor + 1, start + 1) }; diff --git a/core/src/input/snaps.rs b/core/src/input/snaps.rs index 3b882a29..0dc203df 100644 --- a/core/src/input/snaps.rs +++ b/core/src/input/snaps.rs @@ -52,7 +52,7 @@ impl InputSnaps { #[inline] pub(super) fn catch(&mut self) { - let value = mem::replace(&mut self.versions[self.idx].value, String::new()); + let value = mem::take(&mut self.versions[self.idx].value); self.versions[self.idx] = self.current.clone(); self.versions[self.idx].value = value; self.versions[self.idx].reset(); diff --git a/core/src/lib.rs b/core/src/lib.rs index c34ce75d..d2b49c8b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,3 +1,10 @@ +#![allow( + clippy::if_same_then_else, + clippy::len_without_is_empty, + clippy::module_inception, + clippy::option_map_unit_fn +)] + mod blocker; mod event; pub mod external; diff --git a/core/src/manager/manager.rs b/core/src/manager/manager.rs index 3b047f16..be38d48a 100644 --- a/core/src/manager/manager.rs +++ b/core/src/manager/manager.rs @@ -1,4 +1,4 @@ -use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, mem, path::PathBuf}; +use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, mem, path::{Path, PathBuf}}; use anyhow::Error; use config::{open::Opener, OPEN}; @@ -17,9 +17,9 @@ pub struct Manager { } impl Manager { - pub fn new() -> Self { + pub fn make() -> Self { Self { - tabs: Tabs::new(), + tabs: Tabs::make(), yanked: Default::default(), watcher: Watcher::start(), @@ -28,9 +28,9 @@ impl Manager { } pub fn refresh(&mut self) { - env::set_current_dir(&self.current().cwd).ok(); + env::set_current_dir(self.cwd()).ok(); - self.watcher.trigger(&self.current().cwd); + self.watcher.trigger(self.cwd()); if let Some(p) = self.parent() { self.watcher.trigger(&p.cwd); } @@ -174,7 +174,7 @@ impl Manager { } pub fn create(&self) -> bool { - let cwd = self.current().cwd.clone(); + let cwd = self.cwd().to_owned(); tokio::spawn(async move { let result = emit!(Input(InputOpt::top("Create:"))); @@ -248,7 +248,7 @@ impl Manager { pub fn update_read(&mut self, op: FilesOp) -> bool { let path = op.path(); - let cwd = self.current().cwd.clone(); + let cwd = self.cwd().to_owned(); let hovered = self.hovered().map(|h| h.path()); let mut b = if cwd == path && !self.current().in_search { @@ -277,7 +277,7 @@ impl Manager { pub fn update_search(&mut self, op: FilesOp) -> bool { let path = op.path(); - if self.current().in_search && self.current().cwd == path { + if self.current().in_search && self.cwd() == path { return self.current_mut().update(op); } @@ -293,7 +293,7 @@ impl Manager { let path = op.path(); let op = FilesOp::read_empty(&path); - if path == self.current().cwd { + if path == self.cwd() { self.current_mut().update(op); } else if matches!(self.parent(), Some(p) if p.cwd == path) { self.active_mut().parent.as_mut().unwrap().update(op); @@ -338,6 +338,9 @@ impl Manager { } impl Manager { + #[inline] + pub fn cwd(&self) -> &Path { &self.current().cwd } + #[inline] pub fn tabs(&self) -> &Tabs { &self.tabs } diff --git a/core/src/manager/preview.rs b/core/src/manager/preview.rs index db303eb9..1d152180 100644 --- a/core/src/manager/preview.rs +++ b/core/src/manager/preview.rs @@ -45,7 +45,7 @@ impl Preview { } pub fn go(&mut self, path: &Path, mime: &str, show_image: bool) { - let kind = MimeKind::new(&mime); + let kind = MimeKind::new(mime); if !show_image && matches!(kind, MimeKind::Image | MimeKind::Video) { return; } else if self.same(path, mime) { @@ -99,7 +99,7 @@ impl Preview { } pub async fn folder(path: &Path) -> Result { - emit!(Files(match Files::read_dir(&path).await { + emit!(Files(match Files::read_dir(path).await { Ok(items) => FilesOp::Read(path.to_path_buf(), items), Err(_) => FilesOp::IOErr(path.to_path_buf()), })); diff --git a/core/src/manager/tab.rs b/core/src/manager/tab.rs index c7e1135e..719fb57d 100644 --- a/core/src/manager/tab.rs +++ b/core/src/manager/tab.rs @@ -23,7 +23,7 @@ impl Tab { Self { mode: Default::default(), current: Folder::new(path), - parent: path.parent().map(|p| Folder::new(p)), + parent: path.parent().map(Folder::new), search: None, @@ -54,7 +54,7 @@ impl Tab { let ok = if step > 0 { self.current.next(step as usize) } else { - self.current.prev(step.abs() as usize) + self.current.prev(step.unsigned_abs()) }; if !ok { return false; diff --git a/core/src/manager/tabs.rs b/core/src/manager/tabs.rs index d97c569f..3539aa99 100644 --- a/core/src/manager/tabs.rs +++ b/core/src/manager/tabs.rs @@ -1,6 +1,6 @@ use std::path::Path; -use config::MANAGER; +use config::BOOT; use super::Tab; use crate::emit; @@ -13,8 +13,8 @@ pub struct Tabs { } impl Tabs { - pub fn new() -> Self { - let mut tabs = Self { idx: usize::MAX, items: vec![Tab::new(&MANAGER.cwd)] }; + pub fn make() -> Self { + let mut tabs = Self { idx: usize::MAX, items: vec![Tab::new(&BOOT.cwd)] }; tabs.set_idx(0); tabs } @@ -53,7 +53,7 @@ impl Tabs { pub fn close(&mut self, idx: usize) -> bool { let len = self.items.len(); - if len < 2 || idx as usize >= len { + if len < 2 || idx >= len { return false; } @@ -70,7 +70,7 @@ impl Tabs { if rel > 0 { (self.idx + rel as usize).min(self.items.len() - 1) } else { - self.idx.saturating_sub(rel.abs() as usize) + self.idx.saturating_sub(rel.unsigned_abs()) } } diff --git a/core/src/manager/watcher.rs b/core/src/manager/watcher.rs index 5f99843b..18e4b334 100644 --- a/core/src/manager/watcher.rs +++ b/core/src/manager/watcher.rs @@ -56,7 +56,7 @@ impl Watcher { tx.blocking_send(path).ok(); tx.blocking_send(parent).ok(); } - _ => return, + _ => (), } } }, @@ -111,7 +111,7 @@ impl Watcher { self.watcher.unwatch(p).ok(); } for p in to_watch.clone().difference(&keys) { - if self.watcher.watch(&p, RecursiveMode::NonRecursive).is_err() { + if self.watcher.watch(p, RecursiveMode::NonRecursive).is_err() { to_watch.remove(p); } } diff --git a/core/src/tasks/running.rs b/core/src/tasks/running.rs index 5b068bfd..1f38f2b0 100644 --- a/core/src/tasks/running.rs +++ b/core/src/tasks/running.rs @@ -27,9 +27,7 @@ impl Running { pub(super) fn get_mut(&mut self, id: usize) -> Option<&mut Task> { self.all.get_mut(&id) } #[inline] - pub(super) fn get_id(&self, idx: usize) -> Option { - self.values().skip(idx).next().map(|t| t.id) - } + pub(super) fn get_id(&self, idx: usize) -> Option { self.values().nth(idx).map(|t| t.id) } #[inline] pub(super) fn len(&self) -> usize { self.all.len() } diff --git a/core/src/tasks/task.rs b/core/src/tasks/task.rs index fdee3349..c6010475 100644 --- a/core/src/tasks/task.rs +++ b/core/src/tasks/task.rs @@ -46,16 +46,16 @@ impl Task { } } -impl Into for &Task { - fn into(self) -> TaskSummary { +impl From<&Task> for TaskSummary { + fn from(task: &Task) -> Self { TaskSummary { - name: self.name.clone(), + name: task.name.clone(), - found: self.found, - processed: self.processed, + found: task.found, + processed: task.processed, - todo: self.todo, - done: self.done, + todo: task.todo, + done: task.done, } } } diff --git a/core/src/tasks/tasks.rs b/core/src/tasks/tasks.rs index c3e31ae3..8d0f952f 100644 --- a/core/src/tasks/tasks.rs +++ b/core/src/tasks/tasks.rs @@ -38,6 +38,7 @@ impl Tasks { true } + #[allow(clippy::should_implement_trait)] pub fn next(&mut self) -> bool { let limit = Self::limit().min(self.len()); @@ -132,7 +133,7 @@ impl Tasks { let mut openers = BTreeMap::new(); for (path, mime) in targets { if let Some(opener) = OPEN.openers(path, mime).and_then(|o| o.first().cloned()) { - openers.entry(opener).or_insert_with(|| vec![]).push(path.as_ref()); + openers.entry(opener).or_insert_with(Vec::new).push(path.as_ref()); } } for (opener, args) in openers { @@ -143,13 +144,13 @@ impl Tasks { pub fn file_open_with(&self, opener: &Opener, args: &[impl AsRef]) -> bool { if opener.spread { - self.scheduler.process_open(&opener, args); + self.scheduler.process_open(opener, args); return false; } for target in args { - self.scheduler.process_open(&opener, &[target]); + self.scheduler.process_open(opener, &[target]); } - return false; + false } pub fn file_cut(&self, src: &HashSet, dest: PathBuf, force: bool) -> bool { @@ -238,7 +239,7 @@ impl Tasks { pub fn precache_image(&self, mimetype: &BTreeMap) -> bool { let targets = mimetype - .into_iter() + .iter() .filter(|(_, m)| MimeKind::new(m) == MimeKind::Image) .map(|(p, _)| p.clone()) .collect::>(); @@ -251,7 +252,7 @@ impl Tasks { pub fn precache_video(&self, mimetype: &BTreeMap) -> bool { let targets = mimetype - .into_iter() + .iter() .filter(|(_, m)| MimeKind::new(m) == MimeKind::Video) .map(|(p, _)| p.clone()) .collect::>(); @@ -264,7 +265,7 @@ impl Tasks { pub fn precache_pdf(&self, mimetype: &BTreeMap) -> bool { let targets = mimetype - .into_iter() + .iter() .filter(|(_, m)| MimeKind::new(m) == MimeKind::PDF) .map(|(p, _)| p.clone()) .collect::>(); diff --git a/core/src/tasks/workers/precache.rs b/core/src/tasks/workers/precache.rs index 1b5097f7..30951710 100644 --- a/core/src/tasks/workers/precache.rs +++ b/core/src/tasks/workers/precache.rs @@ -21,7 +21,7 @@ pub(crate) struct Precache { pub(crate) enum PrecacheOp { Image(PrecacheOpImage), Video(PrecacheOpVideo), - PDF(PrecacheOpPDF), + Pdf(PrecacheOpPDF), } #[derive(Debug)] @@ -66,7 +66,7 @@ impl Precache { Ok(match self.rx.recv().await? { PrecacheOp::Image(t) => (t.id, PrecacheOp::Image(t)), PrecacheOp::Video(t) => (t.id, PrecacheOp::Video(t)), - PrecacheOp::PDF(t) => (t.id, PrecacheOp::PDF(t)), + PrecacheOp::Pdf(t) => (t.id, PrecacheOp::Pdf(t)), }) } @@ -91,7 +91,7 @@ impl Precache { external::ffmpegthumbnailer(&task.target, &cache).await.ok(); self.sch.send(TaskOp::Adv(task.id, 1, 0))?; } - PrecacheOp::PDF(task) => { + PrecacheOp::Pdf(task) => { let cache = Image::cache(&task.target); if fs::metadata(&cache).await.is_ok() { return Ok(self.sch.send(TaskOp::Adv(task.id, 1, 0))?); @@ -159,7 +159,7 @@ impl Precache { pub(crate) fn pdf(&self, id: usize, targets: Vec) -> Result<()> { for target in targets { self.sch.send(TaskOp::New(id, 0))?; - self.tx.send_blocking(PrecacheOp::PDF(PrecacheOpPDF { id, target }))?; + self.tx.send_blocking(PrecacheOp::Pdf(PrecacheOpPDF { id, target }))?; } self.done(id) } diff --git a/core/src/which/which.rs b/core/src/which/which.rs index f33e28ea..758bb101 100644 --- a/core/src/which/which.rs +++ b/core/src/which/which.rs @@ -22,18 +22,14 @@ impl Which { pub fn show(&mut self, key: &Key, layer: KeymapLayer) -> bool { self.layer = layer; self.times = 1; - self.cands = KEYMAP - .get(layer) - .into_iter() - .filter(|s| s.on.len() > 1 && s.on[0] == *key) - .cloned() - .collect(); + self.cands = + KEYMAP.get(layer).iter().filter(|s| s.on.len() > 1 && s.on[0] == *key).cloned().collect(); self.switch(true); true } pub fn press(&mut self, key: Key) -> bool { - self.cands = mem::replace(&mut self.cands, Vec::new()) + self.cands = mem::take(&mut self.cands) .into_iter() .filter(|s| s.on.len() > self.times && s.on[self.times] == key) .collect(); @@ -49,7 +45,7 @@ impl Which { } self.times += 1; - return true; + true } #[inline] diff --git a/cspell.json b/cspell.json index 120b2a9b..ebb081b7 100644 --- a/cspell.json +++ b/cspell.json @@ -1 +1 @@ -{"version":"0.2","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"]} +{"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"],"version":"0.2","language":"en"} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 00000000..69622ca3 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,31 @@ +color = "Never" +condense_wildcard_suffixes = true +edition = "2021" +enum_discrim_align_threshold = 99 +fn_single_line = true +format_code_in_doc_comments = false +format_generated_files = false +format_macro_matchers = true +format_macro_bodies = true +format_strings = false +hard_tabs = true +hex_literal_case = "Lower" +hide_parse_errors = true +imports_indent = "Visual" +imports_layout = "Horizontal" +imports_granularity = "Crate" +newline_style = "Unix" +normalize_comments = true +normalize_doc_attributes = false +overflow_delimited_expr = true +reorder_impl_items = true +group_imports = "StdExternalCrate" +reorder_modules = true +struct_field_align_threshold = 99 +tab_spaces = 2 +unstable_features = true +use_field_init_shorthand = true +use_small_heuristics = "Max" +use_try_shorthand = true +version = "Two" +wrap_comments = true diff --git a/shared/src/fs.rs b/shared/src/fs.rs index 336d771c..75f9ce24 100644 --- a/shared/src/fs.rs +++ b/shared/src/fs.rs @@ -98,6 +98,7 @@ pub fn copy_with_progress(from: &Path, to: &Path) -> mpsc::Receiver String { use libc::{S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFSOCK, S_IRGRP, S_IROTH, S_IRUSR, S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR}; diff --git a/shared/src/mime.rs b/shared/src/mime.rs index a59df274..04831d6e 100644 --- a/shared/src/mime.rs +++ b/shared/src/mime.rs @@ -28,19 +28,8 @@ impl MimeKind { return false; } - let b = match parts[0] { - "application" => true, - "audio" => true, - "example" => true, - "font" => true, - "image" => true, - "message" => true, - "model" => true, - "multipart" => true, - "text" => true, - "video" => true, - _ => false, - }; + #[rustfmt::skip] + let b = matches!(parts[0], "application" | "audio" | "example" | "font" | "image" | "message" | "model" | "multipart" | "text" | "video"); b && !parts[1].is_empty() } diff --git a/shared/src/throttle.rs b/shared/src/throttle.rs index 9404655b..248eaa69 100644 --- a/shared/src/throttle.rs +++ b/shared/src/throttle.rs @@ -47,7 +47,7 @@ impl Throttle { where F: FnOnce(Vec), { - let mut buf = mem::replace(&mut *self.buf.lock(), Vec::new()); + let mut buf = mem::take(&mut *self.buf.lock()); buf.push(data); f(buf) }