Skip to content

Commit

Permalink
xonsh shell: fix incorrect stdout for mise shell function
Browse files Browse the repository at this point in the history
The old implementation had issues with piping the result since stdout/stderr was not acutally captured in the shell but directly printed to the terminal.
The fix here unfortunately effectivly turns off colors in mise, as mise seems to not honor CLICOLOR_FORCE et. al. environment variables as per #1836
  • Loading branch information
yggdr committed Jun 8, 2024
1 parent 87957bc commit 081ddde
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
25 changes: 19 additions & 6 deletions src/shell/snapshots/mise__shell__xonsh__tests__hook_init.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source: src/shell/xonsh.rs
expression: "xonsh.activate(exe, \" --status\".into())"
---
from os import environ
import subprocess
from xonsh.built_ins import XSH

def listen_prompt(): # Hook Events
Expand All @@ -12,12 +11,26 @@ def listen_prompt(): # Hook Events
envx = XSH.env
envx[ 'MISE_SHELL'] = 'xonsh'
environ['MISE_SHELL'] = envx.get_detyped('MISE_SHELL')
XSH.builtins.events.on_pre_prompt(listen_prompt) # Activate hook: before showing the prompt
XSH.builtins.events.on_pre_prompt(listen_prompt) # Activate hook: before showing the prompt

def _mise(args):
if args and args[0] in ('deactivate', 'shell', 'sh'):
execx(subprocess.run(['command', 'mise', *args], stdout=subprocess.PIPE).stdout.decode())
else:
subprocess.run(['command', 'mise', *args])
from contextlib import nullcontext
import subprocess

misebin = $(which -s mise)
cm = nullcontext()
if $(@(misebin) settings get color).startswith('true'):
cm = __xonsh__.env.swap(FORCE_COLOR=1, CLICOLOR_FORCE=1, CLICOLOR=1)
with cm:
# cmd = !(@(misebin) @(args))
cmd = subprocess.run([misebin, *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = cmd.stdout.decode()
err = cmd.stderr.decode()
#err = '' if cmd.errors is None else cmd.errors
if args and args[0] in ('deactivate', 'shell', 'sh') and not ('--help' in args or '-h' in args):
out = execx(out)
return out, err

XSH.aliases['mise'] = _mise

del _mise, envx, environ, XSH, listen_prompt
26 changes: 20 additions & 6 deletions src/shell/xonsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ impl Shell for Xonsh {
// meanwhile, save variables twice: in shell env + in os env
// use xonsh API instead of $.xsh to allow use inside of .py configs, which start faster due to being compiled to .pyc
// todo: subprocess instead of $() is a bit faster, but lose auto-color detection (use $FORCE_COLOR)

formatdoc! {r#"
from os import environ
import subprocess
from xonsh.built_ins import XSH
def listen_prompt(): # Hook Events
Expand All @@ -56,15 +56,29 @@ impl Shell for Xonsh {
envx = XSH.env
envx[ 'MISE_SHELL'] = 'xonsh'
environ['MISE_SHELL'] = envx.get_detyped('MISE_SHELL')
XSH.builtins.events.on_pre_prompt(listen_prompt) # Activate hook: before showing the prompt
XSH.builtins.events.on_pre_prompt(listen_prompt) # Activate hook: before showing the prompt
def _mise(args):
if args and args[0] in ('deactivate', 'shell', 'sh'):
execx(subprocess.run(['command', 'mise', *args], stdout=subprocess.PIPE).stdout.decode())
else:
subprocess.run(['command', 'mise', *args])
from contextlib import nullcontext
import subprocess
misebin = $(which -s mise)
cm = nullcontext()
if $(@(misebin) settings get color).startswith('true'):
cm = __xonsh__.env.swap(FORCE_COLOR=1, CLICOLOR_FORCE=1, CLICOLOR=1)
with cm:
# cmd = !(@(misebin) @(args))
cmd = subprocess.run([misebin, *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = cmd.stdout.decode()
err = cmd.stderr.decode()
#err = '' if cmd.errors is None else cmd.errors
if args and args[0] in ('deactivate', 'shell', 'sh') and not ('--help' in args or '-h' in args):
out = execx(out)
return out, err
XSH.aliases['mise'] = _mise
del _mise, envx, environ, XSH, listen_prompt
"#}
}

Expand Down

0 comments on commit 081ddde

Please sign in to comment.