summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Lee <pbzweihander@gmail.com>2019-10-06 00:07:00 +0900
committerMatan Kushner <hello@matchai.me>2019-10-06 00:07:00 +0900
commit27e8c8f3ebae1fd5123df28aaee8f7d379d12026 (patch)
tree56e44d21fcccd4a0a17629feea48c9292c4c6c69
parentbc9e44f45ca9bbdf681fcfe6e23cd0542a54ec60 (diff)
refactor: Rewrite `time` module to use module config (#483)
- Config option renamed: `12hr` → `use_12hr`
-rw-r--r--docs/config/README.md6
-rw-r--r--docs/de/config/README.md4
-rw-r--r--docs/fr/config/README.md4
-rw-r--r--docs/ja/config/README.md4
-rw-r--r--docs/ru/config/README.md4
-rw-r--r--docs/zh/config/README.md4
-rw-r--r--src/configs/mod.rs1
-rw-r--r--src/configs/time.rs23
-rw-r--r--src/modules/time.rs43
9 files changed, 60 insertions, 33 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index 318d75e7f..b2870b023 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -874,13 +874,13 @@ To enable it, set `disabled` to `false` in your configuration file.
| Variable | Default | Description |
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
-| `12hr` | `false` | Enables 12 hour formatting |
+| `use_12hr` | `false` | Enables 12 hour formatting |
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
| `style` | `bold yellow` | The style for the module time |
| `disabled` | `true` | Disables the `time` module. |
-If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`.
-Manually setting `format` will override the `12hr` setting.
+If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`.
+Manually setting `format` will override the `use_12hr` setting.
### Example
diff --git a/docs/de/config/README.md b/docs/de/config/README.md
index 143e1779a..30492ebf6 100644
--- a/docs/de/config/README.md
+++ b/docs/de/config/README.md
@@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
| Variable | Default | Description |
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
-| `12hr` | `false` | Enables 12 hour formatting |
+| `use_12hr` | `false` | Enables 12 hour formatting |
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
| `style` | `bold yellow` | The style for the module time |
| `disabled` | `true` | Disables the `time` module. |
-If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
+If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
### Example
diff --git a/docs/fr/config/README.md b/docs/fr/config/README.md
index 143e1779a..30492ebf6 100644
--- a/docs/fr/config/README.md
+++ b/docs/fr/config/README.md
@@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
| Variable | Default | Description |
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
-| `12hr` | `false` | Enables 12 hour formatting |
+| `use_12hr` | `false` | Enables 12 hour formatting |
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
| `style` | `bold yellow` | The style for the module time |
| `disabled` | `true` | Disables the `time` module. |
-If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
+If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
### Example
diff --git a/docs/ja/config/README.md b/docs/ja/config/README.md
index 81cae8840..7a8ef533f 100644
--- a/docs/ja/config/README.md
+++ b/docs/ja/config/README.md
@@ -813,12 +813,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
| Variable | Default | Description |
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
-| `12hr` | `false` | Enables 12 hour formatting |
+| `use_12hr` | `false` | Enables 12 hour formatting |
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
| `style` | `bold yellow` | The style for the module time |
| `disabled` | `true` | Disables the `time` module. |
-If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
+If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
### Example
diff --git a/docs/ru/config/README.md b/docs/ru/config/README.md
index 143e1779a..30492ebf6 100644
--- a/docs/ru/config/README.md
+++ b/docs/ru/config/README.md
@@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
| Variable | Default | Description |
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
-| `12hr` | `false` | Enables 12 hour formatting |
+| `use_12hr` | `false` | Enables 12 hour formatting |
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
| `style` | `bold yellow` | The style for the module time |
| `disabled` | `true` | Disables the `time` module. |
-If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
+If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
### Example
diff --git a/docs/zh/config/README.md b/docs/zh/config/README.md
index 143e1779a..30492ebf6 100644
--- a/docs/zh/config/README.md
+++ b/docs/zh/config/README.md
@@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
| Variable | Default | Description |
| ---------- | ------------- | ------------------------------------------------------------------------------------------------------------------- |
-| `12hr` | `false` | Enables 12 hour formatting |
+| `use_12hr` | `false` | Enables 12 hour formatting |
| `format` | see below | The [chrono format string](https://docs.rs/chrono/0.4.7/chrono/format/strftime/index.html) used to format the time. |
| `style` | `bold yellow` | The style for the module time |
| `disabled` | `true` | Disables the `time` module. |
-If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `12hr` setting.
+If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`. Manually setting `format` will override the `use_12hr` setting.
### Example
diff --git a/src/configs/mod.rs b/src/configs/mod.rs
index 213ec36a5..16461792d 100644
--- a/src/configs/mod.rs
+++ b/src/configs/mod.rs
@@ -4,6 +4,7 @@ pub mod character;
pub mod dotnet;
pub mod kubernetes;
pub mod rust;
+pub mod time;
use crate::config::{ModuleConfig, RootModuleConfig};
diff --git a/src/configs/time.rs b/src/configs/time.rs
new file mode 100644
index 000000000..95483eb6c
--- /dev/null
+++ b/src/configs/time.rs
@@ -0,0 +1,23 @@
+use crate::config::{ModuleConfig, RootModuleConfig};
+
+use ansi_term::{Color, Style};
+use starship_module_config_derive::ModuleConfig;
+
+#[derive(Clone, ModuleConfig)]
+pub struct TimeConfig<'a> {
+ pub use_12hr: bool,
+ pub format: Option<&'a str>,
+ pub style: Style,
+ pub disabled: bool,
+}
+
+impl<'a> RootModuleConfig<'a> for TimeConfig<'a> {
+ fn new() -> Self {
+ TimeConfig {
+ use_12hr: false,
+ format: None,
+ style: Color::Yellow.bold(),
+ disabled: true,
+ }
+ }
+}
diff --git a/src/modules/time.rs b/src/modules/time.rs
index 344c17cfb..7009f984e 100644
--- a/src/modules/time.rs
+++ b/src/modules/time.rs
@@ -1,29 +1,22 @@
-use ansi_term::Color;
use chrono::{DateTime, Local};
use super::{Context, Module};
+use crate::config::{RootModuleConfig, SegmentConfig};
+use crate::configs::time::TimeConfig;
+
/// Outputs the current time
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
- let mut module = context.new_module("time");
+ const TIME_PREFIX: &str = "at ";
- if module.config_value_bool("disabled").unwrap_or(true) {
+ let mut module = context.new_module("time");
+ let config: TimeConfig = TimeConfig::try_load(module.config);
+ if config.disabled {
return None;
- }
-
- let module_style = module
- .config_value_style("style")
- .unwrap_or_else(|| Color::Yellow.bold());
- module.set_style(module_style);
+ };
- // Load module settings
- let is_12hr = module.config_value_bool("12hr").unwrap_or(false);
-
- let default_format = if is_12hr { "%r" } else { "%T" };
- let time_format = module
- .config_value_str("format")
- .unwrap_or(default_format)
- .to_owned();
+ let default_format = if config.use_12hr { "%r" } else { "%T" };
+ let time_format = config.format.unwrap_or(default_format);
log::trace!(
"Timer module is enabled with format string: {}",
@@ -31,9 +24,19 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
);
let local: DateTime<Local> = Local::now();
- let formatted_time_string = format_time(&time_format, local);
- module.new_segment("time", &formatted_time_string);
- module.get_prefix().set_value("at ");
+ let formatted_time_string = format_time(time_format, local);
+
+ module.set_style(config.style);
+
+ module.get_prefix().set_value(TIME_PREFIX);
+
+ module.create_segment(
+ "time",
+ &SegmentConfig {
+ value: &formatted_time_string,
+ style: None,
+ },
+ );
Some(module)
}