Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xonsh: fix subprocess handling #2419

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, ", "))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/carapace-sh/carapace-bin
go 1.21

require (
github.com/carapace-sh/carapace v1.0.0
github.com/carapace-sh/carapace v1.1.1
github.com/carapace-sh/carapace-bridge v1.0.2
github.com/carapace-sh/carapace-shlex v1.0.1
github.com/carapace-sh/carapace-spec v1.0.0
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPn
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/carapace-sh/carapace v1.0.0 h1:O4XTxBjrmDG15PeSnBRvu5RdwA5Khd1k/yF6VD4sHHY=
github.com/carapace-sh/carapace v1.0.0/go.mod h1:4caYCCdFoKqiBFD8t8rxuxzgVXk9bnP/aSBOplTd1Gk=
github.com/carapace-sh/carapace v1.1.1 h1:OuWouCknq9Wqv0rM+7z8/lf3FfEUBdMd4MVXisP6j4w=
github.com/carapace-sh/carapace v1.1.1/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs=
github.com/carapace-sh/carapace-bridge v1.0.2 h1:q2yVrhpxjxA0p3ZcGHpjns99KE9lCrJLc3Zgaa7kMK4=
github.com/carapace-sh/carapace-bridge v1.0.2/go.mod h1:1tuz7tWpJeGMHa6Yvwlkb9QCNqxRD5VsS/4iZdK9ofQ=
github.com/carapace-sh/carapace-pflag v1.0.0 h1:uJMhl+vwEM/Eb0UdxZUuv4jo4rUAyPijkRGP5gfCuCE=
github.com/carapace-sh/carapace-pflag v1.0.0/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/carapace-sh/carapace-shlex v1.0.0/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c=
github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
github.com/carapace-sh/carapace-spec v1.0.0 h1:gq2aV/eLXmQLBLT8hqBGf8lOyyQdx54PNjE1o8ADlEw=
github.com/carapace-sh/carapace-spec v1.0.0/go.mod h1:/HdoGk/59vcD5kCTPmtxlc2QQagyZc4hOHP7Ie0MIoI=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -29,7 +27,6 @@ github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
Expand Down