anki-code / xontrib-prompt-bar

The bar prompt for xonsh shell with customizable sections and Starship support.
BSD 2-Clause "Simplified" License
49 stars 2 forks source link
cli cmd console iterm2 iterm2-theme prompt python shell shell-theme terminal windows-terminal xonsh xonsh-prompt xontrib

The bar prompt for the xonsh shell with customizable sections.

[Demo] Screenshot made in Konsole with $XONSH_COLOR_STYLE = "paraiso-dark".

If you like the idea of bar theme click ⭐ on the repo and tweet.

Features:

Install

xpip install -U xontrib-prompt-bar
echo 'xontrib load prompt_bar' >> ~/.xonshrc
# Reload xonsh

Default theme

$XONTRIB_PROMPT_BAR_THEME = {
    'left': '{hostname}{user}{cwd_abs#accent}',
    'right': '{hist_status#section}{curr_branch#section}{env_name#strip_brackets#section}{date_time_tz}',
    'bar_bg': '{BACKGROUND_#323232}',
    'bar_fg': '{#AAA}',
    'section_bg': '{BACKGROUND_#444}',
    'section_fg': '{#CCC}',
    'accent_fg': '{BOLD_#DDD}',
}
xontrib load prompt_bar

Use cases

Customize the fields

Supported fields:

To customize the appearance of the fields on the bar you can use wrappers and chaining them:

$XONTRIB_PROMPT_BAR_RIGHT = '{hostname#accent#section} {date_time_tz#nocolorx}'
xontrib load prompt_bar

Builtin wrappers:

To create your own fields and wrapper see the section below.

Add custom fields and wrappers

How to add two new fields called my_left_custom and my_right_custom and one new wrapper called brackets.

$PROMPT_FIELDS['my_left_custom'] = 'Hello left!'
$PROMPT_FIELDS['my_right_custom'] = lambda: '>'*3 + ' {YELLOW}Hello right!'

$XONTRIB_PROMPT_BAR_WRAPPERS = {
    'brackets': lambda v: f'[{v}]'
}

$XONTRIB_PROMPT_BAR_LEFT = '{hostname}{user}{cwd_abs#accent}{my_left_custom#brackets}'
$XONTRIB_PROMPT_BAR_RIGHT = '{my_right_custom#section}{env_name#strip_brackets#section}{date_time_tz}'

xontrib load prompt_bar

Result:

[Demo custom fields]

Themes and colors

To change the bar colors there is setting the theme:

$XONTRIB_PROMPT_BAR_THEME = {
    'left':       '{hostname}{user}{cwd_abs#accent}',
    'right':      '{curr_branch#section}{env_name#strip_brackets#section}{date_time_tz}',
    'bar_bg':     '{BACKGROUND_#FF0000}',
    'bar_fg':     '{#AAA}',
    'section_bg': '{BACKGROUND_#444}',
    'section_fg': '{#CCC}',
    'accent_fg':  '{BOLD_#DDD}',
}
xontrib load prompt_bar

To choose the colors there is HTML Color Picker.

Using Starship cross-shell prompt to rendering right sections

"Barship" using xontrib-prompt-starship:

# First of all create a starship config to return sections in one line
mkdir -p ~/.config && echo @("""
[character]
success_symbol = ""
error_symbol = ""
[status]
symbol = ""
""".strip()) > ~/.config/starship_xonsh_right.toml

# Then just add the starship bar to right
$XONTRIB_PROMPT_STARSHIP_RIGHT_CONFIG = "~/.config/starship_xonsh_right.toml"
$XONTRIB_PROMPT_STARSHIP_REPLACE_PROMPT = False
$XONTRIB_PROMPT_BAR_RIGHT = '{starship_right#noesc#nonl#strip}'
xontrib load prompt_starship prompt_bar

Result:

Prompt bar with starship sections.

Using xontrib-cmd-durations

The xontrib-cmd-durations is to send notification once long-running command is finished and also show the execution time. Usage example:

# Add `{long_cmd_duration}` section
$XONTRIB_PROMPT_BAR_RIGHT = '{long_cmd_duration}{curr_branch#section}{screens#section}{env_name#strip_brackets#section}{date_time_tz}'
xontrib load cmd_done

Hide return code in special cases

Sometimes it's needed to hide the return code. In this case you can use -8888 return code i.e.:

aliases['cdls'] = "cd @($arg0) && @(lambda: -8888 if len(g`./*`) > 100 else 0) && ls --group-directories-first -A --color"
cdls /
# return code is 0 and `ls` command was executed and return code is not shown
cdls /usr/sbin
# return code is -8888 and `ls` command was NOT executed and return code is not shown

Additional links

Environment variables

Known issues

Spaces in the copied and pasted command line

Please update prompt_toolkit to 3.0.7+ version via python -m pip install -U prompt_toolkit.

Credits