-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1022 from carapace-sh/xonsh-fix-script
xonsh: fix script
- Loading branch information
Showing
2 changed files
with
36 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters