refactor: convert prompt_ret_code to be a py file (#4032)

to take advantage of flake/coverage
This commit is contained in:
Noorhteen Raja NJ 2021-01-13 00:05:38 +05:30 committed by GitHub
parent b163e6f7cd
commit 80aca856e4
Failed to generate hash of commit
3 changed files with 46 additions and 36 deletions

View file

@ -44,9 +44,9 @@ ignore =
D409, # Section underline should match the length of its name D409, # Section underline should match the length of its name
D411, # Missing blank line before section D411, # Missing blank line before section
D407, # Missing dashed underline after section D407, # Missing dashed underline after section
E122, E122, # continuation line missing indentation or outdented
E203, # whitespace before ':' E203, # whitespace before ':'
E402, E402, # module level import not at top of file
W503, # line break before binary operators is a good thing W503, # line break before binary operators is a good thing
E731, # accept lambda assigned to a variable E731, # accept lambda assigned to a variable
# also acceptable in Xonsh project: reference to global names defined at runtime by black magic # also acceptable in Xonsh project: reference to global names defined at runtime by black magic

View file

@ -0,0 +1,44 @@
from xonsh.tools import ON_WINDOWS as _ON_WINDOWS
import builtins
def _ret_code_color():
if builtins.__xonsh__.history.rtns:
color = "blue" if builtins.__xonsh__.history.rtns[-1] == 0 else "red"
else:
color = "blue"
if _ON_WINDOWS:
if color == "blue":
return "{BOLD_INTENSE_CYAN}"
elif color == "red":
return "{BOLD_INTENSE_RED}"
else:
if color == "blue":
return "{BOLD_BLUE}"
elif color == "red":
return "{BOLD_RED}"
def _ret_code():
if builtins.__xonsh__.history.rtns:
return_code = builtins.__xonsh__.history.rtns[-1]
if return_code != 0:
return "[{}]".format(return_code)
return None
def _update():
env = builtins.__xonsh__.env
env["PROMPT"] = env["PROMPT"].replace(
"{prompt_end}{RESET}", "{ret_code_color}{ret_code}{prompt_end}{RESET}"
)
flds = env["PROMPT_FIELDS"]
flds["ret_code_color"] = _ret_code_color
flds["ret_code"] = _ret_code
# xontrib loads updates context
_update()

View file

@ -1,34 +0,0 @@
from xonsh.tools import ON_WINDOWS as _ON_WINDOWS
def _ret_code_color():
if __xonsh__.history.rtns:
color = 'blue' if __xonsh__.history.rtns[-1] == 0 else 'red'
else:
color = 'blue'
if _ON_WINDOWS:
if color == 'blue':
return '{BOLD_INTENSE_CYAN}'
elif color == 'red':
return '{BOLD_INTENSE_RED}'
else:
if color == 'blue':
return '{BOLD_BLUE}'
elif color == 'red':
return '{BOLD_RED}'
def _ret_code():
if __xonsh__.history.rtns:
return_code = __xonsh__.history.rtns[-1]
if return_code != 0:
return '[{}]'.format(return_code)
return None
$PROMPT = $PROMPT.replace('{prompt_end}{RESET}',
'{ret_code_color}{ret_code}{prompt_end}{RESET}')
$PROMPT_FIELDS['ret_code_color'] = _ret_code_color
$PROMPT_FIELDS['ret_code'] = _ret_code