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:
parent
bc9e44f45c
commit
27e8c8f3eb
@ -874,13 +874,13 @@ To enable it, set `disabled` to `false` in your configuration file.
|
|||||||
|
|
||||||
| Variable | Default | Description |
|
| 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. |
|
| `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 |
|
| `style` | `bold yellow` | The style for the module time |
|
||||||
| `disabled` | `true` | Disables the `time` module. |
|
| `disabled` | `true` | Disables the `time` module. |
|
||||||
|
|
||||||
If `12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`.
|
If `use_12hr` is `true`, then `format` defaults to `"%r"`. Otherwise, it defaults to `"%T"`.
|
||||||
Manually setting `format` will override the `12hr` setting.
|
Manually setting `format` will override the `use_12hr` setting.
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
|
@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
|
|||||||
|
|
||||||
| Variable | Default | Description |
|
| 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. |
|
| `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 |
|
| `style` | `bold yellow` | The style for the module time |
|
||||||
| `disabled` | `true` | Disables the `time` module. |
|
| `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
|
### Example
|
||||||
|
|
||||||
|
@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
|
|||||||
|
|
||||||
| Variable | Default | Description |
|
| 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. |
|
| `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 |
|
| `style` | `bold yellow` | The style for the module time |
|
||||||
| `disabled` | `true` | Disables the `time` module. |
|
| `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
|
### Example
|
||||||
|
|
||||||
|
@ -813,12 +813,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
|
|||||||
|
|
||||||
| Variable | Default | Description |
|
| 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. |
|
| `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 |
|
| `style` | `bold yellow` | The style for the module time |
|
||||||
| `disabled` | `true` | Disables the `time` module. |
|
| `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
|
### Example
|
||||||
|
|
||||||
|
@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
|
|||||||
|
|
||||||
| Variable | Default | Description |
|
| 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. |
|
| `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 |
|
| `style` | `bold yellow` | The style for the module time |
|
||||||
| `disabled` | `true` | Disables the `time` module. |
|
| `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
|
### Example
|
||||||
|
|
||||||
|
@ -812,12 +812,12 @@ This module is disabled by default. To enable it, set `disabled` to `false` in y
|
|||||||
|
|
||||||
| Variable | Default | Description |
|
| 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. |
|
| `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 |
|
| `style` | `bold yellow` | The style for the module time |
|
||||||
| `disabled` | `true` | Disables the `time` module. |
|
| `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
|
### Example
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ pub mod character;
|
|||||||
pub mod dotnet;
|
pub mod dotnet;
|
||||||
pub mod kubernetes;
|
pub mod kubernetes;
|
||||||
pub mod rust;
|
pub mod rust;
|
||||||
|
pub mod time;
|
||||||
|
|
||||||
use crate::config::{ModuleConfig, RootModuleConfig};
|
use crate::config::{ModuleConfig, RootModuleConfig};
|
||||||
|
|
||||||
|
23
src/configs/time.rs
Normal file
23
src/configs/time.rs
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,29 +1,22 @@
|
|||||||
use ansi_term::Color;
|
|
||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Local};
|
||||||
|
|
||||||
use super::{Context, Module};
|
use super::{Context, Module};
|
||||||
|
|
||||||
|
use crate::config::{RootModuleConfig, SegmentConfig};
|
||||||
|
use crate::configs::time::TimeConfig;
|
||||||
|
|
||||||
/// Outputs the current time
|
/// Outputs the current time
|
||||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
|
const TIME_PREFIX: &str = "at ";
|
||||||
|
|
||||||
let mut module = context.new_module("time");
|
let mut module = context.new_module("time");
|
||||||
|
let config: TimeConfig = TimeConfig::try_load(module.config);
|
||||||
if module.config_value_bool("disabled").unwrap_or(true) {
|
if config.disabled {
|
||||||
return None;
|
return None;
|
||||||
}
|
};
|
||||||
|
|
||||||
let module_style = module
|
let default_format = if config.use_12hr { "%r" } else { "%T" };
|
||||||
.config_value_style("style")
|
let time_format = config.format.unwrap_or(default_format);
|
||||||
.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();
|
|
||||||
|
|
||||||
log::trace!(
|
log::trace!(
|
||||||
"Timer module is enabled with format string: {}",
|
"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 local: DateTime<Local> = Local::now();
|
||||||
let formatted_time_string = format_time(&time_format, local);
|
let formatted_time_string = format_time(time_format, local);
|
||||||
module.new_segment("time", &formatted_time_string);
|
|
||||||
module.get_prefix().set_value("at ");
|
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)
|
Some(module)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user