English 中文(简体)
Multiline progress bars
原标题:

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

最佳回答

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");.

问题回答

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 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

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

The Curses library offers powerful control for console UIs.





相关问题
Command Line Parameters

I m trying to add my file in Visual Studio as command line parameters. I know my code works since when I use fopen("whole path here", "r"), it runs. I then add the file as a command line parameter ...

Read from File, or STDIN

I ve written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

热门标签