1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-11 07:40:57 +00:00

feat: cmd_duration module optionally reports milliseconds (#696)

This commit is contained in:
Luca Rinaldi 2019-12-19 14:38:06 -08:00 committed by Matan Kushner
parent d6094d7b9d
commit 6a2b0a67b0
9 changed files with 86 additions and 51 deletions

View File

@ -271,8 +271,9 @@ 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_000` | Shortest duration to show time for (in milliseconds). |
| `show_milliseconds` | `false` | Show milliseconds in addition to seconds for the duration. |
| `prefix` | `took` | Prefix to display immediately before the command duration. | | `prefix` | `took` | Prefix to display immediately before the command duration. |
| `style` | `"bold yellow"` | The style for the module. | | `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `cmd_duration` module. | | `disabled` | `false` | Disables the `cmd_duration` module. |
@ -283,7 +284,7 @@ running `eval $(starship init $0)`, and then proceed as normal.
# ~/.config/starship.toml # ~/.config/starship.toml
[cmd_duration] [cmd_duration]
min_time = 4 min_time = 500
prefix = "underwent " prefix = "underwent "
``` ```
@ -300,7 +301,7 @@ This does not suppress conda's own prompt modifier, you may want to run `conda c
### Options ### Options
| Variable | Default | Description | | Variable | Default | Description |
| ------------------- | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ------------------- | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `truncation_length` | `1` | The number of directories the environment path should be truncated to, if the environment was created via `conda create -p [path]`. `0` means no truncation. Also see the [`directory`](#directory) module. | | `truncation_length` | `1` | The number of directories the environment path should be truncated to, if the environment was created via `conda create -p [path]`. `0` means no truncation. Also see the [`directory`](#directory) module. |
| `symbol` | `"C "` | The symbol used before the environment name. | | `symbol` | `"C "` | The symbol used before the environment name. |
| `style` | `"bold green"` | The style for the module. | | `style` | `"bold green"` | The style for the module. |
@ -1011,7 +1012,7 @@ The module will be shown if any of the following conditions are met:
### Options ### Options
| Variable | Default | Description | | Variable | Default | Description |
| -------------- | ------------- | ----------------------------------------------------------- | | -------------- | ------------ | ----------------------------------------------------------- |
| `symbol` | `"💠 "` | The symbol used before displaying the terraform workspace. | | `symbol` | `"💠 "` | The symbol used before displaying the terraform workspace. |
| `show_version` | `false` | Shows the terraform version. Very slow on large workspaces. | | `show_version` | `false` | Shows the terraform version. Very slow on large workspaces. |
| `style` | `"bold 105"` | The style for the module. | | `style` | `"bold 105"` | The style for the module. |

View File

@ -8,14 +8,16 @@ pub struct CmdDurationConfig<'a> {
pub min_time: i64, pub min_time: i64,
pub prefix: &'a str, pub prefix: &'a str,
pub style: Style, pub style: Style,
pub show_milliseconds: bool,
pub disabled: bool, pub disabled: bool,
} }
impl<'a> RootModuleConfig<'a> for CmdDurationConfig<'a> { impl<'a> RootModuleConfig<'a> for CmdDurationConfig<'a> {
fn new() -> Self { fn new() -> Self {
CmdDurationConfig { CmdDurationConfig {
min_time: 2, min_time: 2_000,
prefix: "took ", prefix: "took ",
show_milliseconds: false,
style: Color::Yellow.bold(), style: Color::Yellow.bold(),
disabled: false, disabled: false,
} }

View File

@ -20,7 +20,7 @@ starship_preexec() {
# Avoid restarting the timer for commands in the same pipeline # Avoid restarting the timer for commands in the same pipeline
if [ "$PREEXEC_READY" = "true" ]; then if [ "$PREEXEC_READY" = "true" ]; then
PREEXEC_READY=false PREEXEC_READY=false
STARSHIP_START_TIME=$(date +%s) STARSHIP_START_TIME=$(::STARSHIP:: time)
fi fi
: "$PREV_LAST_ARG" : "$PREV_LAST_ARG"
@ -36,7 +36,7 @@ starship_precmd() {
# Prepare the timer data, if needed. # Prepare the timer data, if needed.
if [[ $STARSHIP_START_TIME ]]; then if [[ $STARSHIP_START_TIME ]]; then
STARSHIP_END_TIME=$(date +%s) STARSHIP_END_TIME=$(::STARSHIP:: time)
STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME)) STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME))
PS1="$(::STARSHIP:: prompt --status=$STATUS --jobs="$(jobs -p | wc -l)" --cmd-duration=$STARSHIP_DURATION)" PS1="$(::STARSHIP:: prompt --status=$STATUS --jobs="$(jobs -p | wc -l)" --cmd-duration=$STARSHIP_DURATION)"
unset STARSHIP_START_TIME unset STARSHIP_START_TIME
@ -65,10 +65,15 @@ else
trap 'starship_preexec_all "$_"' DEBUG trap 'starship_preexec_all "$_"' DEBUG
fi fi
# Finally, prepare the precmd function and set up the start time. # Finally, prepare the precmd function and set up the start time. We will avoid to
PROMPT_COMMAND="starship_precmd;$PROMPT_COMMAND" # add multiple instances of the starship function and keep other user functions if any.
if [[ -z "$PROMPT_COMMAND" ]]; then
PROMPT_COMMAND="starship_precmd"
elif [[ "$PROMPT_COMMAND" != *"starship_precmd" ]]; then
PROMPT_COMMAND="$PROMPT_COMMAND;starship_precmd"
fi
fi fi
# Set up the start time and STARSHIP_SHELL, which controls shell-specific sequences # Set up the start time and STARSHIP_SHELL, which controls shell-specific sequences
STARSHIP_START_TIME=$(date +%s) STARSHIP_START_TIME=$(::STARSHIP:: time)
export STARSHIP_SHELL="bash" export STARSHIP_SHELL="bash"

View File

@ -7,8 +7,7 @@ function fish_prompt
end end
set -l exit_code $status set -l exit_code $status
# Account for changes in variable name between v2.7 and v3.0 # Account for changes in variable name between v2.7 and v3.0
set -l CMD_DURATION "$CMD_DURATION$cmd_duration" set -l starship_duration "$CMD_DURATION$cmd_duration"
set -l starship_duration (math --scale=0 "$CMD_DURATION / 1000")
::STARSHIP:: prompt --status=$exit_code --keymap=$keymap --cmd-duration=$starship_duration --jobs=(count (jobs -p)) ::STARSHIP:: prompt --status=$exit_code --keymap=$keymap --cmd-duration=$starship_duration --jobs=(count (jobs -p))
end end

View File

@ -8,7 +8,7 @@ function global:prompt {
$jobs = @(Get-Job | Where-Object { $_.State -eq 'Running' }).Count $jobs = @(Get-Job | Where-Object { $_.State -eq 'Running' }).Count
if ($lastCmd = Get-History -Count 1) { if ($lastCmd = Get-History -Count 1) {
$duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalSeconds) $duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds)
# & ensures the path is interpreted as something to execute # & ensures the path is interpreted as something to execute
$out = @(&::STARSHIP:: prompt "--path=$PWD" --status=$lastexitcode --jobs=$jobs --cmd-duration=$duration) $out = @(&::STARSHIP:: prompt "--path=$PWD" --status=$lastexitcode --jobs=$jobs --cmd-duration=$duration)
} else { } else {

View File

@ -19,7 +19,7 @@ starship_precmd() {
NUM_JOBS=$#jobstates NUM_JOBS=$#jobstates
# Compute cmd_duration, if we have a time to consume # Compute cmd_duration, if we have a time to consume
if [[ ! -z "${STARSHIP_START_TIME+1}" ]]; then if [[ ! -z "${STARSHIP_START_TIME+1}" ]]; then
STARSHIP_END_TIME="$(date +%s)" STARSHIP_END_TIME=$(::STARSHIP:: time)
STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME)) STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME))
PROMPT="$(::STARSHIP:: prompt --status=$STATUS --cmd-duration=$STARSHIP_DURATION --jobs="$NUM_JOBS")" PROMPT="$(::STARSHIP:: prompt --status=$STATUS --cmd-duration=$STARSHIP_DURATION --jobs="$NUM_JOBS")"
unset STARSHIP_START_TIME unset STARSHIP_START_TIME
@ -28,7 +28,7 @@ starship_precmd() {
fi fi
} }
starship_preexec(){ starship_preexec(){
STARSHIP_START_TIME="$(date +%s)" STARSHIP_START_TIME=$(::STARSHIP:: time)
} }
# If precmd/preexec arrays are not already set, set them. If we don't do this, # If precmd/preexec arrays are not already set, set them. If we don't do this,
@ -53,6 +53,6 @@ function zle-keymap-select
zle reset-prompt zle reset-prompt
} }
STARSHIP_START_TIME="$(date +%s)" STARSHIP_START_TIME=$(::STARSHIP:: time)
zle -N zle-keymap-select zle -N zle-keymap-select
export STARSHIP_SHELL="zsh" export STARSHIP_SHELL="zsh"

