diff options
author | Marcin Puc <5671049+tranzystorek-io@users.noreply.github.com> | 2021-12-07 11:24:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-07 10:24:42 +0000 |
commit | 56e85f87d6c365816cca71c496aa7e49709e0b11 (patch) | |
tree | 1dbc6db878e8da9544df9445d77d1cd665b9126b /zellij-utils/src | |
parent | d34e6240101d246f02921cbc909dcd04f648203e (diff) |
fix(style): various internal refactorings
Diffstat (limited to 'zellij-utils/src')
-rw-r--r-- | zellij-utils/src/consts.rs | 3 | ||||
-rw-r--r-- | zellij-utils/src/errors.rs | 4 | ||||
-rw-r--r-- | zellij-utils/src/input/config.rs | 2 | ||||
-rw-r--r-- | zellij-utils/src/input/keybinds.rs | 6 | ||||
-rw-r--r-- | zellij-utils/src/input/layout.rs | 33 | ||||
-rw-r--r-- | zellij-utils/src/input/plugins.rs | 4 | ||||
-rw-r--r-- | zellij-utils/src/logging.rs | 2 | ||||
-rw-r--r-- | zellij-utils/src/setup.rs | 58 | ||||
-rw-r--r-- | zellij-utils/src/shared.rs | 2 |
9 files changed, 58 insertions, 56 deletions
diff --git a/zellij-utils/src/consts.rs b/zellij-utils/src/consts.rs index f20dadaac..416153f20 100644 --- a/zellij-utils/src/consts.rs +++ b/zellij-utils/src/consts.rs @@ -47,8 +47,7 @@ lazy_static! { sock_dir.push(envs::get_session_name().unwrap()); sock_dir }; - pub static ref ZELLIJ_TMP_DIR: PathBuf = - PathBuf::from("/tmp/zellij-".to_string() + &format!("{}", *UID)); + pub static ref ZELLIJ_TMP_DIR: PathBuf = PathBuf::from(format!("/tmp/zellij-{}", *UID)); pub static ref ZELLIJ_TMP_LOG_DIR: PathBuf = ZELLIJ_TMP_DIR.join("zellij-log"); pub static ref ZELLIJ_TMP_LOG_FILE: PathBuf = ZELLIJ_TMP_LOG_DIR.join("zellij.log"); } diff --git a/zellij-utils/src/errors.rs b/zellij-utils/src/errors.rs index aeeed2030..d5407dd12 100644 --- a/zellij-utils/src/errors.rs +++ b/zellij-utils/src/errors.rs @@ -121,8 +121,8 @@ impl ErrorContext { /// Adds a call to this [`ErrorContext`]'s call stack representation. pub fn add_call(&mut self, call: ContextType) { - for ctx in self.calls.iter_mut() { - if *ctx == ContextType::Empty { + for ctx in &mut self.calls { + if let ContextType::Empty = ctx { *ctx = call; break; } diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index b38afacd9..04c9b103a 100644 --- a/zellij-utils/src/input/config.rs +++ b/zellij-utils/src/input/config.rs @@ -149,7 +149,7 @@ impl Config { // once serde-yaml supports zero-copy pub fn from_default_assets() -> ConfigResult { let cfg = String::from_utf8(setup::DEFAULT_CONFIG.to_vec())?; - Self::from_yaml(cfg.as_str()) + Self::from_yaml(&cfg) } /// Merges two Config structs into one Config struct diff --git a/zellij-utils/src/input/keybinds.rs b/zellij-utils/src/input/keybinds.rs index e3f88b1a0..fcc2e4a87 100644 --- a/zellij-utils/src/input/keybinds.rs +++ b/zellij-utils/src/input/keybinds.rs @@ -109,7 +109,7 @@ impl Keybinds { for mode in InputMode::iter() { if let Some(keybinds) = keybinds_from_yaml.get(&mode) { - for keybind in keybinds.iter() { + for keybind in keybinds { match keybind { KeyActionUnbind::Unbind(unbind) => { unbind_config.insert(mode, unbind.unbind.clone()); @@ -242,8 +242,8 @@ impl From<KeybindsFromYaml> for Keybinds { for mode in InputMode::iter() { let mut mode_keybinds = ModeKeybinds::new(); - for key_action in keybinds_from_yaml.keybinds.get(&mode).iter() { - for keybind in key_action.iter() { + if let Some(key_action) = keybinds_from_yaml.keybinds.get(&mode) { + for keybind in key_action { mode_keybinds = mode_keybinds.merge(ModeKeybinds::from(keybind.clone())); } } diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 76625645d..29b8de00f 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -254,8 +254,8 @@ impl LayoutFromYamlIntermediate { ) -> LayoutFromYamlIntermediateResult { match layout_dir { Some(dir) => Self::from_path(&dir.join(layout)) - .or_else(|_| LayoutFromYamlIntermediate::from_default_assets(layout.as_path())), - None => LayoutFromYamlIntermediate::from_default_assets(layout.as_path()), + .or_else(|_| LayoutFromYamlIntermediate::from_default_assets(layout)), + None => LayoutFromYamlIntermediate::from_default_assets(layout), } } // Currently still needed but on nightly @@ -277,19 +277,19 @@ impl LayoutFromYamlIntermediate { // once serde-yaml supports zero-copy pub fn default_from_assets() -> LayoutFromYamlIntermediateResult { let layout: LayoutFromYamlIntermediate = - serde_yaml::from_str(String::from_utf8(setup::DEFAULT_LAYOUT.to_vec())?.as_str())?; + serde_yaml::from_str(&String::from_utf8(setup::DEFAULT_LAYOUT.to_vec())?)?; Ok(layout) } pub fn strider_from_assets() -> LayoutFromYamlIntermediateResult { let layout: LayoutFromYamlIntermediate = - serde_yaml::from_str(String::from_utf8(setup::STRIDER_LAYOUT.to_vec())?.as_str())?; + serde_yaml::from_str(&String::from_utf8(setup::STRIDER_LAYOUT.to_vec())?)?; Ok(layout) } pub fn disable_status_from_assets() -> LayoutFromYamlIntermediateResult { let layout: LayoutFromYamlIntermediate = - serde_yaml::from_str(String::from_utf8(setup::NO_STATUS_LAYOUT.to_vec())?.as_str())?; + serde_yaml::from_str(&String::from_utf8(setup::NO_STATUS_LAYOUT.to_vec())?)?; Ok(layout) } } @@ -331,9 +331,10 @@ impl LayoutFromYaml { #[allow(clippy::ptr_arg)] pub fn from_dir(layout: &PathBuf, layout_dir: Option<&PathBuf>) -> LayoutFromYamlResult { match layout_dir { - Some(dir) => Self::new(&dir.join(layout)) - .or_else(|_| Self::from_default_assets(layout.as_path())), - None => Self::from_default_assets(layout.as_path()), + Some(dir) => { + Self::new(&dir.join(layout)).or_else(|_| Self::from_default_assets(layout)) + } + None => Self::from_default_assets(layout), } } @@ -372,19 +373,19 @@ impl LayoutFromYaml { // once serde-yaml supports zero-copy pub fn default_from_assets() -> LayoutFromYamlResult { let layout: LayoutFromYaml = - serde_yaml::from_str(String::from_utf8(setup::DEFAULT_LAYOUT.to_vec())?.as_str())?; + serde_yaml::from_str(&String::from_utf8(setup::DEFAULT_LAYOUT.to_vec())?)?; Ok(layout) } pub fn strider_from_assets() -> LayoutFromYamlResult { let layout: LayoutFromYaml = - serde_yaml::from_str(String::from_utf8(setup::STRIDER_LAYOUT.to_vec())?.as_str())?; + serde_yaml::from_str(&String::from_utf8(setup::STRIDER_LAYOUT.to_vec())?)?; Ok(layout) } pub fn disable_status_from_assets() -> LayoutFromYamlResult { let layout: LayoutFromYaml = - serde_yaml::from_str(String::from_utf8(setup::NO_STATUS_LAYOUT.to_vec())?.as_str())?; + serde_yaml::from_str(&String::from_utf8(setup::NO_STATUS_LAYOUT.to_vec())?)?; Ok(layout) } } @@ -465,7 +466,7 @@ pub struct TabLayout { impl TabLayout { fn check(&self) -> Result<TabLayout, ConfigError> { - for part in self.parts.iter() { + for part in &self.parts { part.check()?; if !part.name.is_empty() { return Err(ConfigError::LayoutNameInTab(LayoutNameInTabError)); @@ -479,7 +480,7 @@ impl Layout { pub fn total_terminal_panes(&self) -> usize { let mut total_panes = 0; total_panes += self.parts.len(); - for part in self.parts.iter() { + for part in &self.parts { match part.run { Some(Run::Command(_)) | None => { total_panes += part.total_terminal_panes(); @@ -493,7 +494,7 @@ impl Layout { pub fn total_borderless_panes(&self) -> usize { let mut total_borderless_panes = 0; total_borderless_panes += self.parts.iter().filter(|p| p.borderless).count(); - for part in self.parts.iter() { + for part in &self.parts { total_borderless_panes += part.total_borderless_panes(); } total_borderless_panes @@ -503,7 +504,7 @@ impl Layout { if self.parts.is_empty() { run_instructions.push(self.run.clone()); } - for part in self.parts.iter() { + for part in &self.parts { let mut current_runnables = part.extract_run_instructions(); run_instructions.append(&mut current_runnables); } @@ -549,7 +550,7 @@ fn layout_size(direction: Direction, layout: &Layout) -> usize { .parts .iter() .map(|p| child_layout_size(direction, layout.direction, p)) - .sum::<usize>(); + .sum(); max(size, children_size) } } diff --git a/zellij-utils/src/input/plugins.rs b/zellij-utils/src/input/plugins.rs index 6be975e4d..71ca264d5 100644 --- a/zellij-utils/src/input/plugins.rs +++ b/zellij-utils/src/input/plugins.rs @@ -18,7 +18,7 @@ pub use zellij_tile::data::PluginTag; lazy_static! { static ref DEFAULT_CONFIG_PLUGINS: PluginsConfig = { let cfg = String::from_utf8(setup::DEFAULT_CONFIG.to_vec()).unwrap(); - let cfg_yaml: ConfigFromYaml = serde_yaml::from_str(cfg.as_str()).unwrap(); + let cfg_yaml: ConfigFromYaml = serde_yaml::from_str(&cfg).unwrap(); PluginsConfig::try_from(cfg_yaml.plugins).unwrap() }; } @@ -271,7 +271,7 @@ mod tests { )?; let plugins = PluginsConfig::get_plugins_with_default(plugins.try_into()?); - assert_eq!(plugins.iter().collect::<Vec<_>>().len(), 4); + assert_eq!(plugins.iter().count(), 4); Ok(()) } diff --git a/zellij-utils/src/logging.rs b/zellij-utils/src/logging.rs index 74ec53979..31b4441f7 100644 --- a/zellij-utils/src/logging.rs +++ b/zellij-utils/src/logging.rs @@ -92,7 +92,7 @@ pub fn atomic_create_dir(dir_name: &Path) -> io::Result<()> { pub fn debug_to_file(message: &[u8], pid: RawFd) -> io::Result<()> { let mut path = PathBuf::new(); path.push(&*ZELLIJ_TMP_LOG_DIR); - path.push(format!("zellij-{}.log", pid.to_string())); + path.push(format!("zellij-{}.log", pid)); let mut file = fs::OpenOptions::new() .append(true) diff --git a/zellij-utils/src/setup.rs b/zellij-utils/src/setup.rs index 74f7c69a3..0de857249 100644 --- a/zellij-utils/src/setup.rs +++ b/zellij-utils/src/setup.rs @@ -12,7 +12,9 @@ use crate::{ }; use directories_next::BaseDirs; use serde::{Deserialize, Serialize}; -use std::{convert::TryFrom, io::Write, path::Path, path::PathBuf, process}; +use std::{ + convert::TryFrom, fmt::Write as FmtWrite, io::Write, path::Path, path::PathBuf, process, +}; use structopt::StructOpt; const CONFIG_LOCATION: &str = ".config/zellij"; @@ -47,7 +49,7 @@ fn default_config_dirs() -> Vec<Option<PathBuf>> { /// Looks for an existing dir, uses that, else returns a /// dir matching the config spec. pub fn get_default_data_dir() -> PathBuf { - vec![ + [ xdg_data_dir(), Path::new(SYSTEM_DEFAULT_DATA_DIR_PREFIX).join("share/zellij"), ] @@ -300,9 +302,9 @@ impl Setup { let mut message = String::new(); - message.push_str(&format!("[Version]: {:?}\n", VERSION)); + writeln!(&mut message, "[Version]: {:?}", VERSION).unwrap(); if let Some(config_dir) = config_dir { - message.push_str(&format!("[CONFIG DIR]: {:?}\n", config_dir)); + writeln!(&mut message, "[CONFIG DIR]: {:?}", config_dir).unwrap(); } else { message.push_str("[CONFIG DIR]: Not Found\n"); let mut default_config_dirs = default_config_dirs() @@ -314,32 +316,34 @@ impl Setup { " On your system zellij looks in the following config directories by default:\n", ); for dir in default_config_dirs { - message.push_str(&format!(" {:?}\n", dir)); + writeln!(&mut message, " {:?}", dir).unwrap(); } } if let Some(config_file) = config_file { - message.push_str(&format!("[CONFIG FILE]: {:?}\n", config_file)); + writeln!(&mut message, "[CONFIG FILE]: {:?}", config_file).unwrap(); match Config::new(&config_file) { Ok(_) => message.push_str("[CONFIG FILE]: Well defined.\n"), - Err(e) => message.push_str(&format!("[CONFIG ERROR]: {}\n", e)), + Err(e) => writeln!(&mut message, "[CONFIG ERROR]: {}", e).unwrap(), } } else { message.push_str("[CONFIG FILE]: Not Found\n"); - message.push_str(&format!( - " By default zellij looks for a file called [{}] in the configuration directory\n", + writeln!( + &mut message, + " By default zellij looks for a file called [{}] in the configuration directory", CONFIG_NAME - )); + ) + .unwrap(); } - message.push_str(&format!("[DATA DIR]: {:?}\n", data_dir)); + writeln!(&mut message, "[DATA DIR]: {:?}", data_dir).unwrap(); message.push_str(&format!("[PLUGIN DIR]: {:?}\n", plugin_dir)); if let Some(layout_dir) = layout_dir { - message.push_str(&format!("[LAYOUT DIR]: {:?}\n", layout_dir)); + writeln!(&mut message, "[LAYOUT DIR]: {:?}", layout_dir).unwrap(); } else { message.push_str("[LAYOUT DIR]: Not Found\n"); } - message.push_str(&format!("[SYSTEM DATA DIR]: {:?}\n", system_data_dir)); + writeln!(&mut message, "[SYSTEM DATA DIR]: {:?}", system_data_dir).unwrap(); - message.push_str(&format!("[ARROW SEPARATOR]: {}\n", ARROW_SEPARATOR)); + writeln!(&mut message, "[ARROW SEPARATOR]: {}", ARROW_SEPARATOR).unwrap(); message.push_str(" Is the [ARROW_SEPARATOR] displayed correctly?\n"); message.push_str(" If not you may want to either start zellij with a compatible mode: 'zellij options --simplified-ui true'\n"); let mut hyperlink_compat = String::new(); @@ -348,22 +352,24 @@ impl Setup { hyperlink_compat.push_str(hyperlink_mid); hyperlink_compat.push_str("https://zellij.dev/documentation/compatibility.html#the-status-bar-fonts-dont-render-correctly"); hyperlink_compat.push_str(hyperlink_end); - message.push_str(&format!( + write!( + &mut message, " Or check the font that is in use:\n {}\n", hyperlink_compat - )); + ) + .unwrap(); message.push_str("[MOUSE INTERACTION]: \n"); message.push_str(" Can be temporarily disabled through pressing the [SHIFT] key.\n"); message.push_str(" If that doesn't fix any issues consider to disable the mouse handling of zellij: 'zellij options --disable-mouse-mode'\n"); - message.push_str(&format!("[FEATURES]: {:?}\n", FEATURES)); + writeln!(&mut message, "[FEATURES]: {:?}", FEATURES).unwrap(); let mut hyperlink = String::new(); hyperlink.push_str(hyperlink_start); hyperlink.push_str("https://www.zellij.dev/documentation/"); hyperlink.push_str(hyperlink_mid); hyperlink.push_str("zellij.dev/documentation"); hyperlink.push_str(hyperlink_end); - message.push_str(&format!("[DOCUMENTATION]: {}\n", hyperlink)); + writeln!(&mut message, "[DOCUMENTATION]: {}", hyperlink).unwrap(); //printf '\e]8;;http://example.com\e\\This is a link\e]8;;\e\\\n' std::io::stdout().write_all(message.as_bytes())?; @@ -371,14 +377,10 @@ impl Setup { Ok(()) } fn generate_completion(shell: String) { - let shell = match shell.as_ref() { - "bash" => structopt::clap::Shell::Bash, - "fish" => structopt::clap::Shell::Fish, - "zsh" => structopt::clap::Shell::Zsh, - "powerShell" => structopt::clap::Shell::PowerShell, - "elvish" => structopt::clap::Shell::Elvish, - other => { - eprintln!("Unsupported shell: {}", other); + let shell = match shell.parse() { + Ok(shell) => shell, + _ => { + eprintln!("Unsupported shell: {}", shell); std::process::exit(1); } }; @@ -400,8 +402,8 @@ mod setup_test { config: &str, layout: &str, ) -> Result<(Config, LayoutFromYamlIntermediate), ConfigError> { - let config = Config::from_yaml(&config)?; - let layout = LayoutFromYamlIntermediate::from_yaml(&layout)?; + let config = Config::from_yaml(config)?; + let layout = LayoutFromYamlIntermediate::from_yaml(layout)?; Ok((config, layout)) } diff --git a/zellij-utils/src/shared.rs b/zellij-utils/src/shared.rs index 008f1ff6b..0211fb2cd 100644 --- a/zellij-utils/src/shared.rs +++ b/zellij-utils/src/shared.rs @@ -19,7 +19,7 @@ pub fn set_permissions(path: &Path) -> io::Result<()> { } pub fn ansi_len(s: &str) -> usize { - from_utf8(&strip(s.as_bytes()).unwrap()).unwrap().width() + from_utf8(&strip(s).unwrap()).unwrap().width() } pub fn adjust_to_size(s: &str, rows: usize, columns: usize) -> String { |