
Originally Posted by
jro";p="4733

Originally Posted by
ZennouRyuu";p="4682
Here is a snap of my ever so complicated bash prompt:....
Hey Zen, mind posting your PS1 env var? I am interested to see how you did it.
Sure, but I would like to make a disclaimer, this is not my original work, I took someones brilliant Idea from the GEntoo forums and modified it to support ACPI battery reading and take out a few superfluous things that I didnt need.
First I use the following script to create the prompt, This is done so you can actually use more complex bash scripting and have the ability to edit it all quickly without changing a million .bashrc files.
Code:
# SET THIS TO 0 if you are not using Gentoo Linux (http://www.gentoo.org)
DIST_GENTOO=1
batt=`/usr/bin/battery | cut -d " " -f 2`
hostnam=$(hostname)
unset temp
function prompt_command
{
local EXIT_CODE=$? # Getting last command exit code.
TERMWIDTH=$(tput cols) # Getting Term width.
#awful way to get current emerge(if you have sandbox documentation email me :) )
local C_EMERGE=`ps aux | grep sandbox | cut -d ']' -f 1 | cut -d '[' -f 2 | grep -v grep`
# n the term wide calculation. (length of your prompt PS1)
# Will be changed soon...
usernam=$(whoami)
local temp="$BLUEGREEN[ $usernam $BLUEGREEN]=[$GREY $hostnam $BLUEGREEN]$BLUEGREEN=[$GREY bg :$WHITE $JOBS $BLUEGREEN]=--$BLUEGREEN--=[$GREY $(date +%D) $(date +%T) $BLUEGREEN]"
# Getting size of unused terminal space :) ==> for filling with $fill characters.
local WHITE="\[\033[1;37m\]"
local NO_COLOR="\[\033[0m\]"
local RED="\[\033[1;31m\]"
local BLUEGREEN="\[\033[0;36m\]"
if [ ${#C_EMERGE} == 0 ]
then
C_DISK=$(df -h "$PWD" | grep / | awk '{print $4}')
C_DISKPER=$(df -h "$PWD" | grep / | awk '{print $5}' | sed "s:%::")
let fillsize=${TERMWIDTH}-${#temp}-${#C_DISK}-3
if [ $C_DISKPER != "-" ]
then
if (( $C_DISKPER <= 65 ))
then
C_DISK=$BLUE_GREEN$C_DISK$NO_COLOR
elif (( $C_DISKPER <= 85 ))
then
C_DISK=$WHITE$C_DISK$NO_COLOR
else
C_DISK=$RED$C_DISK$NO_COLOR
fi
else
C_DISK=$WHITE"None"$NO_COLOR
let fillsize=${TERMWIDTH}-${#temp}-7
fi
else
let fillsize=${TERMWIDTH}-${#temp}-${#C_EMERGE}-3
fi
if [ "$fillsize" -gt "0" ]
then
fill=" "
# Just used for filling with this line when term wide calculation. Verify C_EMERGE !!!
if [ $DIST_GENTOO != "1" ]
then
let fillsize=${TERMWIDTH}-${#temp}
fill="${fill:0:${fillsize}}" # non Gentoo distribution.
else
if [ ${#C_EMERGE} == 0 ]
then
fill="${fill:0:${fillsize}/2} $C_DISK ${fill:0:${fillsize}/2}" # DS
else
fill="${fill:0:${fillsize}/2} $C_EMERGE ${fill:0:${fillsize}/2}" # Gentoo
fi
fi
newPWD="`echo ${PWD} | sed s:${HOME}:~:`"
fi
# prompt path size adaptor ... need to be changed...
# doesn't work as expected now ...
if [ "$fillsize" -lt "0" ]
then
fill=""
# truncate PATH with "..."
let cut=3-${fillsize}
newPWD="...${PWD:${cut}}"
fi
# Calling the personnal prompt fct.
lycos
}
PROMPT_COMMAND=prompt_command
function lycos
{
# dirty ansi escape color codes... change to terminfo soon...
local WHITE="\[\033[1;37m\]"
local NO_COLOR="\[\033[0m\]"
local BLUEGREEN="\[\033[0;36m\]"
local GREY="\[\033[0;37m\]"
local RED="\[\033[1;31m\]"
local PATH_COLOR=$NO_COLOR
local APM_PATH="/bin/apm"
local JOBS="$(jobs | wc -l | awk '{ print $1 }')" # Getting current nb of background jobs.
# Detecting superuser with visible color :)
if [ $usernam != "root" ]
then
usernam="$GREY\u"
else
usernam="$RED\u"
fi
# Add a bold red number for exitcode if its nonzero
if [ $EXIT_CODE -ne 0 ]
then
EXIT_STR="$RED$EXIT_CODE"
else
EXIT_STR="$WHITE$EXIT_CODE"
fi
if [ -f $APM_PATH ]
then
local POWER_APM="$BLUEGREEN[$WHITE$(apm | sed -e "s/.*: \([1-9][0-9]*\)%/\1/" | tr -d " ")$BLUEGREEN]" # Getting APM power status.
else
local POWER_APM=""
fi
# Directory color path.
if [ -O "$PWD" ]
then
PATH_COLOR=$GREY #if owner.
elif [ -w "$PWD" ]
then
PATH_COLOR=$BLUEGREEN #if write permission.
else
PATH_COLOR=$RED #if denied for write.
fi
# Prompt line... make your prompt here ... (Need to be serialized.)
PS1="$BLUEGREEN[ $usernam $BLUEGREEN]=[$GREY $hostnam $BLUEGREEN]$BLUEGREEN=[$GREY bg :$WHITE $JOBS $BLUEGREEN]=-- ${fill}$BLUEGREEN--=[$GREY $(date +%D) $(date +%T) $BLUEGREEN]\n\
$BLUEGREEN[$GREY $PATH_COLOR$newPWD $BLUEGREEN]-[ $RED$batt $BLUEGREEN]:$WHITE "
PS2="$NO_COLOR>"
PS3="$NO_COLOR>"
PS4="$NO_COLOR>"
}
Then I add the following line to my bashrc files
Code:
[ -f /path/to/prompt.bash ] && source /path/to/prompt.bash
and there you have it.
Bookmarks