Skip to content

Commit

Permalink
fix: preserve exit code in omp.bash
Browse files Browse the repository at this point in the history
Other small changes include:

- rename functions to simplify debugging
- quoting some variables and simplifying an expression as suggested by shellcheck
- idempotency when modifying PROMPT_COMMAND.
- check if file exists before trying to remove it in the trap.
  • Loading branch information
erclu authored and JanDeDobbeleer committed Mar 18, 2021
1 parent e7cccf5 commit 8de004f
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/init/omp.bash
Expand Up @@ -4,23 +4,27 @@ TIMER_START="/tmp/${USER}.start.$$"

PS0='$(::OMP:: --millis > $TIMER_START)'

function _update_ps1() {
function _omp_hook() {
local ret=$?

omp_elapsed=-1
if [[ -f $TIMER_START ]]; then
omp_now=$(::OMP:: --millis)
omp_start_time=$(cat "$TIMER_START")
omp_elapsed=$(($omp_now-$omp_start_time))
rm $TIMER_START
omp_elapsed=$((omp_now-omp_start_time))
rm "$TIMER_START"
fi
PS1="$(::OMP:: --config $POSH_THEME --error $? --execution-time $omp_elapsed --shell bash)"
PS1="$(::OMP:: --config $POSH_THEME --shell bash --error $ret --execution-time $omp_elapsed --shell bash)"

return $ret
}

if [ "$TERM" != "linux" ] && [ -x "$(command -v ::OMP::)" ]; then
PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
if [ "$TERM" != "linux" ] && [ -x "$(command -v ::OMP::)" ] && ! [[ "$PROMPT_COMMAND" =~ "_omp_hook" ]]; then
PROMPT_COMMAND="_omp_hook; $PROMPT_COMMAND"
fi

function runonexit() {
rm $TIMER_START
function _omp_runonexit() {
[[ -f $TIMER_START ]] && rm "$TIMER_START"
}

trap runonexit EXIT
trap _omp_runonexit EXIT

0 comments on commit 8de004f

Please sign in to comment.