Skip to content

Commit 27e8c8f

Browse files
pbzweihandermatchai
authored andcommitted
refactor: Rewrite time module to use module config (starship#483)
- Config option renamed: `12hr` → `use_12hr`
1 parent bc9e44f commit 27e8c8f

File tree

9 files changed

+60
-33
lines changed

9 files changed

+60
-33
lines changed

docs/config/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -874,13 +874,13 @@ To enable it, set `disabled` to `false` in your configuration file.
874874

875875
| Variable | Default | Description |
876876
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
877-
| `12hr` | `false` | Enables 12 hour formatting |
877+
| `use_12hr` | `false` | Enables 12 hour formatting |
878878
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
879879
| `style` | `bold yellow` | The style for the module time |
880880
| `disabled` | `true` | Disables the `time` module. |
881881

882-
If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`.
883-
Manually setting `format` will override the `12hr` setting.
882+
If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`.
883+
Manually setting `format` will override the `use_12hr` setting.
884884

885885
### Example
886886

docs/de/config/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
812812

813813
| Variable | Default | Description |
814814
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
815-
| `12hr` | `false` | Enables 12 hour formatting |
815+
| `use_12hr` | `false` | Enables 12 hour formatting |
816816
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
817817
| `style` | `bold yellow` | The style for the module time |
818818
| `disabled` | `true` | Disables the `time` module. |
819819

820-
If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
820+
If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
821821

822822
### Example
823823

docs/fr/config/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
812812

813813
| Variable | Default | Description |
814814
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
815-
| `12hr` | `false` | Enables 12 hour formatting |
815+
| `use_12hr` | `false` | Enables 12 hour formatting |
816816
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
817817
| `style` | `bold yellow` | The style for the module time |
818818
| `disabled` | `true` | Disables the `time` module. |
819819

820-
If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
820+
If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
821821

822822
### Example
823823

docs/ja/config/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -813,12 +813,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
813813

814814
| Variable | Default | Description |
815815
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
816-
| `12hr` | `false` | Enables 12 hour formatting |
816+
| `use_12hr` | `false` | Enables 12 hour formatting |
817817
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
818818
| `style` | `bold yellow` | The style for the module time |
819819
| `disabled` | `true` | Disables the `time` module. |
820820

821-
If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
821+
If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
822822

823823
### Example
824824

docs/ru/config/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
812812

813813
| Variable | Default | Description |
814814
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
815-
| `12hr` | `false` | Enables 12 hour formatting |
815+
| `use_12hr` | `false` | Enables 12 hour formatting |
816816
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
817817
| `style` | `bold yellow` | The style for the module time |
818818
| `disabled` | `true` | Disables the `time` module. |
819819

820-
If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
820+
If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
821821

822822
### Example
823823

docs/zh/config/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
812812

813813
| Variable | Default | Description |
814814
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
815-
| `12hr` | `false` | Enables 12 hour formatting |
815+
| `use_12hr` | `false` | Enables 12 hour formatting |
816816
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
817817
| `style` | `bold yellow` | The style for the module time |
818818
| `disabled` | `true` | Disables the `time` module. |
819819

820-
If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
820+
If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
821821

822822
### Example
823823

src/configs/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod character;
44
pub mod dotnet;
55
pub mod kubernetes;
66
pub mod rust;
7+
pub mod time;
78

89
use crate::config::{ModuleConfig, RootModuleConfig};
910

src/configs/time.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::config::{ModuleConfig, RootModuleConfig};
2+
3+
use ansi_term::{Color, Style};
4+
use starship_module_config_derive::ModuleConfig;
5+
6+
#[derive(Clone, ModuleConfig)]
7+
pub struct TimeConfig<'a> {
8+
pub use_12hr: bool,
9+
pub format: Option<&'a str>,
10+
pub style: Style,
11+
pub disabled: bool,
12+
}
13+
14+
impl<'a> RootModuleConfig<'a> for TimeConfig<'a> {
15+
fn new() -> Self {
16+
TimeConfig {
17+
use_12hr: false,
18+
format: None,
19+
style: Color::Yellow.bold(),
20+
disabled: true,
21+
}
22+
}
23+
}

src/modules/time.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1-
use ansi_term::Color;
21
use chrono::{DateTime, Local};
32

43
use super::{Context, Module};
54

5+
use crate::config::{RootModuleConfig, SegmentConfig};
6+
use crate::configs::time::TimeConfig;
7+
68
/// Outputs the current time
79
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
8-
let mut module = context.new_module("time");
10+
const TIME_PREFIX: &str = "at ";
911

10-
if module.config_value_bool("disabled").unwrap_or(true) {
12+
let mut module = context.new_module("time");
13+
let config: TimeConfig = TimeConfig::try_load(module.config);
14+
if config.disabled {
1115
return None;
12-
}
13-
14-
let module_style = module
15-
.config_value_style("style")
16-
.unwrap_or_else(|| Color::Yellow.bold());
17-
module.set_style(module_style);
16+
};
1817

19-
// Load module settings
20-
let is_12hr = module.config_value_bool("12hr").unwrap_or(false);
21-
22-
let default_format = if is_12hr { "%r" } else { "%T" };
23-
let time_format = module
24-
.config_value_str("format")
25-
.unwrap_or(default_format)
26-
.to_owned();
18+
let default_format = if config.use_12hr { "%r" } else { "%T" };
19+
let time_format = config.format.unwrap_or(default_format);
2720

2821
log::trace!(
2922
"Timer module is enabled with format string: {}",
3023
time_format
3124
);
3225

3326
let local: DateTime<Local> = Local::now();
34-
let formatted_time_string = format_time(&time_format, local);
35-
module.new_segment("time", &formatted_time_string);
36-
module.get_prefix().set_value("at ");
27+
let formatted_time_string = format_time(time_format, local);
28+
29+
module.set_style(config.style);
30+
31+
module.get_prefix().set_value(TIME_PREFIX);
32+
33+
module.create_segment(
34+
"time",
35+
&SegmentConfig {
36+
value: &formatted_time_string,
37+
style: None,
38+
},
39+
);
3740

3841
Some(module)
3942
}

0 commit comments

Comments
 (0)