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.
This commit is contained in:
Nikodem Rabuliński 2019-10-06 03:46:14 +02:00 committed by Kevin Song
parent 044e10de1b
commit 06ba22eb5c
7 changed files with 26 additions and 64 deletions

View File

@ -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:<color>' (specifies that the color read should be a foreground color)

View File

@ -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,
}

View File

@ -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(),
}
}
}

View File

@ -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(),

View File

@ -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,

View File

@ -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,
}

View File

@ -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,
}