Skip to content

Instantly share code, notes, and snippets.

@meoow
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoow/4cb500228c8467de39a5 to your computer and use it in GitHub Desktop.
Save meoow/4cb500228c8467de39a5 to your computer and use it in GitHub Desktop.
Custom PS1 prompt
if ((psi==1));then
PS1='$(
exitstatus=$?
if ((exitstatus==0));then
exitstatus=''
fi
predots=""
postdots=""
extrachars=6
if [[ "$PWD" =~ ^$HOME ]];then
pwd=${PWD/$HOME/\~}
else
pwd=$PWD
fi
if [[ -z $COLUMNS ]];then
if type -P tput >/dev/null;then
COLUMNS=$(tput cols)
else
COLUMNS=80
fi
fi
(( PATHLEN=COLUMNS-${#USER}-extrachars-${#exitstatus} ))
if (( PATHLEN>=1 )) && (( ${#pwd}>PATHLEN ));then
DIR=${pwd/*\/}
if (( ${#DIR}>PATHLEN ));then
postdots=...
pwd="${DIR:0:$(( PATHLEN-3 ))}"
else
predots=...
pwd="${pwd:$(( ${#pwd}-PATHLEN+3 ))}"
fi
fi
if ((EUID>0));then
promptchar=$
else
promptchar=#
fi
printf '\''\[\e[1;31m\]%s\[\e[0m\]'\'' $exitstatus
printf '\''\[\e[0m\][ \[\e[1m\]%s\[\e[0m\]: '\'' "$USER"
printf '\''\[\e[0;31m\]%s\[\e[0m\]'\'' "$predots"
printf %s "$pwd"
printf '\''\[\e[0;31m\]%s\[\e[0m\]'\'' "$postdots"
printf '\'' ]\n\[\e[1m\]%s\[\e[0m\] '\'' $promptchar
)'
elif ((psi==2));then
# style:
# meow: .../meoow/nodefinder/example1 $
# meow: aaaaaaaaaaaaaaaaaaaaaaaaaa... $
PS1='$(
exitstatus=$?
if ((exitstatus==0));then
exitstatus=''
fi
predots=""
postdots=""
extrachars=7
if [[ "$PWD" =~ ^$HOME ]];then
pwd=${PWD/$HOME/\~}
else
pwd=$PWD
fi
if [[ -z $COLUMNS ]];then
if type -P tput >/dev/null;then
COLUMNS=$(tput cols)
else
COLUMNS=80
fi
fi
(( PATHLEN=COLUMNS/2-${#USER}-extrachars-${#exitstatus} ))
if (( PATHLEN>=1 )) && (( ${#pwd}>PATHLEN ));then
DIR=${pwd/*\/}
if (( ${#DIR}>PATHLEN ));then
postdots=...
pwd="${DIR:0:$(( PATHLEN-3 ))}"
else
predots=...
pwd="${pwd:$(( ${#pwd}-PATHLEN+3 ))}"
fi
fi
if ((EUID>0));then
promptchar=$
else
promptchar=#
fi
printf '\''\[\e[1;31m\]%s\[\e[0m\]'\'' $exitstatus
printf '\''\[\e[0m\] \[\e[1m\]%s\[\e[0m\]: '\'' "$USER"
printf '\''\[\e[0;31m\]%s\[\e[0m\]'\'' "$predots"
printf %s "$pwd"
printf '\''\[\e[0;31m\]%s\[\e[0m\]'\'' "$postdots"
printf '\'' \[\e[1m\]%s\[\e[0m\] '\'' $promptchar
)'
elif ((psi==3));then
# style:
# [ meow@mmm-air: ...huber__/go/src/github.com/meoow/nodefinder/example1 ]
# $
# [ meow@mmm-air: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... ]
# $
PS1='$(
exitstatus=$?
if ((exitstatus==0));then
exitstatus=''
fi
predots=""
postdots=""
extrachars=7
if [[ "$PWD" =~ ^$HOME ]];then
pwd=${PWD/$HOME/\~}
else
pwd=$PWD
fi
if [[ -z $COLUMNS ]];then
if type -P tput >/dev/null;then
COLUMNS=$(tput cols)
else
COLUMNS=80
fi
fi
(( PATHLEN=COLUMNS-${#USER}-${#HOSTNAME}-extrachars-${#exitstatus} ))
if (( PATHLEN>=1 )) && (( ${#pwd}>PATHLEN ));then
DIR=${pwd/*\/}
if (( ${#DIR}>PATHLEN ));then
postdots=...
pwd="${DIR:0:$(( PATHLEN-3 ))}"
else
predots=...
pwd="${pwd:$(( ${#pwd}-PATHLEN+3 ))}"
fi
fi
if ((EUID>0));then
promptchar=$
else
promptchar=#
fi
printf '\''\[\e[1;31m\]%s\[\e[0m\]'\'' $exitstatus
printf '\''\[\e[0m\][ \[\e[1m\]%s\[\e[0m\]@%s: '\'' "$USER" "$HOSTNAME"
printf '\''\[\e[0;31m\]%s\[\e[0m\]'\'' "$predots"
printf %s "$pwd"
printf '\''\[\e[0;31m\]%s\[\e[0m\]'\'' "$postdots"
printf '\'' ]\n\[\e[1m\]%s\[\e[0m\] '\'' $promptchar
)'
elif ((psi==4));then
PS1='$(
exitstatus=$?
if ((exitstatus==0));then
exitstatus=''
fi
extrachars=6
if [[ "$PWD" =~ ^$HOME ]];then
pwd=${PWD/$HOME/\~}
else
pwd=$PWD
fi
if [[ -z $COLUMNS ]];then
if type -P tput >/dev/null;then
COLUMNS=$(tput cols)
else
COLUMNS=80
fi
fi
(( PATHLEN=COLUMNS-${#USER}-extrachars-${#exitstatus} ))
pwdlen=${#pwd}
if (( pwdlen>PATHLEN ));then
newdirlist=()
IFS=/ pwdlist=($pwd)
for (( i=0; i<${#pwdlist[@]}; i++ ));do
p=${pwdlist[i]}
plen=${#p}
if (( pwdlen<=PATHLEN )) || (( plen<2 ));then
newdirlist+=(0 "$p")
else
if (( (pwdlen-PATHLEN)>plen ));then
(( pwdlen-=(${#p}-1 )))
newdirlist+=(31 ${p:0:1})
else
(( endlen=plen-(pwdlen-PATHLEN) ))
p=${p:0:$endlen}
newdirlist+=(31 "$p")
(( pwdlen=PATHLEN ))
fi
fi
done
else
newdirlist=(0 "$pwd")
fi
if ((EUID>0));then
promptchar=$
else
promptchar=#
fi
printf '\''\[\e[1;31m\]%s\[\e[0m\]'\'' $exitstatus
printf '\''\[\e[0m\][ \[\e[1m\]%s\[\e[0m\]: '\'' "$USER"
printf '\''\[\e[%dm\]%s\[\e[0m\]/'\'' "${newdirlist[@]}"
printf '\''\b ]\n\[\e[1m\]%s\[\e[0m\] '\'' $promptchar
)'
elif ((psi==5));then
PS1='$(
exitstatus=$?
if ((exitstatus==0));then
exitstatus=''
fi
extrachars=7
if [[ "$PWD" =~ ^$HOME ]];then
pwd=${PWD/$HOME/\~}
else
pwd=$PWD
fi
if [[ -z $COLUMNS ]];then
if type -P tput >/dev/null;then
COLUMNS=$(tput cols)
else
COLUMNS=80
fi
fi
(( PATHLEN=COLUMNS/2-${#USER}-extrachars-${#exitstatus} ))
pwdlen=${#pwd}
newdirlist=()
if (( pwdlen>PATHLEN ));then
IFS=/ pwdlist=($pwd)
for (( i=0; i<${#pwdlist[@]}; i++ ));do
p=${pwdlist[i]}
plen=${#p}
if (( pwdlen<=PATHLEN )) || (( plen<2 ));then
newdirlist+=(0 "$p")
else
if ((( pwdlen-PATHLEN)>plen ));then
(( pwdlen-=(${#p}-1) ))
newdirlist+=(31 ${p:0:1})
else
(( endlen=plen-(pwdlen-PATHLEN) ))
p=${p:0:$endlen}
newdirlist+=(31 "$p")
(( pwdlen=PATHLEN ))
fi
fi
done
else
newdirlist=(0 "$pwd")
fi
if ((EUID>0));then
promptchar=$
else
promptchar=#
fi
printf '\''\[\e[1;31m\]%s\[\e[0m\]'\'' $exitstatus
printf '\''\[\e[0m\] \[\e[1m\]%s\[\e[0m\]: '\'' "$USER"
printf '\''\[\e[%dm\]%s\[\e[0m\]/'\'' "${newdirlist[@]}"
printf '\''\b \[\e[1m\]%s\[\e[0m\] '\'' $promptchar
)'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment