Skip to content

Commit

Permalink
Merge pull request #2419 from carapace-sh/xonsh-fix-script
Browse files Browse the repository at this point in the history
xonsh: fix subprocess handling
  • Loading branch information
rsteube authored Jun 30, 2024
2 parents dad238e + e1c39a4 commit 31ed25e
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions cmd/carapace/cmd/lazyinit/xonsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,49 @@ import (
"strings"
)

// Xonsh creates a generic completer (not lazy).
// Name overlaps with specific `carapace` completer but that shouldn't be an issue.
func Xonsh(completers []string) string {
snippet := `from xonsh.completers._aliases import _add_one_completer
from xonsh.completers.tools import *
snippet := `from xonsh.completers.completer import add_one_completer
from xonsh.completers.tools import contextual_command_completer
import os
%v%v
@contextual_completer
def _carapace_lazy(context):
"""carapace lazy"""
if (context.command and
context.command.arg_index > 0 and
context.command.args[0].value in [%v]):
exec(compile(subprocess.run(['carapace', context.command.args[0].value, 'xonsh'], stdout=subprocess.PIPE).stdout.decode('utf-8'), "", "exec"))
return XSH.completers[context.command.args[0].value](context)
@contextual_command_completer
def _carapace_completer(context):
"""carapace completer"""
from json import loads
from xonsh.completers.tools import sub_proc_get_output, RichCompletion
if context.command not in [%v]:
return
def fix_prefix(s):
"""quick fix for partially quoted prefix completion ('prefix',<TAB>)"""
return s.translate(str.maketrans('', '', '\'"'))
output, _ = sub_proc_get_output(
'carapace', context.command, 'xonsh', *[a.value for a in context.args], fix_prefix(context.prefix)
)
if not output:
return
for c in loads(output):
yield RichCompletion(
c["Value"],
display=c["Display"],
description=c["Description"],
prefix_len=len(context.raw_prefix),
append_closing_quote=False,
style=c["Style"],
)
add_one_completer('carapace', _carapace_completer, 'start')
`
complete := make([]string, len(completers))
for index, completer := range completers {
complete[index] = fmt.Sprintf(`'%v'`, completer)
}
snippet += `_add_one_completer('carapace_lazy', _carapace_lazy, 'start')`
return fmt.Sprintf(snippet, pathSnippet("xonsh"), envSnippet("xonsh"), strings.Join(complete, ", "))
}

0 comments on commit 31ed25e

Please sign in to comment.