From 82071bd06b3192552c2e3e023cd5c89aea034367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Tue, 27 Feb 2024 09:17:09 +0800 Subject: [PATCH] refactor: prefer `Display` over `ToString` (#734) --- yazi-boot/build.rs | 4 ++-- yazi-boot/src/boot.rs | 14 +++++++++----- yazi-config/src/keymap/key.rs | 20 ++++++++++---------- yazi-config/src/manager/sorting.rs | 11 +++++------ yazi-core/src/tab/commands/search.rs | 11 +++++------ yazi-core/src/tab/mode.rs | 11 +++++------ yazi-plugin/src/elements/bar.rs | 2 +- yazi-plugin/src/elements/border.rs | 2 +- yazi-plugin/src/elements/gauge.rs | 4 ++-- yazi-plugin/src/elements/line.rs | 2 +- yazi-plugin/src/elements/list.rs | 2 +- yazi-plugin/src/elements/paragraph.rs | 2 +- yazi-plugin/src/elements/span.rs | 2 +- yazi-plugin/src/elements/style.rs | 22 +++++++++++++--------- yazi-plugin/src/utils/layer.rs | 2 +- yazi-shared/src/fs/url.rs | 19 +++++++++++-------- yazi-shared/src/layer.rs | 11 +++++------ yazi-shared/src/xdg.rs | 3 --- 18 files changed, 74 insertions(+), 70 deletions(-) diff --git a/yazi-boot/build.rs b/yazi-boot/build.rs index 1112057f..2eacb6aa 100644 --- a/yazi-boot/build.rs +++ b/yazi-boot/build.rs @@ -1,7 +1,7 @@ #[path = "src/args.rs"] mod args; -use std::{env, error::Error, fs}; +use std::{env, error::Error}; use clap::CommandFactory; use clap_complete::{generate_to, Shell}; @@ -18,7 +18,7 @@ fn main() -> Result<(), Box> { let bin = "yazi"; let out = "completions"; - fs::create_dir_all(out)?; + std::fs::create_dir_all(out)?; generate_to(Shell::Bash, cmd, bin, out)?; generate_to(Shell::Fish, cmd, bin, out)?; generate_to(Shell::Zsh, cmd, bin, out)?; diff --git a/yazi-boot/src/boot.rs b/yazi-boot/src/boot.rs index 5de5cea0..a2383048 100644 --- a/yazi-boot/src/boot.rs +++ b/yazi-boot/src/boot.rs @@ -14,6 +14,7 @@ pub struct Boot { pub file: Option, pub config_dir: PathBuf, + pub flavor_dir: PathBuf, pub plugin_dir: PathBuf, pub state_dir: PathBuf, } @@ -36,19 +37,22 @@ impl Boot { impl Default for Boot { fn default() -> Self { + let config_dir = Xdg::config_dir().unwrap(); let (cwd, file) = Self::parse_entry(ARGS.entry.as_deref()); + let boot = Self { cwd, file, - config_dir: Xdg::config_dir().unwrap(), - plugin_dir: Xdg::plugin_dir().unwrap(), + flavor_dir: config_dir.join("flavors"), + plugin_dir: config_dir.join("plugins"), + config_dir, state_dir: Xdg::state_dir().unwrap(), }; - if !boot.state_dir.is_dir() { - fs::create_dir_all(&boot.state_dir).unwrap(); - } + fs::create_dir_all(&boot.flavor_dir).expect("Failed to create flavor directory"); + fs::create_dir_all(&boot.plugin_dir).expect("Failed to create plugin directory"); + fs::create_dir_all(&boot.state_dir).expect("Failed to create state directory"); boot } diff --git a/yazi-config/src/keymap/key.rs b/yazi-config/src/keymap/key.rs index cc907aa9..0de1ba7d 100644 --- a/yazi-config/src/keymap/key.rs +++ b/yazi-config/src/keymap/key.rs @@ -1,4 +1,4 @@ -use std::str::FromStr; +use std::{fmt::{Display, Write}, str::FromStr}; use anyhow::bail; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; @@ -132,22 +132,22 @@ impl TryFrom for Key { fn try_from(s: String) -> Result { Self::from_str(&s) } } -impl ToString for Key { - fn to_string(&self) -> String { +impl Display for Key { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if let Some(c) = self.plain() { let c = if self.shift { c.to_ascii_uppercase() } else { c }; - return if c == ' ' { "".to_string() } else { c.to_string() }; + return if c == ' ' { write!(f, "") } else { f.write_char(c) }; } - let mut s = "<".to_string(); + write!(f, "<")?; if self.ctrl { - s += "C-"; + write!(f, "C-")?; } if self.alt { - s += "A-"; + write!(f, "A-")?; } if self.shift && !matches!(self.code, KeyCode::Char(_)) { - s += "S-"; + write!(f, "S-")?; } let code = match self.code { @@ -181,12 +181,12 @@ impl ToString for Key { KeyCode::Char(' ') => "Space", KeyCode::Char(c) => { - s.push(if self.shift { c.to_ascii_uppercase() } else { c }); + f.write_char(if self.shift { c.to_ascii_uppercase() } else { c })?; "" } _ => "Unknown", }; - s + code + ">" + write!(f, "{}>", code) } } diff --git a/yazi-config/src/manager/sorting.rs b/yazi-config/src/manager/sorting.rs index 1656c5ee..6d060090 100644 --- a/yazi-config/src/manager/sorting.rs +++ b/yazi-config/src/manager/sorting.rs @@ -1,4 +1,4 @@ -use std::str::FromStr; +use std::{fmt::Display, str::FromStr}; use anyhow::bail; use serde::{Deserialize, Serialize}; @@ -39,9 +39,9 @@ impl TryFrom for SortBy { fn try_from(s: String) -> Result { Self::from_str(&s) } } -impl ToString for SortBy { - fn to_string(&self) -> String { - match self { +impl Display for SortBy { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { Self::None => "none", Self::Modified => "modified", Self::Created => "created", @@ -49,7 +49,6 @@ impl ToString for SortBy { Self::Alphabetical => "alphabetical", Self::Natural => "natural", Self::Size => "size", - } - .to_string() + }) } } diff --git a/yazi-core/src/tab/commands/search.rs b/yazi-core/src/tab/commands/search.rs index ae8c4640..c940e3a6 100644 --- a/yazi-core/src/tab/commands/search.rs +++ b/yazi-core/src/tab/commands/search.rs @@ -1,4 +1,4 @@ -use std::{mem, time::Duration}; +use std::{fmt::Display, mem, time::Duration}; use anyhow::bail; use tokio::pin; @@ -26,14 +26,13 @@ impl From for OptType { } } -impl ToString for OptType { - fn to_string(&self) -> String { - match self { +impl Display for OptType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { Self::Rg => "rg", Self::Fd => "fd", Self::None => "none", - } - .to_owned() + }) } } diff --git a/yazi-core/src/tab/mode.rs b/yazi-core/src/tab/mode.rs index 389a5daa..05c8d8e5 100644 --- a/yazi-core/src/tab/mode.rs +++ b/yazi-core/src/tab/mode.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeSet, mem}; +use std::{collections::BTreeSet, fmt::Display, mem}; #[derive(Clone, Debug, Default, Eq, PartialEq)] pub enum Mode { @@ -37,13 +37,12 @@ impl Mode { pub fn is_visual(&self) -> bool { matches!(self, Mode::Select(..) | Mode::Unset(..)) } } -impl ToString for Mode { - fn to_string(&self) -> String { - match self { +impl Display for Mode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { Mode::Normal => "normal", Mode::Select(..) => "select", Mode::Unset(..) => "unset", - } - .to_string() + }) } } diff --git a/yazi-plugin/src/elements/bar.rs b/yazi-plugin/src/elements/bar.rs index b96fb615..8428b5a8 100644 --- a/yazi-plugin/src/elements/bar.rs +++ b/yazi-plugin/src/elements/bar.rs @@ -51,7 +51,7 @@ impl UserData for Bar { let mut me = ud.borrow_mut::()?; match value { Value::Nil => me.style = None, - Value::Table(tb) => me.style = Some(Style::from(tb).0), + Value::Table(tb) => me.style = Some(Style::try_from(tb)?.0), Value::UserData(ud) => me.style = Some(ud.borrow::