wick3dr0se
wick3dr0se

Reputation: 19

Timing command execution from .bashrc

I'm trying to create my own minimal BASH shell prompt that times execution of each command entered. So far I have a functional prompt that looks appealing for my desires; I'm just missing the time output, like starship does. I've tried reading user input from $@, $*, /dev/stdin while using time without success. I also tried using date at the start of the script and end while subtracting the difference to a similar affect. No matter what I've tried the execution timing is always off or doesn't work at all. I've checked documentation and several sites for suggestions but none worked

The closest result I got was using this snippet below

function timer_start {
  timer=${timer:-$SECONDS}
}

function timer_stop {
  timer_show=$(($SECONDS - $timer))
  unset timer
}

trap 'timer_start' DEBUG
PROMPT_COMMAND=timer_stop

PS1="$ ${timer_show}s "

Upvotes: 0

Views: 689

Answers (1)

wick3dr0se
wick3dr0se

Reputation: 19

With suggestions from @KamilCuk - I was able to get a functioning timer working from .bashrc! Currently it gets idle/typing + command execution time (which I think I prefer anyway)

prmpt() {
    timeStart=$(date +%s)

    sec=$(((timeStart-timeEnd)%60))
    min=$(((timeStart-timeEnd)%3600/60))
    hr=$(((timeStart-timeEnd)/3600))
    
    timeEnd=$timeStart

    printf '%sh:%sm:%ss' "$hr" "$min" "$sec"
}
PROMPT_COMMAND=prmpt
timeEnd=$(date +%s)

Upvotes: 2

Related Questions