LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 11-30-2008, 10:31 AM   #1
onykage
LQ Newbie
 
Registered: Nov 2008
Location: USA
Distribution: gentoo/fedora/ubuntu/DSL
Posts: 2

Rep: Reputation: 0
tput console display problems


Guys, forgive me if this question has been asked before. I did a search first, and I didnt see anything similar to this in the 3+ pages of results.

I am rebuilding my prompt. This machine is a laptop, thus I wanted to add my battery status to the output, but I didnt really want it displayed on my prompt. So i started playing with tput.

tput outputs correctly. BUT, if I dont tell tput to move up 1 row, it will not display.

Code:
tput cuu1 
tput sc
tput cup x x
output something
tput rc
if the "cuu1" is not used before the save cursor, the tput does not display. If i try to undo the move up one row by using a carriage return inside my prompt, the tput does not display.

atm, the script works, but if any list or commnd issued follows the prompt.

Code:
[mypromptishere]$ ls -l
file1   file2    file3    file4   file5   file6
[mypromptishere]$ file7    file8   file9   file10
thus like the above, because tput moved up 1 row the last line of the output is put after my prompt instead of my prompt being put after the last row of the output.

anyone have any ideas? Or know of a fix for this?
 
Old 11-30-2008, 10:53 AM   #2
onykage
LQ Newbie
 
Registered: Nov 2008
Location: USA
Distribution: gentoo/fedora/ubuntu/DSL
Posts: 2

Original Poster
Rep: Reputation: 0
derr, i fixed it 5 mins later.

lol, something hit me while posting my question, I tried it, and behold it worked like a charm. Here is a copy of my prompt.

Code:
# .bashrc
#!/bin/bash
#
# Bash battery monitor prompt
# @author onykage
# @date 113008
#

BATTERY=/proc/acpi/battery/BAT0

# all the colors in the rainbow
NOCOLOR="\[\033[0m\]"
BLACK="\[\033[0;30m\]"
GRAY="\[\033[1;30m\]"
BLUE="\[\033[0;34m\]"
LIGHT_BLUE="\[\033[1;34m\]"
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
CYAN="\[\033[0;36m\]"
TEAL="\[\033[1;36m\]"
RED="\[\033[0;31m\]"
PINK="\[\033[1;31m\]"
PURPLE="\[\033[0;35m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
BROWN="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
LIGHT_GRAY="\[\033[0;37m\]"
WHITE="\[\033[1;37m\]"

PROMPT_COMMAND=prompt_command

function prompt_command {
#get battery info
REM_CAP=`grep "^remaining capacity" $BATTERY/state | awk '{ print $3 }'`
BATSTATE=`grep "^charging state" $BATTERY/state | awk '{ print $3 }'`
FULL_CAP=`grep "design capacity:" $BATTERY/info | tr -d A-z | tr -d " " | tr -d :`

CHARGE=$(((100*$REM_CAP)/$FULL_CAP))
#display battery status via tput
let prompt_x=$(tput cols)-25
tput sc
tput cup 1 $prompt_x
tput setaf 2 ; tput bold
echo -n "["
tput setab 4
tput setaf 3
echo -n "Battery: "
#adjust color of status based on amount.
case "${BATSTATE}" in
   'charged')
      tput setaf 7 ; echo -n "Full"
   ;;
   'charging')
      if [ $CHARGE -le "15" ]
       then
        tput setaf 1 ; echo -n $CHARGE% 
      fi

      if [ $CHARGE -le "30" ]
       then
        tput setaf 3 ; echo -n $CHARGE% 
       else
        tput setaf 2 ; echo -n $CHARGE% 
      fi
        tput setaf 5
        echo -n " Charging"
   ;;
   'discharging')
      if [ $CHARGE -le "15" ]
        then
         tput setaf 1 ; echo -n $CHARGE% 
      fi

      if [ $CHARGE -le "30" ]
        then
         tput setaf 3 ; echo -n $CHARGE% 
        else
         tput setaf 2 ; echo -n $CHARGE% 
      fi
         tput setaf 6
         echo -n " Draining"
   ;;
esac

tput setaf 2 ; tput bold 
tput setab 0
echo -n "]"
tput rc

case $TERM in
    xterm*)
        TITLEBAR='\[\033]0;\u@\h:\w\007\]'
        ;;
    *)]
        TITLEBAR=""
        ;;
esac

#start with prompt here ------>
PS1="$YELLOW-$LIGHT_BLUE-($YELLOW\u$LIGHT_BLUE@$YELLOW\h\
$LIGHT_BLUE)-(\
$YELLOW\$PWD\
$LIGHT_BLUE)-$YELLOW-\
$NOCOLOR\n\
$YELLOW-$LIGHT_BLUE-(\
$YELLOW\$(date +%H%M)$LIGHT_BLUE:$YELLOW\$(date \"+%a,%d %b %y\")\
$LIGHT_BLUE:$WHITE\$$LIGHT_BLUE)-$YELLOW-$NOCOLOR " 

PS2="$LIGHT_BLUE-$YELLOW-$YELLOW-$LIGHT_GRAY "

}
# end of file
its not perfect yet, but it works.
enjoy.
 
  


Reply

Tags
bash, battery, prompt, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Tput: Add some spice to your UNIX shell scripts LXer Syndicated Linux News 0 04-12-2008 05:50 AM
tput - determining window size subu_s Programming 2 12-06-2004 11:46 PM
Console display error darin3200 Linux - General 2 08-12-2004 05:38 PM
console display problems after kernel compile Mugatu Linux - Software 0 02-26-2004 09:36 AM
display chinese in console skyevil Debian 1 09-12-2003 07:01 AM


All times are GMT -5. The time now is 08:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration