6

I know that to update something like a progress bar on the command line, one uses '\r'. Is there any way to update multiple lines?

asked Nov 13, 2009 at 4:38

4 Answers 4

5

If you're using Python try using blessings. It's a really intuitive wrapper around curses.

Simple example:

from blessings import Terminal
term = Terminal()
with term.location(0, 10):
 print("Text on line 10")
with term.location(0, 11):
 print("Text on line 11")

If you're actually trying to implement a progress bar, consider using progressbar. It will save you a lot of \r cruft.

You can actually connect blessings and progressbar together. Try running this:

import time
from blessings import Terminal
from progressbar import ProgressBar
term = Terminal()
class Writer(object):
 """Create an object with a write method that writes to a
 specific place on the screen, defined at instantiation.
 This is the glue between blessings and progressbar.
 """
 def __init__(self, location):
 """
 Input: location - tuple of ints (x, y), the position
 of the bar in the terminal
 """
 self.location = location
 def write(self, string):
 with term.location(*self.location):
 print(string)
writer1 = Writer((0, 10))
writer2 = Writer((0, 20))
pbar1 = ProgressBar(fd=writer1)
pbar2 = ProgressBar(fd=writer2)
pbar1.start()
pbar2.start()
for i in range(100):
 pbar1.update(i)
 pbar2.update(i)
 time.sleep(0.02)
pbar1.finish()
pbar2.finish()

multiline-progress

answered Apr 18, 2013 at 15:36
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect! Small update: the current version of progressbar wants the file handle to have a flush() method too.
3

The best way is to use some existing library like ncurses. But you may try dirty workaround by clearing console with system call: system("cls");.

answered Nov 13, 2009 at 4:42

2 Comments

Is system("cls") Windows-only?
OS X core Darwin is Unix compliant and it should have "clear" along with other Unix-like systems.
2

You can use VT100 codes to reposition the cursor on a higher line, then overdraw it with your updated status.

answered Nov 13, 2009 at 4:41

1 Comment

The link is broken. Here's web archive's version: web.archive.org/web/20110103061220/http://web.eecs.utk.edu/…
0

The Curses library offers powerful control for console UIs.

answered Nov 13, 2009 at 4:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.