diff --git a/src/configs/java.rs b/src/configs/java.rs new file mode 100644 index 00000000..088aefba --- /dev/null +++ b/src/configs/java.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 JavaConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for JavaConfig<'a> { + fn new() -> Self { + JavaConfig { + symbol: SegmentConfig::new("☕ "), + style: Color::Red.dimmed(), + disabled: false, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 62bae584..4c6bd934 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -10,9 +10,11 @@ pub mod git_branch; pub mod git_status; pub mod go; pub mod hostname; +pub mod java; pub mod jobs; pub mod kubernetes; pub mod memory_usage; +pub mod nix_shell; pub mod nodejs; pub mod package; pub mod python; diff --git a/src/configs/nix_shell.rs b/src/configs/nix_shell.rs new file mode 100644 index 00000000..d5316b6a --- /dev/null +++ b/src/configs/nix_shell.rs @@ -0,0 +1,25 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct NixShellConfig<'a> { + pub use_name: bool, + pub impure_msg: SegmentConfig<'a>, + pub pure_msg: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for NixShellConfig<'a> { + fn new() -> Self { + NixShellConfig { + use_name: false, + impure_msg: SegmentConfig::new("impure"), + pure_msg: SegmentConfig::new("pure"), + style: Color::Red.bold(), + disabled: false, + } + } +} diff --git a/src/modules/java.rs b/src/modules/java.rs index 80c25747..2f73a3c2 100644 --- a/src/modules/java.rs +++ b/src/modules/java.rs @@ -1,9 +1,8 @@ +use crate::configs::java::JavaConfig; use std::process::Command; use std::process::Output; -use ansi_term::Color; - -use super::{Context, Module}; +use super::{Context, Module, RootModuleConfig, SegmentConfig}; use crate::modules::utils::java_version_parser; @@ -25,17 +24,13 @@ pub fn module<'a>(context: &'a Context) -> Option> { match get_java_version() { Some(java_version) => { - const JAVA_CHAR: &str = "☕ "; - let mut module = context.new_module("java"); - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Red.dimmed()); - module.set_style(module_style); + let config: JavaConfig = JavaConfig::try_load(module.config); + module.set_style(config.style); let formatted_version = format_java_version(java_version)?; - module.new_segment("symbol", JAVA_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/nix_shell.rs b/src/modules/nix_shell.rs index 36228993..33fb5bb8 100644 --- a/src/modules/nix_shell.rs +++ b/src/modules/nix_shell.rs @@ -1,7 +1,8 @@ -use ansi_term::Color; use std::env; -use super::{Context, Module}; +use super::{Context, Module, RootModuleConfig, SegmentConfig}; + +use crate::configs::nix_shell::NixShellConfig; // IN_NIX_SHELL should be "pure" or "impure" but lorri uses "1" for "impure" // https://github.com/target/lorri/issues/140 @@ -23,34 +24,31 @@ use super::{Context, Module}; /// - impure // use_name == false in an impure nix-shell pub fn module<'a>(context: &'a Context) -> Option> { let mut module = context.new_module("nix_shell"); + let config: NixShellConfig = NixShellConfig::try_load(module.config); - env::var("IN_NIX_SHELL") - .ok() - .and_then(|shell_type| { - if shell_type == "1" || shell_type == "impure" { - Some(module.config_value_str("impure_msg").unwrap_or("impure")) - } else if shell_type == "pure" { - Some(module.config_value_str("pure_msg").unwrap_or("pure")) - } else { - None - } - }) - .map(|shell_type| { - if module.config_value_bool("use_name").unwrap_or(false) { - match env::var("name").ok() { - Some(name) => format!("{} ({})", name, shell_type), - None => shell_type.to_string(), - } - } else { - shell_type.to_string() - } - }) - .map(|segment| { - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Red.bold()); - module.set_style(module_style); - module.new_segment("nix_shell", &segment); - module - }) + module.set_style(config.style); + + let shell_type = env::var("IN_NIX_SHELL").ok()?; + let shell_type_segment: SegmentConfig = match shell_type.as_ref() { + "1" | "impure" => config.impure_msg, + "pure" => config.pure_msg, + _ => { + return None; + } + }; + + if config.use_name { + if let Ok(name) = env::var("name") { + module.create_segment( + "nix_shell", + &shell_type_segment.with_value(&format!("{} ({})", name, shell_type_segment.value)), + ); + } else { + module.create_segment("nix_shell", &shell_type_segment); + } + } else { + module.create_segment("nix_shell", &shell_type_segment); + } + + Some(module) }