View File

@ -1,3 +1,5 @@
use std::time::SystemTime;
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;
@ -44,7 +46,7 @@ fn main() {
.short("d") .short("d")
.long("cmd-duration") .long("cmd-duration")
.value_name("CMD_DURATION") .value_name("CMD_DURATION")
.help("The execution duration of the last command, in seconds") .help("The execution duration of the last command, in milliseconds")
.takes_value(true); .takes_value(true);
let keymap_arg = Arg::with_name("keymap") let keymap_arg = Arg::with_name("keymap")
@ -115,6 +117,11 @@ fn main() {
.subcommand(SubCommand::with_name("bug-report").about( .subcommand(SubCommand::with_name("bug-report").about(
"Create a pre-populated GitHub issue with information about your configuration", "Create a pre-populated GitHub issue with information about your configuration",
)) ))
.subcommand(
SubCommand::with_name("time")
.about("Prints time in milliseconds")
.settings(&[AppSettings::Hidden]),
)
.get_matches(); .get_matches();
match matches.subcommand() { match matches.subcommand() {
@ -141,6 +148,15 @@ fn main() {
} }
("configure", Some(_)) => configure::edit_configuration(), ("configure", Some(_)) => configure::edit_configuration(),
("bug-report", Some(_)) => bug_report::create(), ("bug-report", Some(_)) => bug_report::create(),
("time", _) => {
match SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.ok()
{
Some(time) => println!("{}", time.as_millis()),
None => println!("{}", -1),
}
}
_ => {} _ => {}
} }
} }

