From 83c1af63b41ec018b295f8c67ada863910cc2fce Mon Sep 17 00:00:00 2001 From: "L. Yeung" Date: Tue, 9 Aug 2022 16:16:33 +0800 Subject: [PATCH] refactor(bash): simplify timer --- src/engine/image_test.go | 2 +- src/environment/battery/battery_linux.go | 2 +- src/font/install_unix.go | 2 +- src/shell/init.go | 7 +++---- src/shell/scripts/omp.bash | 26 ++++++------------------ 5 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/engine/image_test.go b/src/engine/image_test.go index be4333c5..9f970dfe 100644 --- a/src/engine/image_test.go +++ b/src/engine/image_test.go @@ -1,7 +1,7 @@ package engine import ( - "io/ioutil" //nolint: staticcheck + "io/ioutil" //nolint:staticcheck,nolintlint "oh-my-posh/color" "os" "path/filepath" diff --git a/src/environment/battery/battery_linux.go b/src/environment/battery/battery_linux.go index be0c13c1..77f8f3b9 100644 --- a/src/environment/battery/battery_linux.go +++ b/src/environment/battery/battery_linux.go @@ -24,7 +24,7 @@ package battery import ( "errors" "fmt" - "io/ioutil" + "io/ioutil" //nolint:staticcheck,nolintlint "os" "path/filepath" "strconv" diff --git a/src/font/install_unix.go b/src/font/install_unix.go index 7add71ea..6678d6a1 100644 --- a/src/font/install_unix.go +++ b/src/font/install_unix.go @@ -5,7 +5,7 @@ package font import ( - "io/ioutil" + "io/ioutil" //nolint:staticcheck,nolintlint "os" "path" "strings" diff --git a/src/shell/init.go b/src/shell/init.go index eee16943..a175ebd1 100644 --- a/src/shell/init.go +++ b/src/shell/init.go @@ -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()) } diff --git a/src/shell/scripts/omp.bash b/src/shell/scripts/omp.bash index 645610b6..942deaee 100644 --- a/src/shell/scripts/omp.bash +++ b/src/shell/scripts/omp.bash @@ -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