fix(elvish): improve robustness of initialization
Some checks are pending
Code QL / code-ql (push) Waiting to run
Release / changelog (push) Waiting to run
Release / artifacts (push) Blocked by required conditions

This commit is contained in:
L. Yeung 2024-08-23 01:31:24 +08:00 committed by Jan De Dobbeleer
parent 7486558436
commit 8f6aa125a5
4 changed files with 19 additions and 16 deletions

View file

@ -54,16 +54,17 @@ func Init(env runtime.Environment, feats Features) string {
} }
var command, config string var command, config string
switch shell { switch shell {
case PWSH, PWSH5: case PWSH, PWSH5:
command = "(@(& %s init %s --config=%s --print%s) -join \"`n\") | Invoke-Expression" command = "(@(& %s init %s --config=%s --print%s) -join \"`n\") | Invoke-Expression"
config = quotePwshStr(env.Flags().Config)
executable = quotePwshStr(executable)
case ELVISH: case ELVISH:
command = "eval (%s init %s --config=%s --print%s | slurp)" command = "eval ((external %s) init %s --config=%s --print%s | slurp)"
config = env.Flags().Config
} }
config = quotePwshOrElvishStr(env.Flags().Config)
executable = quotePwshOrElvishStr(executable)
return fmt.Sprintf(command, executable, shell, config, additionalParams) return fmt.Sprintf(command, executable, shell, config, additionalParams)
case ZSH, BASH, FISH, CMD, TCSH, XONSH: case ZSH, BASH, FISH, CMD, TCSH, XONSH:
return PrintInit(env, feats, nil) return PrintInit(env, feats, nil)
@ -88,8 +89,8 @@ func PrintInit(env runtime.Environment, features Features, startTime *time.Time)
switch shell { switch shell {
case PWSH, PWSH5: case PWSH, PWSH5:
executable = quotePwshStr(executable) executable = quotePwshOrElvishStr(executable)
configFile = quotePwshStr(configFile) configFile = quotePwshOrElvishStr(configFile)
script = pwshInit script = pwshInit
case ZSH: case ZSH:
executable = quotePosixStr(executable) executable = quotePosixStr(executable)
@ -116,6 +117,8 @@ func PrintInit(env runtime.Environment, features Features, startTime *time.Time)
configFile = quotePosixStr(configFile) configFile = quotePosixStr(configFile)
script = tcshInit script = tcshInit
case ELVISH: case ELVISH:
executable = quotePwshOrElvishStr(executable)
configFile = quotePwshOrElvishStr(configFile)
script = elvishInit script = elvishInit
case XONSH: case XONSH:
script = xonshInit script = xonshInit

View file

@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestQuotePwshStr(t *testing.T) { func TestQuotePwshOrElvishStr(t *testing.T) {
tests := []struct { tests := []struct {
str string str string
expected string expected string
@ -19,7 +19,7 @@ func TestQuotePwshStr(t *testing.T) {
{str: `C:\tmp\omp's dir\oh-my-posh.exe`, expected: `'C:\tmp\omp''s dir\oh-my-posh.exe'`}, {str: `C:\tmp\omp's dir\oh-my-posh.exe`, expected: `'C:\tmp\omp''s dir\oh-my-posh.exe'`},
} }
for _, tc := range tests { for _, tc := range tests {
assert.Equal(t, tc.expected, quotePwshStr(tc.str), fmt.Sprintf("quotePwshStr: %s", tc.str)) assert.Equal(t, tc.expected, quotePwshOrElvishStr(tc.str), fmt.Sprintf("quotePwshStr: %s", tc.str))
} }
} }

View file

@ -37,6 +37,6 @@ func (f Feature) Pwsh() Code {
} }
} }
func quotePwshStr(str string) string { func quotePwshOrElvishStr(str string) string {
return fmt.Sprintf("'%s'", strings.ReplaceAll(str, "'", "''")) return fmt.Sprintf("'%s'", strings.ReplaceAll(str, "'", "''"))
} }

View file

@ -1,21 +1,21 @@
set-env POSH_PID (to-string (randint 10000000000000 10000000000000000)) set-env POSH_PID (to-string (randint 10000000000000 10000000000000000))
set-env POSH_THEME '::CONFIG::' set-env POSH_THEME ::CONFIG::
set-env POSH_SHELL_VERSION (elvish --version) set-env POSH_SHELL_VERSION (elvish --version)
set-env POWERLINE_COMMAND 'oh-my-posh' set-env POWERLINE_COMMAND 'oh-my-posh'
var error-code = 0 var _omp_error_code = 0
var _omp_executable = ::OMP:: var _omp_executable = ::OMP::
fn posh-after-command-hook {|m| fn posh-after-command-hook {|m|
var error = $m[error] var error = $m[error]
if (is $error $nil) { if (is $error $nil) {
set error-code = 0 set _omp_error_code = 0
} else { } else {
try { try {
set error-code = $error[reason][exit-status] set _omp_error_code = $error[reason][exit-status]
} catch { } catch {
# built-in commands don't have a status code. # built-in commands don't have a status code.
set error-code = 1 set _omp_error_code = 1
} }
} }
} }
@ -24,10 +24,10 @@ set edit:after-command = [ $@edit:after-command $posh-after-command-hook~ ]
set edit:prompt = { set edit:prompt = {
var cmd-duration = (printf "%.0f" (* $edit:command-duration 1000)) var cmd-duration = (printf "%.0f" (* $edit:command-duration 1000))
$_omp_executable print primary --shell=elvish --execution-time=$cmd-duration --status=$error-code --pwd=$pwd --shell-version=$E:POSH_SHELL_VERSION (external $_omp_executable) print primary --shell=elvish --execution-time=$cmd-duration --status=$_omp_error_code --pwd=$pwd --shell-version=$E:POSH_SHELL_VERSION
} }
set edit:rprompt = { set edit:rprompt = {
var cmd-duration = (printf "%.0f" (* $edit:command-duration 1000)) var cmd-duration = (printf "%.0f" (* $edit:command-duration 1000))
$_omp_executable print right --shell=elvish --execution-time=$cmd-duration --status=$error-code --pwd=$pwd --shell-version=$E:POSH_SHELL_VERSION (external $_omp_executable) print right --shell=elvish --execution-time=$cmd-duration --status=$_omp_error_code --pwd=$pwd --shell-version=$E:POSH_SHELL_VERSION
} }