From 57b38f17bbc9540f57fe8fa7fa07b90848a284ad Mon Sep 17 00:00:00 2001 From: Zhenhui Xie Date: Thu, 10 Oct 2019 16:21:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20Rewrite=20hostname,=20jobs=20and=20?= =?UTF-8?q?line=5Fbreak=20module=20to=20use=20mo=E2=80=A6=20(#462)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/config/README.md | 2 +- src/config.rs | 2 +- src/configs/hostname.rs | 25 +++++++++++++++++++++++++ src/configs/jobs.rs | 23 +++++++++++++++++++++++ src/configs/mod.rs | 2 ++ src/modules/hostname.rs | 20 ++++++++++---------- src/modules/jobs.rs | 20 ++++++++------------ src/modules/line_break.rs | 3 ++- 8 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 src/configs/hostname.rs create mode 100644 src/configs/jobs.rs diff --git a/docs/config/README.md b/docs/config/README.md index 9243ca01..504c87b1 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -573,7 +573,7 @@ more than the `threshold` config value, if it exists. | Variable | Default | Description | | ----------- | ------------- | ----------------------------------------------------- | -| `symbol` | `"✦ "` | The symbol used before displaying the number of jobs. | +| `symbol` | `"✦"` | The symbol used before displaying the number of jobs. | | `threshold` | `1` | Show number of jobs if exceeded. | | `style` | `"bold blue"` | The style for the module. | | `disabled` | `false` | Disables the `jobs` module. | diff --git a/src/config.rs b/src/config.rs index dafbe1be..785cd26b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -219,7 +219,7 @@ impl<'a> ModuleConfig<'a> for SegmentConfig<'a> { } impl<'a> SegmentConfig<'a> { - pub fn new(value: &'static str) -> Self { + pub fn new(value: &'a str) -> Self { Self { value, style: None } } diff --git a/src/configs/hostname.rs b/src/configs/hostname.rs new file mode 100644 index 00000000..fc2d0c07 --- /dev/null +++ b/src/configs/hostname.rs @@ -0,0 +1,25 @@ +use crate::config::{ModuleConfig, RootModuleConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct HostnameConfig<'a> { + pub ssh_only: bool, + pub prefix: &'a str, + pub suffix: &'a str, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for HostnameConfig<'a> { + fn new() -> Self { + HostnameConfig { + ssh_only: true, + prefix: "", + suffix: "", + style: Color::Green.bold().dimmed(), + disabled: false, + } + } +} diff --git a/src/configs/jobs.rs b/src/configs/jobs.rs new file mode 100644 index 00000000..a2f255ea --- /dev/null +++ b/src/configs/jobs.rs @@ -0,0 +1,23 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct JobsConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub threshold: i64, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for JobsConfig<'a> { + fn new() -> Self { + JobsConfig { + symbol: SegmentConfig::new("✦"), + threshold: 1, + style: Color::Blue.bold(), + disabled: false, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index a8e8302d..c2f0ab08 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -3,6 +3,8 @@ pub mod battery; pub mod character; pub mod conda; pub mod dotnet; +pub mod hostname; +pub mod jobs; pub mod kubernetes; pub mod rust; pub mod time; diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs index d71f733f..acb39379 100644 --- a/src/modules/hostname.rs +++ b/src/modules/hostname.rs @@ -1,9 +1,11 @@ -use ansi_term::Color; use std::env; use super::{Context, Module}; use std::ffi::OsString; +use crate::config::RootModuleConfig; +use crate::configs::hostname::HostnameConfig; + /// Creates a module with the system hostname /// /// Will display the hostname if all of the following criteria are met: @@ -11,12 +13,10 @@ use std::ffi::OsString; /// - hostname.ssh_only is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`) pub fn module<'a>(context: &'a Context) -> Option> { let mut module = context.new_module("hostname"); - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Green.bold().dimmed()); + let config: HostnameConfig = HostnameConfig::try_load(module.config); let ssh_connection = env::var("SSH_CONNECTION").ok(); - if module.config_value_bool("ssh_only").unwrap_or(true) && ssh_connection.is_none() { + if config.ssh_only && ssh_connection.is_none() { return None; } @@ -30,11 +30,11 @@ pub fn module<'a>(context: &'a Context) -> Option> { } }; - let prefix = module.config_value_str("prefix").unwrap_or("").to_owned(); - let suffix = module.config_value_str("suffix").unwrap_or("").to_owned(); - - module.set_style(module_style); - module.new_segment("hostname", &format!("{}{}{}", prefix, host, suffix)); + module.set_style(config.style); + module.new_segment( + "hostname", + &format!("{}{}{}", config.prefix, host, config.suffix), + ); module.get_prefix().set_value("on "); Some(module) diff --git a/src/modules/jobs.rs b/src/modules/jobs.rs index 8a667049..e0f2308d 100644 --- a/src/modules/jobs.rs +++ b/src/modules/jobs.rs @@ -1,18 +1,14 @@ -use ansi_term::Color; - use super::{Context, Module}; +use crate::config::{RootModuleConfig, SegmentConfig}; +use crate::configs::jobs::JobsConfig; + /// Creates a segment to show if there are any active jobs running pub fn module<'a>(context: &'a Context) -> Option> { let mut module = context.new_module("jobs"); + let config: JobsConfig = JobsConfig::try_load(module.config); - let threshold = module.config_value_i64("threshold").unwrap_or(1); - - const JOB_CHAR: &str = "✦"; - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Blue.bold()); - module.set_style(module_style); + module.set_style(config.style); let arguments = &context.arguments; let num_of_jobs = arguments @@ -24,9 +20,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { if num_of_jobs == 0 { return None; } - module.new_segment("symbol", JOB_CHAR); - if num_of_jobs > threshold { - module.new_segment("number", &num_of_jobs.to_string()); + module.create_segment("symbol", &config.symbol); + if num_of_jobs > config.threshold { + module.create_segment("number", &SegmentConfig::new(&num_of_jobs.to_string())); } module.get_prefix().set_value(""); diff --git a/src/modules/line_break.rs b/src/modules/line_break.rs index 259e6087..3b9c0628 100644 --- a/src/modules/line_break.rs +++ b/src/modules/line_break.rs @@ -1,4 +1,5 @@ use super::{Context, Module}; +use crate::config::SegmentConfig; /// Creates a module for the line break pub fn module<'a>(context: &'a Context) -> Option> { @@ -9,7 +10,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { module.get_prefix().set_value(""); module.get_suffix().set_value(""); - module.new_segment("character", LINE_ENDING); + module.create_segment("character", &SegmentConfig::new(LINE_ENDING)); Some(module) }