From aadd8ecf2c0f3bdbb9684f900fee66b13f47c061 Mon Sep 17 00:00:00 2001 From: Zhenhui Xie Date: Sat, 19 Oct 2019 09:51:38 +0800 Subject: [PATCH] refactor: Refactor modules to use module config (#514) --- src/configs/mod.rs | 5 ++++ src/configs/nodejs.rs | 21 ++++++++++++++++ src/configs/package.rs | 21 ++++++++++++++++ src/configs/python.rs | 27 +++++++++++++++++++++ src/configs/ruby.rs | 21 ++++++++++++++++ src/configs/username.rs | 23 ++++++++++++++++++ src/modules/mod.rs | 2 +- src/modules/nodejs.rs | 18 +++++++------- src/modules/package.rs | 17 +++++++------ src/modules/python.rs | 53 +++++++++++++++-------------------------- src/modules/ruby.rs | 28 +++++++++------------- src/modules/username.rs | 34 +++++++++++--------------- 12 files changed, 179 insertions(+), 91 deletions(-) create mode 100644 src/configs/nodejs.rs create mode 100644 src/configs/package.rs create mode 100644 src/configs/python.rs create mode 100644 src/configs/ruby.rs create mode 100644 src/configs/username.rs diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 4f1a90b51..95743df1c 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -11,8 +11,13 @@ pub mod go; pub mod hostname; pub mod jobs; pub mod kubernetes; +pub mod nodejs; +pub mod package; +pub mod python; +pub mod ruby; pub mod rust; pub mod time; +pub mod username; use crate::config::{ModuleConfig, RootModuleConfig}; diff --git a/src/configs/nodejs.rs b/src/configs/nodejs.rs new file mode 100644 index 000000000..5faee3fe0 --- /dev/null +++ b/src/configs/nodejs.rs @@ -0,0 +1,21 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct NodejsConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> { + fn new() -> Self { + NodejsConfig { + symbol: SegmentConfig::new("⬢ "), + style: Color::Green.bold(), + disabled: false, + } + } +} diff --git a/src/configs/package.rs b/src/configs/package.rs new file mode 100644 index 000000000..92417a294 --- /dev/null +++ b/src/configs/package.rs @@ -0,0 +1,21 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct PackageConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for PackageConfig<'a> { + fn new() -> Self { + PackageConfig { + symbol: SegmentConfig::new("📦 "), + style: Color::Red.bold(), + disabled: false, + } + } +} diff --git a/src/configs/python.rs b/src/configs/python.rs new file mode 100644 index 000000000..736283f4f --- /dev/null +++ b/src/configs/python.rs @@ -0,0 +1,27 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct PythonConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub version: SegmentConfig<'a>, + pub pyenv_prefix: SegmentConfig<'a>, + pub pyenv_version_name: bool, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for PythonConfig<'a> { + fn new() -> Self { + PythonConfig { + symbol: SegmentConfig::new("🐍 "), + version: SegmentConfig::default(), + pyenv_prefix: SegmentConfig::new("pyenv "), + pyenv_version_name: false, + style: Color::Yellow.bold(), + disabled: false, + } + } +} diff --git a/src/configs/ruby.rs b/src/configs/ruby.rs new file mode 100644 index 000000000..82d1d03eb --- /dev/null +++ b/src/configs/ruby.rs @@ -0,0 +1,21 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct RubyConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for RubyConfig<'a> { + fn new() -> Self { + RubyConfig { + symbol: SegmentConfig::new("💎 "), + style: Color::Red.bold(), + disabled: false, + } + } +} diff --git a/src/configs/username.rs b/src/configs/username.rs new file mode 100644 index 000000000..8414fe68c --- /dev/null +++ b/src/configs/username.rs @@ -0,0 +1,23 @@ +use crate::config::{ModuleConfig, RootModuleConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct UsernameConfig { + pub style_root: Style, + pub style_user: Style, + pub show_always: bool, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for UsernameConfig { + fn new() -> Self { + UsernameConfig { + style_root: Color::Red.bold(), + style_user: Color::Yellow.bold(), + show_always: false, + disabled: false, + } + } +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 29b8c3b25..f32e6fcc0 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -28,7 +28,7 @@ mod username; #[cfg(feature = "battery")] mod battery; -use crate::config::RootModuleConfig; +use crate::config::{RootModuleConfig, SegmentConfig}; use crate::context::Context; use crate::module::Module; diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index b30f2f531..8f9bc1f43 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -1,7 +1,8 @@ -use ansi_term::Color; use std::process::Command; -use super::{Context, Module}; +use super::{Context, Module, RootModuleConfig, SegmentConfig}; + +use crate::configs::nodejs::NodejsConfig; /// Creates a module with the current Node.js version /// @@ -23,17 +24,14 @@ pub fn module<'a>(context: &'a Context) -> Option> { match get_node_version() { Some(node_version) => { - const NODE_CHAR: &str = "⬢ "; - let mut module = context.new_module("nodejs"); - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Green.bold()); - module.set_style(module_style); + let config: NodejsConfig = NodejsConfig::try_load(module.config); + + module.set_style(config.style); let formatted_version = node_version.trim(); - module.new_segment("symbol", NODE_CHAR); - module.new_segment("version", formatted_version); + module.create_segment("symbol", &config.symbol); + module.create_segment("version", &SegmentConfig::new(formatted_version)); Some(module) } diff --git a/src/modules/package.rs b/src/modules/package.rs index 0debe162d..2a2580f1e 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -1,27 +1,26 @@ use super::{Context, Module}; use crate::utils; -use ansi_term::Color; use serde_json as json; use toml; +use super::{RootModuleConfig, SegmentConfig}; +use crate::configs::package::PackageConfig; + /// Creates a module with the current package version /// /// Will display if a version is defined for your Node.js or Rust project (if one exists) pub fn module<'a>(context: &'a Context) -> Option> { match get_package_version() { Some(package_version) => { - const PACKAGE_CHAR: &str = "📦 "; - let mut module = context.new_module("package"); - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Red.bold()); - module.set_style(module_style); + let config: PackageConfig = PackageConfig::try_load(module.config); + + module.set_style(config.style); module.get_prefix().set_value("is "); - module.new_segment("symbol", PACKAGE_CHAR); - module.new_segment("version", &package_version); + module.create_segment("symbol", &config.symbol); + module.create_segment("version", &SegmentConfig::new(&package_version)); Some(module) } diff --git a/src/modules/python.rs b/src/modules/python.rs index 68d63673d..18d683859 100644 --- a/src/modules/python.rs +++ b/src/modules/python.rs @@ -2,9 +2,8 @@ use std::env; use std::path::Path; use std::process::Command; -use ansi_term::Color; - -use super::{Context, Module}; +use super::{Context, Module, RootModuleConfig, SegmentConfig}; +use crate::configs::python::PythonConfig; /// Creates a module with the current Python version /// @@ -33,43 +32,29 @@ pub fn module<'a>(context: &'a Context) -> Option> { } let mut module = context.new_module("python"); - let pyenv_version_name = module - .config_value_bool("pyenv_version_name") - .unwrap_or(false); + let config: PythonConfig = PythonConfig::try_load(module.config); - const PYTHON_CHAR: &str = "🐍 "; - let module_color = module - .config_value_style("style") - .unwrap_or_else(|| Color::Yellow.bold()); - module.set_style(module_color); - module.new_segment("symbol", PYTHON_CHAR); + module.set_style(config.style); + module.create_segment("symbol", &config.symbol); - select_python_version(pyenv_version_name) - .map(|python_version| python_module(module, pyenv_version_name, python_version)) -} - -fn python_module(mut module: Module, pyenv_version_name: bool, python_version: String) -> Module { - const PYENV_PREFIX: &str = "pyenv "; - - if pyenv_version_name { - module.new_segment("pyenv_prefix", PYENV_PREFIX); - module.new_segment("version", &python_version.trim()); + if config.pyenv_version_name { + let python_version = get_pyenv_version()?; + module.create_segment("pyenv_prefix", &config.pyenv_prefix); + module.create_segment("version", &SegmentConfig::new(&python_version.trim())); } else { + let python_version = get_python_version()?; let formatted_version = format_python_version(&python_version); - module.new_segment("version", &formatted_version); - get_python_virtual_env() - .map(|virtual_env| module.new_segment("virtualenv", &format!(" ({})", virtual_env))); + module.create_segment("version", &SegmentConfig::new(&formatted_version)); + + if let Some(virtual_env) = get_python_virtual_env() { + module.create_segment( + "virtualenv", + &SegmentConfig::new(&format!(" ({})", virtual_env)), + ); + }; }; - module -} - -fn select_python_version(pyenv_version_name: bool) -> Option { - if pyenv_version_name { - get_pyenv_version() - } else { - get_python_version() - } + Some(module) } fn get_pyenv_version() -> Option { diff --git a/src/modules/ruby.rs b/src/modules/ruby.rs index 0faa2ccf8..247b941eb 100644 --- a/src/modules/ruby.rs +++ b/src/modules/ruby.rs @@ -1,7 +1,8 @@ -use ansi_term::Color; use std::process::Command; -use super::{Context, Module}; +use super::{Context, Module, RootModuleConfig, SegmentConfig}; + +use crate::configs::ruby::RubyConfig; /// Creates a module with the current Ruby version /// @@ -19,24 +20,17 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - match get_ruby_version() { - Some(ruby_version) => { - const RUBY_CHAR: &str = "💎 "; + let ruby_version = get_ruby_version()?; + let formatted_version = format_ruby_version(&ruby_version)?; - let mut module = context.new_module("ruby"); - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Red.bold()); - module.set_style(module_style); + let mut module = context.new_module("ruby"); + let config: RubyConfig = RubyConfig::try_load(module.config); + module.set_style(config.style); - let formatted_version = format_ruby_version(&ruby_version)?; - module.new_segment("symbol", RUBY_CHAR); - module.new_segment("version", &formatted_version); + module.create_segment("symbol", &config.symbol); + module.create_segment("version", &SegmentConfig::new(&formatted_version)); - Some(module) - } - None => None, - } + Some(module) } fn get_ruby_version() -> Option { diff --git a/src/modules/username.rs b/src/modules/username.rs index 376da86c8..e89336bb6 100644 --- a/src/modules/username.rs +++ b/src/modules/username.rs @@ -1,8 +1,9 @@ -use ansi_term::{Color, Style}; use std::env; use std::process::Command; -use super::{Context, Module}; +use super::{Context, Module, RootModuleConfig, SegmentConfig}; + +use crate::configs::username::UsernameConfig; /// Creates a module with the current user's username /// @@ -19,17 +20,21 @@ pub fn module<'a>(context: &'a Context) -> Option> { let user_uid = get_uid(); let mut module = context.new_module("username"); - let show_always = module.config_value_bool("show_always").unwrap_or(false); + let config: UsernameConfig = UsernameConfig::try_load(module.config); + + if user != logname || ssh_connection.is_some() || user_uid == ROOT_UID || config.show_always { + let module_style = match user_uid { + Some(0) => config.style_root, + _ => config.style_user, + }; - if user != logname || ssh_connection.is_some() || user_uid == ROOT_UID || show_always { - let module_style = get_mod_style(user_uid, &module); module.set_style(module_style); - module.new_segment("username", &user?); + module.create_segment("username", &SegmentConfig::new(&user?)); - return Some(module); + Some(module) + } else { + None } - - None } fn get_uid() -> Option { @@ -40,14 +45,3 @@ fn get_uid() -> Option { Err(_) => None, } } - -fn get_mod_style(user_uid: Option, module: &Module) -> Style { - match user_uid { - Some(0) => module - .config_value_style("style_root") - .unwrap_or_else(|| Color::Red.bold()), - _ => module - .config_value_style("style_user") - .unwrap_or_else(|| Color::Yellow.bold()), - } -}