Skip to content

Commit

Permalink
fix(elvish): improve robustness of initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis-yeung authored and JanDeDobbeleer committed Aug 22, 2024
1 parent 7486558 commit 8f6aa12
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
15 changes: 9 additions & 6 deletions src/shell/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ func Init(env runtime.Environment, feats Features) string {
}

var command, config string

switch shell {
case PWSH, PWSH5:
command = "(@(& %s init %s --config=%s --print%s) -join \"`n\") | Invoke-Expression"
config = quotePwshStr(env.Flags().Config)
executable = quotePwshStr(executable)
case ELVISH:
command = "eval (%s init %s --config=%s --print%s | slurp)"
config = env.Flags().Config
command = "eval ((external %s) init %s --config=%s --print%s | slurp)"
}

config = quotePwshOrElvishStr(env.Flags().Config)
executable = quotePwshOrElvishStr(executable)

return fmt.Sprintf(command, executable, shell, config, additionalParams)
case ZSH, BASH, FISH, CMD, TCSH, XONSH:
return PrintInit(env, feats, nil)
Expand All @@ -88,8 +89,8 @@ func PrintInit(env runtime.Environment, features Features, startTime *time.Time)

switch shell {
case PWSH, PWSH5:
executable = quotePwshStr(executable)
configFile = quotePwshStr(configFile)
executable = quotePwshOrElvishStr(executable)
configFile = quotePwshOrElvishStr(configFile)
script = pwshInit
case ZSH:
executable = quotePosixStr(executable)
Expand All @@ -116,6 +117,8 @@ func PrintInit(env runtime.Environment, features Features, startTime *time.Time)
configFile = quotePosixStr(configFile)
script = tcshInit
case ELVISH:
executable = quotePwshOrElvishStr(executable)
configFile = quotePwshOrElvishStr(configFile)
script = elvishInit
case XONSH:
script = xonshInit
Expand Down
4 changes: 2 additions & 2 deletions src/shell/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestQuotePwshStr(t *testing.T) {
func TestQuotePwshOrElvishStr(t *testing.T) {
tests := []struct {
str string
expected string
Expand All @@ -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'`},
}
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))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/shell/pwsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "'", "''"))
}
14 changes: 7 additions & 7 deletions src/shell/scripts/omp.elv
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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 POWERLINE_COMMAND 'oh-my-posh'

var error-code = 0
var _omp_error_code = 0
var _omp_executable = ::OMP::

fn posh-after-command-hook {|m|
var error = $m[error]
if (is $error $nil) {
set error-code = 0
set _omp_error_code = 0
} else {
try {
set error-code = $error[reason][exit-status]
set _omp_error_code = $error[reason][exit-status]
} catch {
# built-in commands don't have a status code.
set error-code = 1
set _omp_error_code = 1
}
}
}
Expand All @@ -24,10 +24,10 @@ set edit:after-command = [ $@edit:after-command $posh-after-command-hook~ ]

set edit:prompt = {
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 = {
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
}

0 comments on commit 8f6aa12

Please sign in to comment.