Skip to content

Commit

Permalink
Add command execution time (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion authored Jul 25, 2023
1 parent 2118fc3 commit 581fd02
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
3 changes: 2 additions & 1 deletion cmd/gokart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
19 changes: 19 additions & 0 deletions gokart.zsh-theme
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions internal/execTime.go
Original file line number Diff line number Diff line change
@@ -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")
}
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 581fd02

Please sign in to comment.