1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 15:56:28 +00:00

refactor: Rewrite time module to use module config (#483)

- Config option renamed: `12hr` → `use_12hr`
This commit is contained in:
Thomas Lee 2019-10-06 00:07:00 +09:00 committed by Matan Kushner
parent bc9e44f45c
commit 27e8c8f3eb
9 changed files with 60 additions and 33 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@ pub mod character;
pub mod dotnet;
pub mod kubernetes;
pub mod rust;
pub mod time;
use crate::config::{ModuleConfig, RootModuleConfig};

23
src/configs/time.rs Normal file
View File

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

View File

@ -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>> {
const TIME_PREFIX: &str = "at ";
let mut module = context.new_module("time");
if module.config_value_bool("disabled").unwrap_or(true) {
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)
}