Skip to content

Commit

Permalink
Merge pull request #1022 from carapace-sh/xonsh-fix-script
Browse files Browse the repository at this point in the history
xonsh: fix script
  • Loading branch information
rsteube authored Jun 30, 2024
2 parents 16f1d57 + 4bf5932 commit af8791a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
31 changes: 18 additions & 13 deletions example/cmd/_test/xonsh.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
from xonsh.completers.tools import *
from xonsh.completers.completer import add_one_completer
from xonsh.completers.tools import contextual_command_completer

@contextual_command_completer
def _example_completer(context):
"""carapace completer for example"""
if context.completing_command('example'):
from json import loads
from subprocess import Popen, PIPE
from xonsh.completers.tools import RichCompletion
from xonsh.completers.tools import sub_proc_get_output, RichCompletion

def fix_prefix(s):
"""quick fix for partially quoted prefix completion ('prefix',<TAB>)"""
return s.translate(str.maketrans('', '', '\'"'))

output, _ = Popen(['example', '_carapace', 'xonsh', *[a.value for a in context.args], fix_prefix(context.prefix)], stdout=PIPE, stderr=PIPE).communicate()
try:
result = {RichCompletion(c["Value"], display=c["Display"], description=c["Description"], prefix_len=len(context.raw_prefix), append_closing_quote=False, style=c["Style"]) for c in loads(output)}
except:
result = {}
if len(result) == 0:
result = {RichCompletion(context.prefix, display=context.prefix, description='', prefix_len=len(context.raw_prefix), append_closing_quote=False)}
return result
output, _ = sub_proc_get_output(
'example', '_carapace', 'xonsh', *[a.value for a in context.args], fix_prefix(context.prefix)
)
if not output:
return

for c in json.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"],
)

from xonsh.completers._aliases import _add_one_completer
_add_one_completer('example', _example_completer, 'start')
add_one_completer('example', _example_completer, 'start')

31 changes: 18 additions & 13 deletions internal/shell/xonsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,36 @@ import (
// Snippet creates the xonsh completion script.
func Snippet(cmd *cobra.Command) string {
functionName := strings.Replace(cmd.Name(), "-", "__", -1)
return fmt.Sprintf(`from xonsh.completers.tools import *
return fmt.Sprintf(`from xonsh.completers.completer import add_one_completer
from xonsh.completers.tools import contextual_command_completer
@contextual_command_completer
def _%v_completer(context):
"""carapace completer for %v"""
if context.completing_command('%v'):
from json import loads
from subprocess import Popen, PIPE
from xonsh.completers.tools import RichCompletion
from xonsh.completers.tools import sub_proc_get_output, RichCompletion
def fix_prefix(s):
"""quick fix for partially quoted prefix completion ('prefix',<TAB>)"""
return s.translate(str.maketrans('', '', '\'"'))
output, _ = Popen(['%v', '_carapace', 'xonsh', *[a.value for a in context.args], fix_prefix(context.prefix)], stdout=PIPE, stderr=PIPE).communicate()
try:
result = {RichCompletion(c["Value"], display=c["Display"], description=c["Description"], prefix_len=len(context.raw_prefix), append_closing_quote=False, style=c["Style"]) for c in loads(output)}
except:
result = {}
if len(result) == 0:
result = {RichCompletion(context.prefix, display=context.prefix, description='', prefix_len=len(context.raw_prefix), append_closing_quote=False)}
return result
output, _ = sub_proc_get_output(
'%v', '_carapace', 'xonsh', *[a.value for a in context.args], fix_prefix(context.prefix)
)
if not output:
return
for c in json.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"],
)
from xonsh.completers._aliases import _add_one_completer
_add_one_completer('%v', _%v_completer, 'start')
add_one_completer('%v', _%v_completer, 'start')
`, functionName, cmd.Name(), cmd.Name(), uid.Executable(), cmd.Name(), functionName)
}

0 comments on commit af8791a

Please sign in to comment.