Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryran/dc0ec41ae5311ac40ce2896d19bbc984 to your computer and use it in GitHub Desktop.
Save ryran/dc0ec41ae5311ac40ce2896d19bbc984 to your computer and use it in GitHub Desktop.
powerline-go for PS1, customizable-on-the-fly thanks to bash-preexec & a custom helper function
# powerline-go for PS1, customizable-on-the-fly thanks to bash-preexec & a custom helper function
# https://gist.github.com/ryran/dc0ec41ae5311ac40ce2896d19bbc984
# Source from the bottom of ~/.bashrc
# Backup PS1
[[ $PS1_ORIG ]] || PS1_ORIG=$PS1
# Download binary from github.com/justjanne/powerline-go & customize the path here
__pwr_binpath=/usr/local/bin/powerline-go
# Clone github.com/rcaloras/bash-preexec & customize the path here
# We use this to plug powerline-go into the shell PS1
__bash_preexec_path=/usr/local/lib/bash-preexec.sh
# Need to be able to grab default powerline-go modules cfg from help page
__pwr_mods_get_default() { $__pwr_binpath -h 2>&1 | awk '/^ -modules string/,/default/' | grep -F '(default "' | cut -d\" -f2; }
# And let's also get some other stuff for our own help page
__pwr_get_valid_themes() { $__pwr_binpath -h 2>&1 | awk '/^ -theme string/,/valid choices/' | grep -o '(valid choices.*'; }
__pwr_get_valid_cwdmodes() { $__pwr_binpath -h 2>&1 | awk '/^ -cwd-mode string/,/valid choices/' | grep -o '(valid choices.*'; }
__pwr_get_valid_gitmodes() { $__pwr_binpath -h 2>&1 | awk '/^ -git-mode string/,/valid choices/' | grep -o '(valid choices.*'; }
# Define the components of powerline-go PS1 precmd function (for use w/bash-preexec)
# Check the "pwr -h" help page in order to compare this with powerline-go's default
# Notable ommissions that might bother you & which you might want to remedy:
# "user" (your username) & "root" (the $ or # indicating root user)
__pwr_mods_my_default='time,ssh,user,host,shell-var,venv,cwd,perms,git,jobs,exit,duration'
# Next is the variable that's actually used for PS1, so if you want to experiment
# with modules & their ordering, you could modify this from an interactive shell
__pwr_mods=${__pwr_mods_my_default}
# Replace my custom path alias examples with any of your own, comma-delimited
__pwr_path_aliases='-path-aliases /often/used/path/to/dir=short-dir-alias,/a/b/c/d=abcd'
# Custom TZ
__pwr_tz=
# Custom theme
__pwr_theme='-theme default'
# Whether to use --newline
__pwr_newline=
# Custom cwd-mode
__pwr_cwd_mode='-cwd-mode fancy'
# Custom git-mode
__pwr_git_mode='-git-mode fancy'
# Whether to only display hostname if ssh
__pwr_hostname_if_ssh='-hostname-only-if-ssh'
# Customize as needed:
__pwr_extra_opts='-shell bash -colorize-hostname -numeric-exit-codes -shell-var-no-warn-empty -shell-var OS_CLOUDNAME'
export OS_CLOUDNAME=$OS_CLOUDNAME
# You can enable/disable all of bash-preexec at any time via shell var PREEXEC={0|1}
: ${PREEXEC:=1}
# You can enable/disable powerline-go prompt at any time via shell var PWR={0|1}
: ${PWR:=1}
precmd_powerline_prompt() {
# Shouldn't need to customize this unless you want to get rid of checking of background-jobs
local rc=$?
if [[ -x $__pwr_binpath ]] && [[ $PWR == 1 || $PWR == on ]]; then
PS1=$( [[ $__pwr_tz ]] && export TZ=$__pwr_tz; $__pwr_binpath -error $rc -duration $((SECONDS - __CMD_START)) -jobs $(jobs -p | wc -l) $__pwr_path_aliases $__pwr_theme $__pwr_newline $__pwr_cwd_mode $__pwr_git_mode $__pwr_hostname_if_ssh -modules $__pwr_mods $__pwr_extra_opts )
else
PS1=$PS1_ORIG
fi
}
# This command is used to manage the powerline-go in PS1, on-the-fly
pwr() {
local me=${FUNCNAME[0]}
local arg= x= __need_to_add
local usage=$(cat <<-EOF
Usage: $me [0 | off]
or: $me [1 | on] [r[eset] | d[efault]] [-t | -time] [-u | -user] [-H | -host]
[-c | -cwd] [-g | -git] [-e | -exit] [-r | -root] [-d | -duration]
[paths+=ALIAS | paths=[ALIAS][,ALIAS]...] [tz=[TZ]] [theme=[THEME]]
[newline=[y]] [cwd=[MODE]] [git=[MODE]] [host=[ssh]]
EOF
)
if [[ $# == 0 || $1 == -h || $1 == --help ]]; then
echo "$usage"
cat <<-EOF
Control & configure powerline-go integrated into current shell's PS1 prompt
0|off Completely disable powerline-go prompt
Alternatively: set shell var PWR=0
1|on If powerline-go prompt disabled, enable it
Alternatively: set shell var PWR=1
r|reset Reset modules to custom defaults defined in the shell var
"__pwr_mods_my_default", i.e.:
$__pwr_mods_my_default
To make permanent changes, update that var in bashrc
d|default Reset modules to powerline-go's built-in defaults, extracted
from the help page, i.e.:
$( __pwr_mods_get_default )
Use the following to disable specific modules in current shell.
(Can be used in concert with "on", "reset", and "default".)
-t|-time Disable time module (don't show the current time)
-u|-user Disable user module (don't show username)
-H|-host Disable host module (don't show hostname)
-c|-cwd Disable cwd module (don't show CWD)
-g|-git Disable git module (don't show git info when in a git proj)
-e|-exit Disable exit module (don't show error codes)
-r|-root Disable root module (don't show "$" or "#")
-d|-duration Disable duration module (don't show elapsed time)
Use the following args to tweak other bits of the prompt in current shell.
To make changes permanent, update the corresponding shell cfg variable
(listed underneath each option) in bashrc. To reset any of these to
powerline-go's built-in default, use "--OPTION=", i.e., equals sign w/ no arg.
paths+=ALIAS
paths=[ALIAS][,ALIAS]...
Add new path alias in the form /DIR=NAME or define a comma-
delimited list of path aliases or get rid of them all
(current setting: "${__pwr_path_aliases#-path-aliases}")
\$__pwr_path_aliases
tz=[TZ] Set timezone used by time module
\$__pwr_tz
theme=[THEME] Modify theme
$( __pwr_get_valid_themes )
\$__pwr_theme
newline=[y] Show the prompt on a new line
\$__pwr_newline
cwd=[MODE] Change how cwd module displays current dir
$( __pwr_get_valid_cwdmodes )
\$__pwr_cwd_mode
git=MODE Change how git module displays status
$( __pwr_get_valid_gitmodes )
\$__pwr_git_mode
host=[ssh] Modify host module to only show hostname if ssh
Use "--host=" to reset to always showing hostname
\$__pwr_hostname_if_ssh
$me originally created by rsaw, aka ryran, as "pwr"
https://gist.github.com/ryran/dc0ec41ae5311ac40ce2896d19bbc984
EOF
return 1
fi
# Process off/on/reset/default before any other args
for arg in $@; do case $arg in
0|off)
PWR=0; return ;;
1|on)
# only reset if this is completely unset
[[ $__pwr_mods ]] || __pwr_mods=$__pwr_mods_my_default
PWR=1 ;;
r|reset)
__pwr_mods=$__pwr_mods_my_default ;;
d|default)
__pwr_mods=$( __pwr_mods_get_default ) ;;
*)
continue
esac; done
until [[ $# == 0 ]]; do
case $1 in
-t|-time) x=${__pwr_mods/time} x=${x#,} x=${x%,} __pwr_mods=${x/,,/,} ;;
-u|-user) x=${__pwr_mods/user} x=${x#,} x=${x%,} __pwr_mods=${x/,,/,} ;;
-H|-host) x=${__pwr_mods/host} x=${x#,} x=${x%,} __pwr_mods=${x/,,/,} ;;
-c|-cwd) x=${__pwr_mods/cwd} x=${x#,} x=${x%,} __pwr_mods=${x/,,/,} ;;
-g|-git) x=${__pwr_mods/git} x=${x#,} x=${x%,} __pwr_mods=${x/,,/,} ;;
-e|-exit) x=${__pwr_mods/exit} x=${x#,} x=${x%,} __pwr_mods=${x/,,/,} ;;
-r|-root) x=${__pwr_mods/root} x=${x#,} x=${x%,} __pwr_mods=${x/,,/,} ;;
-d|-duration) x=${__pwr_mods/duration} x=${x#,} x=${x%,} __pwr_mods=${x/,,/,} ;;
1|on|r|reset|d|default)
# Already processed above
: ;;
paths+=*)
arg=${1#paths+=}
[[ $__pwr_path_aliases ]] && __pwr_path_aliases+=,$arg || __pwr_path_aliases="-path-aliases $arg" ;;
paths=*)
arg=${1#paths=}
[[ $arg ]] && __pwr_path_aliases="-path-aliases $arg" || __pwr_path_aliases= ;;
tz=*) __pwr_tz=${1#tz=} ;;
theme=*) arg=${1#theme=}; [[ $arg ]] && __pwr_theme="-theme $arg" || __pwr_theme= ;;
newline=*) [[ ${1#newline=} == y ]] && __pwr_newline=--newline || __pwr_newline= ;;
cwd=*) arg=${1#cwd=}; [[ $arg ]] && __pwr_cwd_mode="-cwd-mode $arg" || __pwr_cwd_mode= ;;
git=*) arg=${1#git=}; [[ $arg ]] && __pwr_git_mode="-git-mode $arg" || __pwr_git_mode= ;;
host=*) [[ ${1#host=} == ssh ]] && __pwr_hostname_if_ssh=-hostname-only-if-ssh || __pwr_hostname_if_ssh= ;;
*)
echo -e "ERROR: unsupported arg \"$1\"\n\n$usage"
return 2
esac
shift
done
if [[ $PWR == 1 || $PWR == on ]] && [[ $TERM != linux ]]; then
__need_to_add=1
for func in ${precmd_functions[@]}; do
[[ $func == precmd_powerline_prompt ]] && __need_to_add=
done
[[ $__need_to_add ]] && precmd_functions+=( precmd_powerline_prompt )
fi
return 0
}
# Enable or disable bash-preexec based on $PREEXEC
if [[ $PREEXEC == 1 ]]; then
. $__bash_preexec_path
# Add a simple cmd-timer that takes advantage of bash's $SECONDS var
preexec_start_timer() { __CMD_START=$SECONDS; }
preexec_functions+=( preexec_start_timer )
# Enable or disable powerline-go based on $PWR
[[ $PWR == 1 || $PWR == on ]] && pwr on || pwr off
else
pwr off
unset -f preexec precmd preexec_functions precmd_functions
fi
@ryran
Copy link
Author

ryran commented Mar 8, 2021

Screenshot showing that in action:
pwr

Help page for the pwr function:

Usage: pwr [0 | off]
  or:  pwr [1 | on] [r[eset] | d[efault]] [-d | -duration] [-t | -time]
           [-u | -user] [-c | -cwd] [-g | -git] [-e | -exit] [-r | -root]
           [paths+=ALIAS | paths=[ALIAS][,ALIAS]...] [tz=[TZ]] [theme=[THEME]]
           [newline=[y]] [cwd=[MODE]] [git=[MODE]] [host=[ssh]]

Control & configure powerline-go integrated into current shell's PS1 prompt

   0|off    Completely disable powerline-go prompt
            Alternatively: set shell var PWR=0

   1|on     If powerline-go prompt disabled, enable it
            Alternatively: set shell var PWR=1

   r|reset      Reset modules to custom defaults defined in the shell var
                "__pwr_mods_my_default", i.e.:
                    time,venv,host,ssh,cwd,perms,git,jobs,exit,duration
                To make permanent changes, update that var in bashrc

   d|default    Reset modules to powerline-go's built-in defaults, extracted
                from the help page, i.e.:
                    venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root

Use the following to disable specific modules in current shell.
(Can be used in concert with "on", "reset", and "default".)

   -t|-time        Disable time module (don't show the current time)
   -u|-user        Disable user module (don't show username)
   -c|-cwd         Disable cwd module (don't show CWD)
   -g|-git         Disable git module (don't show git info when in a git proj)
   -e|-exit        Disable exit module (don't show error codes)
   -r|-root        Disable root module (don't show "$" or "#")
   -d|-duration    Disable duration module (don't show elapsed time)

Use the following args to tweak other bits of the prompt in current shell.
To make changes permanent, update the corresponding shell cfg variable
(listed underneath each option) in bashrc. To reset any of these to
powerline-go's built-in default, use "--OPTION=", i.e., equals sign w/ no arg.

   paths+=ALIAS       
   paths=[ALIAS][,ALIAS]...
                   Add new path alias in the form /DIR=NAME or define a comma-
                   delimited list of path aliases or get rid of them all
                   (current setting: " /g/x/git=x")
                   $__pwr_path_aliases
   
   tz=[TZ]         Set timezone used by time module
                   $__pwr_tz

   theme=[THEME]   Modify theme
                   (valid choices: default, low-contrast)
                   $__pwr_theme
   
   newline=[y]     Show the prompt on a new line
                   $__pwr_newline
   
   cwd=[MODE]      Change how cwd module displays current dir
                   (valid choices: fancy, semifancy, plain, dironly)
                   $__pwr_cwd_mode
   
   git=MODE        Change how git module displays status
                   (valid choices: fancy, simple)
                   $__pwr_git_mode
   
   host=[ssh]      Modify host module to only show hostname if ssh
                   Use "--host=" to reset to always showing hostname
                   $__pwr_hostname_if_ssh

pwr originally created by rsaw, aka ryran, as "pwr"
https://gist.github.com/ryran/dc0ec41ae5311ac40ce2896d19bbc984

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment