Skip to content

Instantly share code, notes, and snippets.

@knadh
Last active May 30, 2024 12:32
Show Gist options
  • Save knadh/123bca5cfdae8645db750bfb49cb44b0 to your computer and use it in GitHub Desktop.
Save knadh/123bca5cfdae8645db750bfb49cb44b0 to your computer and use it in GitHub Desktop.
Elapsed and execution time for commands in ZSH

Elapsed and execution time display for commands in ZSH

Append this to your ~/.zshrc file.

function preexec() {
  timer=$(($(date +%s%0N)/1000000))
}

function precmd() {
  if [ $timer ]; then
    now=$(($(date +%s%0N)/1000000))
    elapsed=$(($now-$timer))

    export RPROMPT="%F{cyan}${elapsed}ms %{$reset_color%}"
    unset timer
  fi
}

Remixed from @adri's snippet.

@digitaldonkey
Copy link

<3 @mezza

@Joilence
Copy link

Is it possible to add this execution time at the rear of the first line like the style of Spaceship Prompt?

@sudarshan85
Copy link

Dumb question but whats the math for getting this in seconds? Change 1000000 to 1000?

@Sleepful
Copy link

<3 @jututt

@yanis-fourel
Copy link

Dumb question but whats the math for getting this in seconds? Change 1000000 to 1000?

Here is the same snippet as above, but it displays as seconds with 3 decimals

function preexec() {
  timer=$(($(date +%s%0N)*0.000000001))
}

function precmd() {
  if [ $timer ]; then
    now=$(($(date +%s%0N)*0.000000001))
    elapsed=$(echo $(($now-$timer)) | awk '{printf "%.3f", $1}')

    export RPROMPT="%F{cyan}${elapsed}s %{$reset_color%}"
    unset timer
  fi
}

@varalgit
Copy link

  cmd_start=$(($(print -P %D{%s%6.}) / 1000))

Why not simply use the following?

  cmd_start=$(print -P %D{%s%3.})

No need to ask for microsecs and then divide by 1000, if you can get millisecs directly...

@varalgit
Copy link

varalgit commented Mar 14, 2023

I wanted slightly more accuracy, and the time on the left of the normal prompt, so I've changed the code of rafbm to this (works on macos 13):

myprompt="%B%F{cyan}%n@%m:%d/ %h%(!.#.>)%f%b"

function preexec() {
  timer=$(print -P %D{%s%3.})
}

function precmd() {
  timeprompt=""	
  if [ $timer ]; then
    now=$(print -P %D{%s%3.})
    local d_ms=$(($now - $timer))
    local d_s=$((d_ms / 1000))
    local ms=$((d_ms % 1000))
    local s=$((d_s % 60))
    local m=$(((d_s / 60) % 60))
    local h=$((d_s / 3600))

    if   ((h > 0)); then timeprompt=${h}h${m}m${s}s
    elif ((m > 0)); then timeprompt=${m}m${s}.$(printf $(($ms / 100)))s # 1m12.3s
    elif ((s > 9)); then timeprompt=${s}.$(printf %02d $(($ms / 10)))s # 12.34s
    elif ((s > 0)); then timeprompt=${s}.$(printf %03d $ms)s # 1.234s
    else timeprompt=${ms}ms
    fi
    timeprompt="%B%F{yellow}${timeprompt} %f%b"
    unset timer
  fi
  export PS1=${timeprompt}${myprompt}
}

@junguler
Copy link

junguler commented Nov 4, 2023

here is my idea of a simple prompt with random colors and also a slightly modified way of showing the numbers, having a 2 part place holder prompt when starting zsh and a 3 part prompt with elapsed time, also includes black and white part for error-ed out commands

had some help from here for making the random colors in the function part of the prompt work

PROMPT='%F{$(($RANDOM%6+1))}[%f%D{%H:%M}%F{$(($RANDOM%6+1))}] %F{$(($RANDOM%6+1))}[%f$(shrink_path -f)%F{$(($RANDOM%6+1))}]%f '

function preexec() {
  timer=$(date +%s%3N)
}

function precmd() {
  if [ $timer ]; then
    local now=$(date +%s%3N)
    local d_ms=$(($now-$timer))
    local d_s=$((d_ms / 1000))
    local ms=$((d_ms % 1000))
    local s=$((d_s % 60))
    local m=$(((d_s / 60) % 60))
    local h=$((d_s / 3600))
    if ((h > 0)); then elapsed=(${h}h ${m}m)
    elif ((m > 0)); then elapsed=(${m}m ${s}s)
    elif ((s >= 10)); then elapsed=${s}.$((ms / 100))s
    elif ((s > 0)); then elapsed=${s}.$((ms / 10))s
    else elapsed=${ms}ms
    fi

	psvar=("$(shrink_path -f)" "$elapsed" "[" "]")
	local ok="%F{$((RANDOM%6+1))}%3v%f%D{%H:%M}%F{$((RANDOM%6+1))}%4v %F{$((RANDOM%6+1))}%3v%f%1v%F{$((RANDOM%6+1))}%4v %F{$((RANDOM%6+1))}%3v%f%2v%F{$((RANDOM%6+1))}%4v%f "
	local err="%K{7}%F{0}%3v%D{%H:%M}%4v%k %K{7}%3v%1v%4v%k %K{7}%3v%2v%4v%k%f "
	PROMPT="%(?.$ok.$err)"

    unset timer
  fi
}

@akhildevelops
Copy link

Shows execution time at the end of the prompt

OG_PROMPT=$PROMPT
function preexec() {
  timer=$(($(date +%s%0N)/1000000))
}

function precmd() {
  if [ $timer ]; then
    now=$(($(date +%s%0N)/1000000))
    elapsed=$(($now-$timer))

    export PROMPT="$OG_PROMPT%F{cyan}${elapsed}ms %{$reset_color%}> "
    unset timer
  fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment