refactor(bash): simplify timer

This commit is contained in:
L. Yeung 2022-08-09 16:16:33 +08:00 committed by Jan De Dobbeleer
parent 3345017b2d
commit 83c1af63b4
5 changed files with 12 additions and 27 deletions

View file

@ -1,7 +1,7 @@
package engine
import (
"io/ioutil" //nolint: staticcheck
"io/ioutil" //nolint:staticcheck,nolintlint
"oh-my-posh/color"
"os"
"path/filepath"

View file

@ -24,7 +24,7 @@ package battery
import (
"errors"
"fmt"
"io/ioutil"
"io/ioutil" //nolint:staticcheck,nolintlint
"os"
"path/filepath"
"strconv"

View file

@ -5,7 +5,7 @@
package font
import (
"io/ioutil"
"io/ioutil" //nolint:staticcheck,nolintlint
"os"
"path"
"strings"

View file

@ -3,7 +3,6 @@ package shell
import (
_ "embed"
"path/filepath"
"runtime"
"strconv"
"fmt"
@ -52,7 +51,7 @@ func getExecutablePath(env environment.Environment) (string, error) {
// On Windows, it fails when the excutable is called in MSYS2 for example
// which uses unix style paths to resolve the executable's location.
// PowerShell knows how to resolve both, so we can swap this without any issue.
if runtime.GOOS == environment.WINDOWS {
if env.GOOS() == environment.WINDOWS {
executable = strings.ReplaceAll(executable, "\\", "/")
}
return executable, nil
@ -98,7 +97,7 @@ func quotePosixStr(str string) string {
needQuoting = true
}
}
// the quoting form $'...' is used for a string includes any special characters
// the quoting form $'...' is used for a string contains any special characters
if needQuoting {
return fmt.Sprintf("$'%s'", b.String())
}
@ -127,7 +126,7 @@ func quoteFishStr(str string) string {
needQuoting = true
}
}
// single quotes are used when the string includes any special characters
// single quotes are used when the string contains any special characters
if needQuoting {
return fmt.Sprintf("'%s'", b.String())
}

View file

@ -1,44 +1,30 @@
export POSH_THEME=::CONFIG::
export POWERLINE_COMMAND="oh-my-posh"
export CONDA_PROMPT_MODIFIER=false
TIMER_START="/tmp/${USER}.start.$$"
# some environments don't have the filesystem we'd expect
if [[ ! -d "/tmp" ]]; then
TIMER_START="${HOME}/.${USER}.start.$$"
fi
omp_start_time=""
# start timer on command start
PS0='$(_omp_start_timer)'
PS0='${omp_start_time:0:$((omp_start_time="$(_omp_start_timer)",0))}'
# set secondary prompt
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=bash --shell-version="$BASH_VERSION")"
function _omp_start_timer() {
::OMP:: get millis > "$TIMER_START"
::OMP:: get millis
}
function _omp_hook() {
local ret=$?
local omp_stack_count=$((${#DIRSTACK[@]} - 1))
local omp_elapsed=-1
if [[ -f "$TIMER_START" ]]; then
omp_now=$(::OMP:: get millis)
omp_start_time=$(cat "$TIMER_START")
if [[ -n "$omp_start_time" ]]; then
local omp_now=$(::OMP:: get millis)
omp_elapsed=$((omp_now-omp_start_time))
rm -f "$TIMER_START"
omp_start_time=""
fi
PS1="$(::OMP:: print primary --config="$POSH_THEME" --shell=bash --shell-version="$BASH_VERSION" --error="$ret" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" | tr -d '\0')"
return $ret
}
if [ "$TERM" != "linux" ] && [ -x "$(command -v ::OMP::)" ] && ! [[ "$PROMPT_COMMAND" =~ "_omp_hook" ]]; then
PROMPT_COMMAND="_omp_hook; $PROMPT_COMMAND"
fi
function _omp_runonexit() {
[[ -f $TIMER_START ]] && rm -f "$TIMER_START"
}
trap _omp_runonexit EXIT