diff --git a/README.md b/README.md index 827e96e..1fc0b4a 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ This should print a "branch" icon. - Git branch and status information - Command exit code +- Command execution time[^zsh-only] - Version information for various tools and languages - Docker - .NET @@ -71,5 +72,7 @@ This should print a "branch" icon. - Ruby - Rust +[^zsh-only]: Only in Zsh, due to limitations of Bash. + Is something missing? Feel free to [open an issue](https://github.com/LucaScorpion/gokart-prompt/issues/new) or send a pull request! diff --git a/cmd/gokart.go b/cmd/gokart.go index e85b3fe..f7a8595 100644 --- a/cmd/gokart.go +++ b/cmd/gokart.go @@ -34,8 +34,9 @@ func ps1() { fmt.Print(internal.Path()) fmt.Print(git.Git()) fmt.Print(versions.All(wdFiles)) - fmt.Print(ansi.Reset()) + fmt.Print(internal.CmdTime()) + fmt.Print(ansi.Reset()) fmt.Println() ps2() } diff --git a/gokart.zsh-theme b/gokart.zsh-theme index a180f29..a9f9888 100644 --- a/gokart.zsh-theme +++ b/gokart.zsh-theme @@ -5,17 +5,36 @@ GOKART_PROMPT_DIR=${${(%):-%x}:A:h} gokart_prompt_precmd() { + # Store the previous command exit code. + # Note that this has to be the very first command of this function. export EXIT_CODE=$? + + # Store the current time, i.e. the previous command end time. + export GOKART_CMD_END=$EPOCHREALTIME + export GOKART_SHELL=zsh PS1=$("$GOKART_PROMPT_DIR/gokart" ps1) PS2=$("$GOKART_PROMPT_DIR/gokart" ps2) + + # Clear the entered command. + export GOKART_CMD= +} + +gokart_prompt_preexec() { + # Get and store the command that was entered. + # https://zsh.sourceforge.io/Doc/Release/Functions.html#index-preexec_005ffunctions + export GOKART_CMD="$1" + + # Store the command start time. + export GOKART_CMD_START=$EPOCHREALTIME } gokart_prompt_setup() { autoload -Uz add-zsh-hook add-zsh-hook precmd gokart_prompt_precmd + add-zsh-hook preexec gokart_prompt_preexec } gokart_prompt_setup diff --git a/internal/execTime.go b/internal/execTime.go new file mode 100644 index 0000000..cdedd56 --- /dev/null +++ b/internal/execTime.go @@ -0,0 +1,32 @@ +package internal + +import ( + "fmt" + "gokart-prompt/internal/ansi" + "os" + "strconv" +) + +func CmdTime() string { + // If no command was entered, never display the time. + if os.Getenv("GOKART_CMD") == "" { + return "" + } + + // Try to parse the command start and end times. + cmdStart, err := strconv.ParseFloat(os.Getenv("GOKART_CMD_START"), 64) + if err != nil { + return "" + } + cmdEnd, err := strconv.ParseFloat(os.Getenv("GOKART_CMD_END"), 64) + if err != nil { + return "" + } + + execTime := cmdEnd - cmdStart + if execTime < 1 { + return "" + } + + return ansi.Color(ansi.Yellow, " took "+fmt.Sprintf("%.1f", execTime)+"s") +} diff --git a/screenshot.png b/screenshot.png index 5a02453..6bce76b 100644 Binary files a/screenshot.png and b/screenshot.png differ