1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-09-22 10:49:02 +00:00

feat: Added ability for setting command duration prefix (#414)

This commit is contained in:
Tom Hotston 2019-09-30 06:26:32 +01:00 committed by Matan Kushner
parent 61604a4a8e
commit 7588137b09
3 changed files with 48 additions and 6 deletions

View File

@ -257,11 +257,12 @@ running `eval $(starship init $0)`, and then proceed as normal.
### Options ### Options
| Variable | Default | Description | | Variable | Default | Description |
| ---------- | --------------- | ----------------------------------- | | ---------- | --------------- | ------------------------------------------- |
| `min_time` | `2` | Shortest duration to show time for. | | `min_time` | `2` | Shortest duration to show time for. |
| `style` | `"bold yellow"` | The style for the module. | | `prefix` | `took ` | Prefix to display immediately cmd_duration. |
| `disabled` | `false` | Disables the `cmd_duration` module. | | `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `cmd_duration` module. |
### Example ### Example
@ -270,6 +271,7 @@ running `eval $(starship init $0)`, and then proceed as normal.
[cmd_duration] [cmd_duration]
min_time = 4 min_time = 4
prefix = "underwent "
``` ```
## Directory ## Directory

View File

@ -16,6 +16,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.parse::<u64>() .parse::<u64>()
.ok()?; .ok()?;
let prefix = module
.config_value_str("prefix")
.unwrap_or("took ")
.to_owned();
let signed_config_min = module.config_value_i64("min_time").unwrap_or(2); let signed_config_min = module.config_value_i64("min_time").unwrap_or(2);
/* TODO: Once error handling is implemented, warn the user if their config /* TODO: Once error handling is implemented, warn the user if their config
@ -38,7 +43,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}; };
module.set_style(module_color); module.set_style(module_color);
module.new_segment("cmd_duration", &format!("took {}", render_time(elapsed))); module.new_segment(
"cmd_duration",
&format!("{}{}", prefix, render_time(elapsed)),
);
module.get_prefix().set_value(""); module.get_prefix().set_value("");
Some(module) Some(module)

View File

@ -58,3 +58,35 @@ fn config_5s_duration_10s() -> io::Result<()> {
assert_eq!(expected, actual); assert_eq!(expected, actual);
Ok(()) Ok(())
} }
#[test]
fn config_1s_duration_prefix_underwent() -> io::Result<()> {
let output = common::render_module("cmd_duration")
.use_config(toml::toml! {
[cmd_duration]
prefix = "underwent "
})
.arg("--cmd-duration=1")
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = "";
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn config_5s_duration_prefix_underwent() -> io::Result<()> {
let output = common::render_module("cmd_duration")
.use_config(toml::toml! {
[cmd_duration]
prefix = "underwent "
})
.arg("--cmd-duration=5")
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = format!("{} ", Color::Yellow.bold().paint("underwent 5s"));
assert_eq!(expected, actual);
Ok(())
}