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> { impl<'a> SegmentConfig<'a> {
pub fn new(value: &'static str) -> Self {
Self { value, style: None }
}
/// Mutably set value /// Mutably set value
pub fn set_value(&mut self, value: &'a str) { pub fn set_value(&mut self, value: &'a str) {
self.value = value; 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 /** Parse a style string which represents an ansi style. Valid tokens in the style
string include the following: string include the following:
- 'fg:<color>' (specifies that the color read should be a foreground color) - '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> { impl<'a> RootModuleConfig<'a> for AwsConfig<'a> {
fn new() -> Self { fn new() -> Self {
AwsConfig { AwsConfig {
symbol: SegmentConfig { symbol: SegmentConfig::new("☁️ "),
value: "☁️ ", profile: SegmentConfig::default(),
style: None,
},
profile: SegmentConfig {
value: "",
style: None,
},
style: Color::Yellow.bold(), style: Color::Yellow.bold(),
disabled: false, disabled: false,
} }

View File

@ -18,18 +18,9 @@ pub struct BatteryConfig<'a> {
impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> { impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> {
fn new() -> Self { fn new() -> Self {
BatteryConfig { BatteryConfig {
full_symbol: SegmentConfig { full_symbol: SegmentConfig::new(""),
value: "", charging_symbol: SegmentConfig::new(""),
style: None, discharging_symbol: SegmentConfig::new(""),
},
charging_symbol: SegmentConfig {
value: "",
style: None,
},
discharging_symbol: SegmentConfig {
value: "",
style: None,
},
unknown_symbol: None, unknown_symbol: None,
empty_symbol: None, empty_symbol: None,
display: vec![BatteryDisplayConfig { display: vec![BatteryDisplayConfig {
@ -37,10 +28,7 @@ impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> {
style: Color::Red.bold(), style: Color::Red.bold(),
}], }],
disabled: false, disabled: false,
percentage: SegmentConfig { percentage: SegmentConfig::default(),
value: "",
style: None,
},
} }
} }
} }

View File

@ -17,18 +17,9 @@ pub struct CharacterConfig<'a> {
impl<'a> RootModuleConfig<'a> for CharacterConfig<'a> { impl<'a> RootModuleConfig<'a> for CharacterConfig<'a> {
fn new() -> Self { fn new() -> Self {
CharacterConfig { CharacterConfig {
symbol: SegmentConfig { symbol: SegmentConfig::new(""),
value: "", error_symbol: SegmentConfig::new(""),
style: None, vicmd_symbol: SegmentConfig::new(""),
},
error_symbol: SegmentConfig {
value: "",
style: None,
},
vicmd_symbol: SegmentConfig {
value: "",
style: None,
},
use_symbol_for_status: false, use_symbol_for_status: false,
style_success: Color::Green.bold(), style_success: Color::Green.bold(),
style_failure: Color::Red.bold(), style_failure: Color::Red.bold(),

View File

@ -15,14 +15,8 @@ pub struct DotnetConfig<'a> {
impl<'a> RootModuleConfig<'a> for DotnetConfig<'a> { impl<'a> RootModuleConfig<'a> for DotnetConfig<'a> {
fn new() -> Self { fn new() -> Self {
DotnetConfig { DotnetConfig {
symbol: SegmentConfig { symbol: SegmentConfig::new("•NET "),
value: "•NET ", version: SegmentConfig::default(),
style: None,
},
version: SegmentConfig {
value: "",
style: None,
},
style: Color::Blue.bold(), style: Color::Blue.bold(),
heuristic: true, heuristic: true,
disabled: false, disabled: false,

View File

@ -15,18 +15,9 @@ pub struct KubernetesConfig<'a> {
impl<'a> RootModuleConfig<'a> for KubernetesConfig<'a> { impl<'a> RootModuleConfig<'a> for KubernetesConfig<'a> {
fn new() -> Self { fn new() -> Self {
KubernetesConfig { KubernetesConfig {
symbol: SegmentConfig { symbol: SegmentConfig::new(""),
value: "", context: SegmentConfig::default(),
style: None, namespace: SegmentConfig::default(),
},
context: SegmentConfig {
value: "",
style: None,
},
namespace: SegmentConfig {
value: "",
style: None,
},
style: Color::Cyan.bold(), style: Color::Cyan.bold(),
disabled: true, disabled: true,
} }

View File

@ -14,14 +14,8 @@ pub struct RustConfig<'a> {
impl<'a> RootModuleConfig<'a> for RustConfig<'a> { impl<'a> RootModuleConfig<'a> for RustConfig<'a> {
fn new() -> Self { fn new() -> Self {
RustConfig { RustConfig {
symbol: SegmentConfig { symbol: SegmentConfig::new("🦀 "),
value: "🦀 ", version: SegmentConfig::default(),
style: None,
},
version: SegmentConfig {
value: "",
style: None,
},
style: Color::Red.bold(), style: Color::Red.bold(),
disabled: false, disabled: false,
} }