From 06ba22eb5c12c8cc0892261899ed140bbb51c99a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikodem=20Rabuli=C5=84ski?= Date: Sun, 6 Oct 2019 03:46:14 +0200 Subject: [PATCH] refactor: Implement Default for SegmentConfig (#495) Implements the Default trait for SegmentConfig to clean up construction of empty segments. Also adds a segment::new() function to ease construction of simple segments. --- src/config.rs | 10 ++++++++++ src/configs/aws.rs | 10 ++-------- src/configs/battery.rs | 20 ++++---------------- src/configs/character.rs | 15 +++------------ src/configs/dotnet.rs | 10 ++-------- src/configs/kubernetes.rs | 15 +++------------ src/configs/rust.rs | 10 ++-------- 7 files changed, 26 insertions(+), 64 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6f5025179..dafbe1beb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -219,6 +219,10 @@ impl<'a> ModuleConfig<'a> for SegmentConfig<'a> { } impl<'a> SegmentConfig<'a> { + pub fn new(value: &'static str) -> Self { + Self { value, style: None } + } + /// Mutably set value pub fn set_value(&mut self, value: &'a str) { self.value = value; @@ -246,6 +250,12 @@ impl<'a> SegmentConfig<'a> { } } +impl Default for SegmentConfig<'static> { + fn default() -> Self { + Self::new("") + } +} + /** Parse a style string which represents an ansi style. Valid tokens in the style string include the following: - 'fg:' (specifies that the color read should be a foreground color) diff --git a/src/configs/aws.rs b/src/configs/aws.rs index 96ed3cb5b..f362d0878 100644 --- a/src/configs/aws.rs +++ b/src/configs/aws.rs @@ -14,14 +14,8 @@ pub struct AwsConfig<'a> { impl<'a> RootModuleConfig<'a> for AwsConfig<'a> { fn new() -> Self { AwsConfig { - symbol: SegmentConfig { - value: "☁️ ", - style: None, - }, - profile: SegmentConfig { - value: "", - style: None, - }, + symbol: SegmentConfig::new("☁️ "), + profile: SegmentConfig::default(), style: Color::Yellow.bold(), disabled: false, } diff --git a/src/configs/battery.rs b/src/configs/battery.rs index e60b95e2f..d86227d79 100644 --- a/src/configs/battery.rs +++ b/src/configs/battery.rs @@ -18,18 +18,9 @@ pub struct BatteryConfig<'a> { impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> { fn new() -> Self { BatteryConfig { - full_symbol: SegmentConfig { - value: "•", - style: None, - }, - charging_symbol: SegmentConfig { - value: "↑", - style: None, - }, - discharging_symbol: SegmentConfig { - value: "↓", - style: None, - }, + full_symbol: SegmentConfig::new("•"), + charging_symbol: SegmentConfig::new("↑"), + discharging_symbol: SegmentConfig::new("↓"), unknown_symbol: None, empty_symbol: None, display: vec![BatteryDisplayConfig { @@ -37,10 +28,7 @@ impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> { style: Color::Red.bold(), }], disabled: false, - percentage: SegmentConfig { - value: "", - style: None, - }, + percentage: SegmentConfig::default(), } } } diff --git a/src/configs/character.rs b/src/configs/character.rs index 77a509cff..92f977ebb 100644 --- a/src/configs/character.rs +++ b/src/configs/character.rs @@ -17,18 +17,9 @@ pub struct CharacterConfig<'a> { impl<'a> RootModuleConfig<'a> for CharacterConfig<'a> { fn new() -> Self { CharacterConfig { - symbol: SegmentConfig { - value: "❯", - style: None, - }, - error_symbol: SegmentConfig { - value: "✖", - style: None, - }, - vicmd_symbol: SegmentConfig { - value: "❮", - style: None, - }, + symbol: SegmentConfig::new("❯"), + error_symbol: SegmentConfig::new("✖"), + vicmd_symbol: SegmentConfig::new("❮"), use_symbol_for_status: false, style_success: Color::Green.bold(), style_failure: Color::Red.bold(), diff --git a/src/configs/dotnet.rs b/src/configs/dotnet.rs index 084798c6d..f6bb59e40 100644 --- a/src/configs/dotnet.rs +++ b/src/configs/dotnet.rs @@ -15,14 +15,8 @@ pub struct DotnetConfig<'a> { impl<'a> RootModuleConfig<'a> for DotnetConfig<'a> { fn new() -> Self { DotnetConfig { - symbol: SegmentConfig { - value: "•NET ", - style: None, - }, - version: SegmentConfig { - value: "", - style: None, - }, + symbol: SegmentConfig::new("•NET "), + version: SegmentConfig::default(), style: Color::Blue.bold(), heuristic: true, disabled: false, diff --git a/src/configs/kubernetes.rs b/src/configs/kubernetes.rs index 733919cd1..2d8e681be 100644 --- a/src/configs/kubernetes.rs +++ b/src/configs/kubernetes.rs @@ -15,18 +15,9 @@ pub struct KubernetesConfig<'a> { impl<'a> RootModuleConfig<'a> for KubernetesConfig<'a> { fn new() -> Self { KubernetesConfig { - symbol: SegmentConfig { - value: "☸ ", - style: None, - }, - context: SegmentConfig { - value: "", - style: None, - }, - namespace: SegmentConfig { - value: "", - style: None, - }, + symbol: SegmentConfig::new("☸ "), + context: SegmentConfig::default(), + namespace: SegmentConfig::default(), style: Color::Cyan.bold(), disabled: true, } diff --git a/src/configs/rust.rs b/src/configs/rust.rs index 2bd257588..911524ab5 100644 --- a/src/configs/rust.rs +++ b/src/configs/rust.rs @@ -14,14 +14,8 @@ pub struct RustConfig<'a> { impl<'a> RootModuleConfig<'a> for RustConfig<'a> { fn new() -> Self { RustConfig { - symbol: SegmentConfig { - value: "🦀 ", - style: None, - }, - version: SegmentConfig { - value: "", - style: None, - }, + symbol: SegmentConfig::new("🦀 "), + version: SegmentConfig::default(), style: Color::Red.bold(), disabled: false, }