View File

@ -15,7 +15,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let elapsed = props let elapsed = props
.get("cmd_duration") .get("cmd_duration")
.unwrap_or(&"invalid_time".into()) .unwrap_or(&"invalid_time".into())
.parse::<u64>() .parse::<u128>()
.ok()?; .ok()?;
/* TODO: Once error handling is implemented, warn the user if their config /* TODO: Once error handling is implemented, warn the user if their config
@ -28,7 +28,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; return None;
} }
let config_min = config.min_time as u64; let config_min = config.min_time as u128;
let module_color = match elapsed { let module_color = match elapsed {
time if time < config_min => return None, time if time < config_min => return None,
@ -36,7 +36,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}; };
module.set_style(module_color); module.set_style(module_color);
let cmd_duration_stacked = &format!("{}{}", config.prefix, render_time(elapsed)); let cmd_duration_stacked = &format!(
"{}{}",
config.prefix,
render_time(elapsed, config.show_milliseconds)
);
module.create_segment("cmd_duration", &SegmentConfig::new(&cmd_duration_stacked)); module.create_segment("cmd_duration", &SegmentConfig::new(&cmd_duration_stacked));
module.get_prefix().set_value(""); module.get_prefix().set_value("");
@ -44,8 +48,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
} }
// Render the time into a nice human-readable string // Render the time into a nice human-readable string
fn render_time(raw_seconds: u64) -> String { fn render_time(raw_millis: u128, show_millis: bool) -> String {
// Calculate a simple breakdown into days/hours/minutes/seconds // Calculate a simple breakdown into days/hours/minutes/seconds/milliseconds
let (millis, raw_seconds) = (raw_millis % 1000, raw_millis / 1000);
let (seconds, raw_minutes) = (raw_seconds % 60, raw_seconds / 60); let (seconds, raw_minutes) = (raw_seconds % 60, raw_seconds / 60);
let (minutes, raw_hours) = (raw_minutes % 60, raw_minutes / 60); let (minutes, raw_hours) = (raw_minutes % 60, raw_minutes / 60);
let (hours, days) = (raw_hours % 24, raw_hours / 24); let (hours, days) = (raw_hours % 24, raw_hours / 24);
@ -53,16 +58,19 @@ fn render_time(raw_seconds: u64) -> String {
let components = [days, hours, minutes, seconds]; let components = [days, hours, minutes, seconds];
let suffixes = ["d", "h", "m", "s"]; let suffixes = ["d", "h", "m", "s"];
let rendered_components: Vec<String> = components let mut rendered_components: Vec<String> = components
.iter() .iter()
.zip(&suffixes) .zip(&suffixes)
.map(render_time_component) .map(render_time_component)
.collect(); .collect();
if show_millis || raw_millis < 1000 {
rendered_components.push(render_time_component((&millis, &"ms")));
}
rendered_components.join("") rendered_components.join("")
} }
/// Render a single component of the time string, giving an empty string if component is zero /// Render a single component of the time string, giving an empty string if component is zero
fn render_time_component((component, suffix): (&u64, &&str)) -> String { fn render_time_component((component, suffix): (&u128, &&str)) -> String {
match component { match component {
0 => String::new(), 0 => String::new(),
n => format!("{}{}", n, suffix), n => format!("{}{}", n, suffix),
@ -73,20 +81,24 @@ fn render_time_component((component, suffix): (&u64, &&str)) -> String {
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn test_500ms() {
assert_eq!(render_time(500 as u128, true), "500ms")
}
#[test] #[test]
fn test_10s() { fn test_10s() {
assert_eq!(render_time(10 as u64), "10s") assert_eq!(render_time(10_000 as u128, true), "10s")
} }
#[test] #[test]
fn test_90s() { fn test_90s() {
assert_eq!(render_time(90 as u64), "1m30s") assert_eq!(render_time(90_000 as u128, true), "1m30s")
} }
#[test] #[test]
fn test_10110s() { fn test_10110s() {
assert_eq!(render_time(10110 as u64), "2h48m30s") assert_eq!(render_time(10_110_000 as u128, true), "2h48m30s")
} }
#[test] #[test]
fn test_1d() { fn test_1d() {
assert_eq!(render_time(86400 as u64), "1d") assert_eq!(render_time(86_400_000 as u128, true), "1d")
} }
} }

View File

@ -6,7 +6,7 @@ use crate::common::{self, TestCommand};
#[test] #[test]
fn config_blank_duration_1s() -> io::Result<()> { fn config_blank_duration_1s() -> io::Result<()> {
let output = common::render_module("cmd_duration") let output = common::render_module("cmd_duration")
.arg("--cmd-duration=1") .arg("--cmd-duration=1000")
.output()?; .output()?;
let actual = String::from_utf8(output.stdout).unwrap(); let actual = String::from_utf8(output.stdout).unwrap();
@ -18,7 +18,7 @@ fn config_blank_duration_1s() -> io::Result<()> {
#[test] #[test]
fn config_blank_duration_5s() -> io::Result<()> { fn config_blank_duration_5s() -> io::Result<()> {
let output = common::render_module("cmd_duration") let output = common::render_module("cmd_duration")
.arg("--cmd-duration=5") .arg("--cmd-duration=5000")
.output()?; .output()?;
let actual = String::from_utf8(output.stdout).unwrap(); let actual = String::from_utf8(output.stdout).unwrap();
@ -32,9 +32,9 @@ fn config_5s_duration_3s() -> io::Result<()> {
let output = common::render_module("cmd_duration") let output = common::render_module("cmd_duration")
.use_config(toml::toml! { .use_config(toml::toml! {
[cmd_duration] [cmd_duration]
min_time = 5 min_time = 5000
}) })
.arg("--cmd-duration=3") .arg("--cmd-duration=3000")
.output()?; .output()?;
let actual = String::from_utf8(output.stdout).unwrap(); let actual = String::from_utf8(output.stdout).unwrap();
@ -48,9 +48,9 @@ fn config_5s_duration_10s() -> io::Result<()> {
let output = common::render_module("cmd_duration") let output = common::render_module("cmd_duration")
.use_config(toml::toml! { .use_config(toml::toml! {
[cmd_duration] [cmd_duration]
min_time = 5 min_time = 5000
}) })
.arg("--cmd-duration=10") .arg("--cmd-duration=10000")
.output()?; .output()?;
let actual = String::from_utf8(output.stdout).unwrap(); let actual = String::from_utf8(output.stdout).unwrap();
@ -66,7 +66,7 @@ fn config_1s_duration_prefix_underwent() -> io::Result<()> {
[cmd_duration] [cmd_duration]
prefix = "underwent " prefix = "underwent "
}) })
.arg("--cmd-duration=1") .arg("--cmd-duration=1000")
.output()?; .output()?;
let actual = String::from_utf8(output.stdout).unwrap(); let actual = String::from_utf8(output.stdout).unwrap();
@ -82,7 +82,7 @@ fn config_5s_duration_prefix_underwent() -> io::Result<()> {
[cmd_duration] [cmd_duration]
prefix = "underwent " prefix = "underwent "
}) })
.arg("--cmd-duration=5") .arg("--cmd-duration=5000")
.output()?; .output()?;
let actual = String::from_utf8(output.stdout).unwrap(); let actual = String::from_utf8(output.stdout).unwrap();