diff options
author | Zhenhui Xie <xiezh0831@yahoo.co.jp> | 2019-10-19 09:51:38 +0800 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-10-19 10:51:38 +0900 |
commit | aadd8ecf2c0f3bdbb9684f900fee66b13f47c061 (patch) | |
tree | d2ff151e7bdc0de6c9b89e4723a3df23025aa957 /src | |
parent | 09353fff005b2b983fe9917df662483b9669dd64 (diff) |
refactor: Refactor modules to use module config (#514)
Diffstat (limited to 'src')
-rw-r--r-- | src/configs/mod.rs | 5 | ||||
-rw-r--r-- | src/configs/nodejs.rs | 21 | ||||
-rw-r--r-- | src/configs/package.rs | 21 | ||||
-rw-r--r-- | src/configs/python.rs | 27 | ||||
-rw-r--r-- | src/configs/ruby.rs | 21 | ||||
-rw-r--r-- | src/configs/username.rs | 23 | ||||
-rw-r--r-- | src/modules/mod.rs | 2 | ||||
-rw-r--r-- | src/modules/nodejs.rs | 18 | ||||
-rw-r--r-- | src/modules/package.rs | 17 | ||||
-rw-r--r-- | src/modules/python.rs | 53 | ||||
-rw-r--r-- | src/modules/ruby.rs | 28 | ||||
-rw-r--r-- | src/modules/username.rs | 34 |
12 files changed, 179 insertions, 91 deletions
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<Module<'a>> { 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<Module<'a>> { 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<Module<'a>> { } let mut module = context.new_module("python"); - let pyenv_version_name = module - .config_value_bool("pyenv_version_name") - .unwrap_or(false); - - 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); - - select_python_version(pyenv_version_name) - .map(|python_version| python_module(module, pyenv_version_name, python_version)) -} + let config: PythonConfig = PythonConfig::try_load(module.config); -fn python_module(mut module: Module, pyenv_version_name: bool, python_version: String) -> Module { - const PYENV_PREFIX: &str = "pyenv "; + module.set_style(config.style); + module.create_segment("symbol", &config.symbol); - 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<String> { - if pyenv_version_name { - get_pyenv_version() - } else { - get_python_version() - } + Some(module) } fn get_pyenv_version() -> Option<String> { 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<Module<'a>> { 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<String> { 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<Module<'a>> { 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<u32> { @@ -40,14 +45,3 @@ fn get_uid() -> Option<u32> { Err(_) => None, } } - -fn get_mod_style(user_uid: Option<u32>, 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()), - } -} |