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 package engine
import ( import (
"io/ioutil" //nolint: staticcheck "io/ioutil" //nolint:staticcheck,nolintlint
"oh-my-posh/color" "oh-my-posh/color"
"os" "os"
"path/filepath" "path/filepath"

View file

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

View file

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

View file

@ -3,7 +3,6 @@ package shell
import ( import (
_ "embed" _ "embed"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"fmt" "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 // On Windows, it fails when the excutable is called in MSYS2 for example
// which uses unix style paths to resolve the executable's location. // 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. // 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, "\\", "/") executable = strings.ReplaceAll(executable, "\\", "/")
} }
return executable, nil return executable, nil
@ -98,7 +97,7 @@ func quotePosixStr(str string) string {
needQuoting = true 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 { if needQuoting {
return fmt.Sprintf("$'%s'", b.String()) return fmt.Sprintf("$'%s'", b.String())
} }
@ -127,7 +126,7 @@ func quoteFishStr(str string) string {
needQuoting = true 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 { if needQuoting {
return fmt.Sprintf("'%s'", b.String()) return fmt.Sprintf("'%s'", b.String())
} }

View file

@ -1,44 +1,30 @@
export POSH_THEME=::CONFIG:: export POSH_THEME=::CONFIG::
export POWERLINE_COMMAND="oh-my-posh" export POWERLINE_COMMAND="oh-my-posh"
export CONDA_PROMPT_MODIFIER=false export CONDA_PROMPT_MODIFIER=false
omp_start_time=""
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
# start timer on command start # start timer on command start
PS0='$(_omp_start_timer)' PS0='${omp_start_time:0:$((omp_start_time="$(_omp_start_timer)",0))}'
# set secondary prompt # set secondary prompt
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=bash --shell-version="$BASH_VERSION")" PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=bash --shell-version="$BASH_VERSION")"
function _omp_start_timer() { function _omp_start_timer() {
::OMP:: get millis > "$TIMER_START" ::OMP:: get millis
} }
function _omp_hook() { function _omp_hook() {
local ret=$? local ret=$?
local omp_stack_count=$((${#DIRSTACK[@]} - 1)) local omp_stack_count=$((${#DIRSTACK[@]} - 1))
local omp_elapsed=-1 local omp_elapsed=-1
if [[ -f "$TIMER_START" ]]; then if [[ -n "$omp_start_time" ]]; then
omp_now=$(::OMP:: get millis) local omp_now=$(::OMP:: get millis)
omp_start_time=$(cat "$TIMER_START")
omp_elapsed=$((omp_now-omp_start_time)) omp_elapsed=$((omp_now-omp_start_time))
rm -f "$TIMER_START" omp_start_time=""
fi 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')" 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 return $ret
} }
if [ "$TERM" != "linux" ] && [ -x "$(command -v ::OMP::)" ] && ! [[ "$PROMPT_COMMAND" =~ "_omp_hook" ]]; then if [ "$TERM" != "linux" ] && [ -x "$(command -v ::OMP::)" ] && ! [[ "$PROMPT_COMMAND" =~ "_omp_hook" ]]; then
PROMPT_COMMAND="_omp_hook; $PROMPT_COMMAND" PROMPT_COMMAND="_omp_hook; $PROMPT_COMMAND"
fi fi
function _omp_runonexit() {
[[ -f $TIMER_START ]] && rm -f "$TIMER_START"
}
trap _omp_runonexit EXIT