You are not logged in.
Hi, I want to do a couple of things, for starters im trying to print an updating string to the terminal, like how powerpill does it when you have the download complete messages and the speed updating at the bottom, how can I achieve this in python? Ive managed to get the stdout from aria2c.
Offline
I know one way, but I suspect it's probably not the preferred method. Still, it seems to work:
#!/usr/bin/env python
import time
import sys
for i in range (1,5):
sys.stdout.write("%d\r" % (i,))
sys.stdout.flush()
time.sleep(1)
print "\n",
sys.stdout.write("First message")
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rupdated*")
sys.stdout.flush()
print "\n",
If you run the code it should be obvious how it works. I'm sure you're clever enough to generalise it into a nice simple method. Anyway, it seems a bit crummy if you ask me but it might work if nobody comes up with a better idea. Maybe you could use ncurses but that might be a bit of overkill for your purposes, I don't know.
Offline
"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page
Offline
I don't know about python, but in bash you should use tput. 'Cause different terminals are different.
aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies
Offline
http://docs.python.org/dev/howto/curses.html
Or, likely more convenient, man console_codes.
Offline
Okay, I don't have time to pretty this up too much, but I have it in my scrap folder. Compiled it a while ago by trial and error.
You'd use these like this:
printf "%s%s%s" $'\eSOMERAWCONTROLCHARS' "text" $'\eSOMEMORE'
or if `tput foo` produces \eSOMERAWCONTROLCHARS and `tput bar 5` produces \eSOMEMORE, then you could use:
printf "%s%s%s" "$(tput foo)" "text" "$(tput bar)"
You could use `echo -e ...` instead of printf if you like.
The raw codes given are all understood by urxvt. (Some depend on compile-time options being enabled, I compile urxvt myself so I haven't kept close track of which options are enabled in the default Arch install. Most of the ones needed below should be.)
Many of these will also work on other terminals.
TPUT CAPABILITIES and RAW TERMCODES
tput clear # same as "clear"
tpur rs1 # does clear and also clears scrollback buffer
# raw version = \Ec
tput cols
tput lines
tput colors
tput btns # number of mouse buttons
tput ht # tab
tput cbt # backtab (raw is \E[Z)
other spacing chars:
\n = ^J = \012 = \x0a
\v = ^K = \013 = \x0b
\f = ^L = \014 = \x0c
\r = ^M = \015 = \x0d
tput bel # bell char (\a = ^G = \007)
tput flash # visual bell
tput sc # save cursor (sc=\E7, also \E[s, also \E[?1048h)
tput rc # restore cursor (rc=\E8, also \E[u, also \E[?1048l)
Below I use _ as an integer variable:
tput cuu _ # go up _ lines, stay in this column (cuu _=\E[_A)
tput cuu1 # same as cuu 1 (cuu1=\E[A)
not in terminfo:
\e[_F equivalent to cuu _ \r
tput cud _: down _ lines, stay this column (cud=\E[_B)
tput cud1 (^J, it's ignored by urxvt?)
won't go to line where no text has been written yet
use \v or \f to do that (\eD works too)
\n also goes down one line, then does \r (go to column 0) (\eE works too)
not in terminfo:
\e[_E equivalent to cud _ \r (like \n or \eE)
the following means you can use "tput cuf _" (whose raw version is \E[_C) or "tput cuf1" (whose raw version is \E[C):
tput cuf1, cuf _ # right _ spaces (doesn't overwrite) (cuf=\E[_C)
tput cub1 (^H), cub _ # left _ spaces (cub=\E[_D)
tput cup RR CC # go to row,col (1-based, topleft of screen) (cup=\E[rr;ccH, \e[rr;ccf works too)
tput home (\E[H)
tput cr # goto col 1 (^M)
tput hpa _ # go to column _ (1-based) (\E[_G)
tput vpa _ # go to row _ (1-based) (\E[_d)
tput ri, rin _ # "scroll back" i.e. move up _ lines, while discarding bottom _ lines of output (even if you're not there) (ri=\EM, rin=\E[_T, rin 1 works better than ri)
tput ind (^J, it's ignored), indn _ # "scroll forward" i.e. move down _ lines, but doesn't restore lines deleted by scrolling back (indn=\E[_S)
tput ech _ # erase ahead _ chars (overwrites with current bg color) (ech=\E[_X)
tput el # clear to end of line, stay here (el=\E[K, also \e[0K)
tput el1 # clear to start of line, stay here (el1=\E[1K)
\e[2K clears whole line
tput ed # clear to end of screen, stay here (ed=\E[J, also \e[0J)
\e[1J clears screen before cursor
\e[2J clears whole screen
tput ich1, ich _ # insert ahead _ chars (using current bg color) (ich,ich1=\E[_@)
tput il1, il _ # insert _ lines (scroll the line you're currently writing down _, discarding bottom _ lines of output, cursor goes to top of newly-scrolled region
this differs from ri/rin in that it inserts blank lines) (il,il1=\E[_L)
tput dch1, dch _ # delete ahead _ chars (dch,dch1=\E[_P)
tput dl1, dl _ # delete _ lines, including \n, stay this column (dl,dl1=\E[_M)
COLORS AND ATTRIBUTES
tput bold (\E[1m)
tput sitm/ritm # italics (\E[3m \E[23m)
tput smul/rmul # underline (\E[4m \E[24m)
tput blink # blinks and bold bg (\E[5m, rapid blink is 6)
tput rev, smso/rmso # standout (\E[7m \E[27m)
attrib 8=invis is not implemented
attrib 2=? is not implemented
tput op # use default colors (op=\E[39;49m)
Colors:
0 black, 1 red, 2 green, 3 yellow, 4 blue, 5 magenta, 6 cyan, 7 white, 9 default
3_ fg, 4_ bg
3_;1 may use bright fg, as does 9_
49;5 and 49;6 blink
other 4_;5 and 4_;6 use bold/bright bg, as does 10_
38;5;_ fg to color0-255
48;5;_ bg to color0-255
tput sgr0 # clear all attributes (sgr0=\E[m\017)
\e8 seems to restore attributes and colors as well as position
\e[?7h,\e[?7l seems to reset attributes and colors
\e[?XXYY where YY=s to save, r to restore, h to set, l to clear, t to toggle
and XX=5 whole terminal reverse video (used by flash)
XX=6 write from 0,0
XX=7 do wrap to next line (smam=\e[?7h, rmam=\e[?7l)
XX=25 cursor is visible (cnorm/cvvis=\e[?25h, civis=\e[?25l)
XX=47 use alternate buffer
XX=1047 use alternate buffer, clear when returning from it
XX=1048 save cursor on set, restore on clear
XX=1049 use alternatr buffer, clear on switching to it (smcup=\e[?1049h, rmcup=\e[r\e[?1049l)
write in "status line"/xterm title:
tput dsl # delete it (\E]2;\007)
tput tsl # to status line (\E]2)
tput fsl # from status line to main window (^G)
"\e]XX;YY\e\\" sets xterm parameters, where XX;YY=
0;text set icon name and window title to text
1;text set icon name only
2;text set window title only
10;color or index change terminal fg to color
11;color or index change terminal bg to color
12;color or index change cursor fg to color
13;color or index change pointer fg to color
17;color or index change highlight to color
706;color or index change bold to color
707;color or index change underlined chars to color
704:color or index change italics to color
50 or 710;font change normal fontset
711;font change bold fontset
712;font change italics fontset
713;font change boldItalics fontset
720 or 721;0 clear scrollback buffer
777;extension:parameters call perl extension
\eD index (not in terminfo)
Last edited by Profjim (2010-02-15 21:22:51)
Offline