Page MenuHomeVyOS Platform

Make console prompts customizable
Open, LowPublic

Description

As of now, console prompts are hardcoded: it's user@host$ in operational mode and user@host# in configuration mode. That's how it works in classic network OSes, but UNIX shells (including bash, that we use) offers a lot more flexibility that some people may want and have no way to use now.

We could add options to allow people to configure their own prompts if they want different formats, colors, etc. For example:

set system option console-prompt mode operational <format string>
set system option console-prompt mode configuration <format string>

Details

Version
-
Is it a breaking change?
Perfectly compatible
Issue type
Feature (new functionality)
Forum thread
https://forum.vyos.io/t/how-to-customize-ps1/16472/9

Event Timeline

Here's what I did to get a nice color prompt containing time, hostname, build version, working directory and when in config mode, a clear bright yellow "✎ edit »" line. with the actual path you're editing in. It then looks like this:

14:56 vyos@gw 1.4.2 /home/vyos
✎ edit interfaces bonding bond0 vif 90 » show
 address 192.168.90.1/24
 address 2001:xxxx:x:xxxx::1/64
 description Foobar
 mtu 1500

It relies on:

  • info in /etc/os-release which will be pretty predictable
  • on an API call through cli-shell-api to get the current config mode path. Not sure how futureproof that is.
  • the SHLVL variable that contains a 1 when in op mode and a 2 when in config mode. Also, not sure how futureproof that is.

This method has been working fine since forever. While my method works, it feels hackish. Maybe this'll serve as inspiration to someone to make a good PR out of it that is solid and properly integrated.

Firstly, I put these two files in my /config:

/config/my-env.sh:

# prompt
function prompt_command {
    source /config/my-vyos-prompt.sh
}

export PROMPT_COMMAND=prompt_command

# You may put other stuff here, like aliases, your favorite tetmode EDITOR, stty commands to setup serial console behaviour etc...
# Because it's in this file, it'll survive upgrades if during upgrade you choose to copy over the working config.

/config/my-vyos-prompt.sh:

# Custom VyOS shell prompt
#
# - Operational shell level => SHLVL==1
# - Configure shell level =>   SHLVL==2
#
# Color definitions, add \[\] around ANSI color codes to prevent
# weird prompt behaviour, see: http://stackoverflow.com/questions/19092488/custom-bash-prompt-is-overwriting-itself

# Reset
Color_Off='\[\e[0m\]'       # Text Reset

# Colors used
Purple='\[\e[0;35m\]'       # Purple
IRed='\[\e[0;91m\]'         # Red
IGreen='\[\e[0;92m\]'       # Green
IYellow='\[\e[0;93m\]'      # Yellow
ICyan='\[\e[0;96m\]'        # Cyan
IWhite='\[\e[0;97m\]'       # White

# Build color prompt
PSC="\n${IGreen}\A "

# Add username and hostname
# If root, add a ★ to emphasize the powers that come with it
if [ "$(id -u)" -eq 0 ]
then
    PSC="${PSC}${IRed}"$'\xe2\x98\x85'
    if [ "$(id -un)" != "$(logname)" ]; then
        INSIDESU='(su)'
    fi
else
    # Switch to cyan
    PSC="${PSC}${ICyan}"
fi

PSC="${PSC}\u$INSIDESU@\h\e"

# Add version info
RELEASE="$(sed -nE 's/^\s*VERSION_ID=\"(.*)\"$/\1/p' /etc/os-release)"
PSC="${PSC}${Purple} ${RELEASE}${Color_Off}"

# Add current (full) path
PSC="${PSC}${IWhite} ${PWD} "

# If in VyOS config mode, add config path
if [[ $SHLVL == 2 ]]
then
    VYOSPROMPT="$(cli-shell-api getEditLevelStr)"
    if [[ ${VYOSPROMPT} == "" ]]
    then
        PSC="${PSC}\n${IYellow}"$'\xe2\x9c\x8e'" edit ${Color_Off}"
    else
        PSC="${PSC}\n${IYellow}"$'\xe2\x9c\x8e'" edit ${VYOSPROMPT} ${Color_Off}"
    fi
else
    # Add last return code
    PSC="${PSC}\`RC=\$?;"
    PSC="${PSC}echo -n '\n';"
    PSC="${PSC}if [ \$RC -eq 0 ]; "
    PSC="${PSC}then echo -n '${IGreen}';"
    PSC="${PSC}else echo -n '${IRed}';"
    PSC="${PSC}fi;"
    PSC="${PSC}echo -n \$RC; echo -n '${Color_Off}';\`"
fi

# Add » as a prompt sign
PSC="${PSC}"$'\xc2\xbb'" "

# Set prompt
PS1=${PSC}

# Set PS2, the continuation prompt (default is >, we want it to be »)
PS2=" "$'\xc2\xbb'" "

# Variables no longer needed
unset PSC INSIDESU RELEASE Color_Off Purple IRed IGreen IYellow ICyan IWhite

Then, in your ~/.bashrc file, put this as the last line:

source /config/my-env.sh