Results 1 to 4 of 4
I have a fairly complex bash prompt with some builtins. It shows battery percent, memory percent, and HDD space left all in colors. The colors even change when not plugged ...
- 03-07-2010 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 3
Custom Bash Prompt Line Wrap Issue
I have a fairly complex bash prompt with some builtins. It shows battery percent, memory percent, and HDD space left all in colors. The colors even change when not plugged in, or low memory left.
The problem is that the line wraps about 3/4 from the end to the beginning of the same line, so commands overwrite themselves and I cannot see them.
I originally posted in ubuntuforums but there are no bites yet.
It seems that I cannot post any URLs here until I have 15 posts...[sigh]. But it has helpful images.
It is at ubuntu forums. The thread is 1285442 and the title is "Custom Bash Prompt Line Wrap Issue".
Here is the script:
Anybody understand enough bash to help me? Or maybe point me somewhere where I can get help?Code:shopt -s checkwinsize #Defining Colors Used RED='\e[0;31m' REDWARNING='\e[4;31m' GREEN='\e[0;32m' YELLOW='\e[1;33m' ORANGE='\e[0;33m' BLUE='\e[0;34m' MAGENTA='\e[0;35m' WHITE='\e[0m' GREY='\e[1;30m' myprompt() { #VARS REM=`awk '/remaining capacity/ { print $3 }' /proc/acpi/battery/BAT0/state` LAST=`awk '/last full/ { print $4}' /proc/acpi/battery/BAT0/info` CHARGESTATE=`awk '/charging state/ { print $3 }' /proc/acpi/battery/BAT0/state` SPACE=`df -h | awk 'NR==2{ print $4 }'` MEMTOT=`free -m | awk 'NR==2{print $2}'` MEMUSED=`free -m | awk 'NR==3{print $3}'` CHARGEPERCENT=`echo $REM $LAST | awk '{printf "%d", ($1/$2)*100'}` case "${CHARGESTATE}" in 'charged') CHARGECOLOR="$GREEN+" ;; 'charging') CHARGECOLOR="$YELLOW+" ;; 'discharging') if [ "$CHARGEPERCENT" -le "30" ] ; then CHARGECOLOR="$REDWARNING-" else CHARGECOLOR="$YELLOW-" fi ;; esac MEMPERCENT=`echo $MEMTOT $MEMUSED | awk '{printf "%d", ($2/$1)*100'}` if [ $MEMPERCENT -ge "90" ] ; then MEMCOLOR=$RED else MEMCOLOR=$MAGENTA fi echo -e "$GREY|${CHARGECOLOR}${CHARGEPERCENT}%$GREY|$ORANGE${SPACE}$GREY|${MEMCOLOR}${MEMPERCENT}%$GREY|" } PS1="${debian_chroot:+($debian_chroot)}\$(myprompt)$BLUE\w$WHITE\$ "
- 03-08-2010 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
I had the same wrapping behavior, and modifying a previous command (history) did not always work well.
When I applied the advice to the prompt setting as noted here Readline problems - The UNIX and Linux Forums the problem went away for me:
I also used \033 in place of \e.... use the \[ escape to begin a sequence of non-printing characters,
and the \] escape to signal the end of such a sequence
Specifically, here is my bash prompt:
Let us know if this works for you ... cheers, drlCode:export PS1="\[\033[0;31m\]\u \h \W $ \[\033[0m\] "
A few other links:
Tip: Prompt magic
How to make custom prompts (scroll down to the bash section)Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 03-09-2010 #3Just Joined!
- Join Date
- Mar 2010
- Posts
- 3
Someone on Ubuntuforums helped within an hour of my last message. Must have hit it at the right time.
As for your fix; I could't add escape brackets to my PS1 line because of the scripts. The fix that worked is this:
Thank you for the quick reply.Code:There seems to be some unresolved issues with a "live" prompt command that issues terminal control signals. You could use the PROMPT_COMMAND to work around this. Also, non-printing characters within a prompt should be enclosed in \[ and \]. Replace your PS1 line with these 2: Code:
^you also have to change the `echo -e` line to `echo -ne` to complete the illusion. The only glitches I've seen out of this method so far are control+L clearing and control+R searching.Code:PROMPT_COMMAND='myprompt' PS1="\[$BLUE\]\w\[$WHITE\]\$ "
Last edited by quasarcannon; 03-09-2010 at 03:08 AM. Reason: Cleanup
- 03-09-2010 #4Just Joined!
- Join Date
- Mar 2010
- Posts
- 3
Here is the working code for anyone who wants it.
Code:###___________MY COOL STUFFS__________### shopt -s checkwinsize #Defining Colors Used RED='\e[0;31m' REDWARNING='\e[4;31m' GREEN='\e[0;32m' YELLOW='\e[1;33m' ORANGE='\e[0;33m' BLUE='\e[0;34m' MAGENTA='\e[0;35m' WHITE='\e[0m' GREY='\e[1;30m' myprompt() { #VARS REM=`awk '/remaining capacity/ { print $3 }' /proc/acpi/battery/BAT0/state` LAST=`awk '/last full/ { print $4 }' /proc/acpi/battery/BAT0/info` CHARGESTATE=`awk '/charging state/ { print $3 }' /proc/acpi/battery/BAT0/state` SPACE=`df -h . | awk 'NR==2{ print $4 }'` MEMTOT=`free -m | awk 'NR==2{ print $2 }'` MEMUSED=`free -m | awk 'NR==3{ print $3 }'` CHARGEPERCENT=`echo $REM $LAST | awk '{printf "%d", ($1/$2)*100'}` case "${CHARGESTATE}" in 'charged') CHARGECOLOR="$GREEN+" ;; 'charging') CHARGECOLOR="$YELLOW+" ;; 'discharging') if [ "$CHARGEPERCENT" -le "30" ] ; then CHARGECOLOR="$REDWARNING-" else CHARGECOLOR="$YELLOW-" fi ;; esac MEMPERCENT=`echo $MEMTOT $MEMUSED | awk '{printf "%d", ($2/$1)*100'}` if [ $MEMPERCENT -ge "90" ] ; then MEMCOLOR=$RED else MEMCOLOR=$MAGENTA fi #echo -e "$GREY|${CHARGECOLOR}${CHARGEPERCENT}%$GREY|$ORANGE${SPACE}$GREY|${MEMCOLOR}${MEMPERCENT}%$GREY|" echo -ne "$GREY|${CHARGECOLOR}${CHARGEPERCENT}%$GREY|$ORANGE${SPACE}$GREY|${MEMCOLOR}${MEMPERCENT}%$GREY|" } #PS1="${debian_chroot:+($debian_chroot)}\$(myprompt)$BLUE\w$WHITE\$ " PROMPT_COMMAND='myprompt' PS1="\[$BLUE\]\w\[$WHITE\]\$ "


Reply With Quote