Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

curses and refreshing problem

3 views
Skip to first unread message

Karlo Lozovina

unread,
Dec 13, 2008, 3:29:43 PM12/13/08
to
Hi, I'm trying to implement text output interface, something similar to
wget, using curses module. There are just two things I can't find out how
to do: prevent curses from clearing the terminal when starting my program,
and leaving my output after the program closes. Any way to do this with
curses?

Thanks...


--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|

Carl Banks

unread,
Dec 14, 2008, 12:15:17 AM12/14/08
to
On Dec 13, 2:29 pm, Karlo Lozovina <_karlo_@_mosor.net_> wrote:
> Hi, I'm trying to implement text output interface, something similar to
> wget, using curses module. There are just two things I can't find out how
> to do: prevent curses from clearing the terminal when starting my program,
> and leaving my output after the program closes. Any way to do this with
> curses?

Unless you are referring to some wget screen mode I don't know about,
I suspect wget outputs its progress bar using carriage returns without
newlines. If that's all you want, there is no need to use curses.

Here is a little example program to illustrate:

import time, sys
for i in range(21):
sys.stdout.write('\rProgress: [' + '='*i + ' '*(20-i) + ']')
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\nFinised!\n")


Notice I'm using sys.stdout.write instead of print, because print
automatically appends a newline (which we don't want here). Yes you
can suppress the newline on print by using a trailing comma, but that
creates an undesirable artifact--a leading space--on the following
cycle.

Notice the '\r' at the beginning of the sys.stdout.write call. This
tells the terminal to move the cursor back to the beginning of the
line, whence it draws the new progress bar over the old progress bar.

And finally, notice the call to sys.stdout.flush(). When a program is
run on a terminal the underlying I/O is usually line-buffered, meaning
that nothing actually gets output until a newline character is sent.
Therefore we have to call sys.stdout.flush() to flush the buffer
manually.


Carl Banks

Karlo Lozovina

unread,
Dec 14, 2008, 6:52:15 AM12/14/08
to
Carl Banks <pavlove...@gmail.com> wrote in
news:69d2698a-6f44-4d85...@r15g2000prd.googlegroups.com:

> Unless you are referring to some wget screen mode I don't know about,
> I suspect wget outputs its progress bar using carriage returns
> without newlines. If that's all you want, there is no need to use
> curses.
>
> Here is a little example program to illustrate:
>
> import time, sys
> for i in range(21):
> sys.stdout.write('\rProgress: [' + '='*i + ' '*(20-i) + ']')
> sys.stdout.flush()
> time.sleep(1)
> sys.stdout.write("\nFinised!\n")

Thanks, that's it! I just assumed wget uses curses for the progress bar,
so the carriage return didn't even cross my mind ;).

Carl Banks

unread,
Dec 14, 2008, 1:14:45 PM12/14/08
to
On Dec 14, 5:52 am, Karlo Lozovina <_karlo_@_mosor.net_> wrote:

> Carl Banks <pavlovevide...@gmail.com> wrote innews:69d2698a-6f44-4d85...@r15g2000prd.googlegroups.com:
>
> > Unless you are referring to some wget screen mode I don't know about,
> > I suspect wget outputs its progress bar using carriage returns
> > without newlines.  If that's all you want, there is no need to use
> > curses.
>
> > Here is a little example program to illustrate:
>
> > import time, sys
> > for i in range(21):
> >     sys.stdout.write('\rProgress: [' + '='*i + ' '*(20-i) + ']')
> >     sys.stdout.flush()
> >     time.sleep(1)
> > sys.stdout.write("\nFinised!\n")
>
> Thanks, that's it! I just assumed wget uses curses for the progress bar,
> so the carriage return didn't even cross my mind ;).


Sure. One other thing I'd like to point out is sometimes even if
carriage return is not sufficient, you can get a little better control
of the terminal by using escape sequences (quod Google). The more
basic Xterm escape sequences work in pretty much any Unix-like
environment you'd see these days, some might even work on a Windows
terminal. For example, print "\033[2J\033[H" probably clears your
screen and moves the cursor to top.


Carl Banks

0 new messages