0

I have a python program with which I have to update the last line of a terminal while on the rest of them I print data as if the last line is non-existent. Here's an example. Suppose we have this (pseudo)code:

1. print("test1")
2. updateLastLine("79 percent")
3. print("test2")
4. updateLastLine("80 percent")

wheres on the terminal the data would look like:

the first 2 lines of code:

test1
79 percent

the next 2 lines of code added:

test1
test2
80 percent

How do I implement such a solution in python? Also, how would I capture input from keyboard from the updating line? It may help to know that the only place I capture input is from the updating line. Is there any library to do this?

A close example would be the sudo apt-get update command which behaves similarly.

Robert Lucian Chiriac
  • 686
  • 2
  • 15
  • 24
  • `apt-get update` doesn't take any interactive input – Wayne Werner Aug 01 '16 at 21:11
  • the behavior of `apt-get` *output*, as an example, is what OP is referring to, not input – chickity china chinese chicken Aug 01 '16 at 21:12
  • Yes, but it's the closest I can get to. At least it can do everything I'm required to do, but w/o input. – Robert Lucian Chiriac Aug 01 '16 at 21:13
  • 1
    I don't get the downvote since it's an eligible question. At least come with something to support your decision and makes us understand. Generally, whoever downvotes, has to tell the community what's wrong, so it can be fixed. – Robert Lucian Chiriac Aug 01 '16 at 21:22
  • You're asking for a *tool or resource*, which is off-topic for SO. But there is a http://softwarerecs.stackexchange.com/ – Wayne Werner Aug 01 '16 at 21:36
  • So you're suggesting me to vote for deletion and repost on softwarerecs.stackexchange.com? – Robert Lucian Chiriac Aug 01 '16 at 21:38
  • Robert, since I think you've got a valid question related to implementing a tool-like behavior *in python*, you may want to look into different print flags, I think it might work for part of your question - maybe check out some of these examples: [1](http://stackoverflow.com/questions/3419984/print-to-the-same-line-and-not-a-new-line-in-python), [2](http://forums.devshed.com/python-programming-11/print-line-console-previous-text-334539.html), [3](http://stackoverflow.com/questions/6840420/python-rewrite-multiple-lines-in-the-console), specifically the `print(var, end="\r")` type syntax. – chickity china chinese chicken Aug 01 '16 at 21:50
  • Yes, that might work .. somehow. I think I should move the cursor to the previous line, print the data normally, and then move the cursor to the last line and update the percentage w/ `print(var, end = "\r"`, maybe even trying to get an input. I was taking a look at the `curses` module. Let me read what you gave me. – Robert Lucian Chiriac Aug 01 '16 at 22:03
  • Cool, sounds good, those are just a few examples, there are tons more. And don't get discouraged by downvotes, sometimes people quick glance at a question and immediately respond with negativity without seeing potential or intent to learn. :) – chickity china chinese chicken Aug 01 '16 at 22:07
  • I won't get discouraged and thanks for encouraging me. I'll probably come up with an answer. – Robert Lucian Chiriac Aug 01 '16 at 22:17

2 Answers2

1
import shutil

class Printer(object):
    def __init__(self):
        self.last_line = ''

    def print(self, line):
        (w, h) = shutil.get_terminal_size()
        print(' '*(w-1), end='\r')
        print(line)
        self.update_last_line(self.last_line)

    def update_last_line(self, line):
        print(line, end=len(self.last_line) * ' ' + '\r')
        self.last_line = line

    def read_last_line(self, line):
        response = input(line)
        print('\033[{}C\033[1A'.format(len(line) + len(response)), end = '\r')
        return response


if __name__ == '__main__':
    p = Printer()
    p.print("test1")
    p.update_last_line("79 percent")
    p.print("test2")
    p.update_last_line("80 percent")
    response = p.read_last_line("age = ")
    p.print(response)
Robert Lucian Chiriac
  • 686
  • 2
  • 15
  • 24
Amitay Stern
  • 619
  • 7
  • 13
0

The percentage part put me on the way of a screen refreshing : In a (almost) endless loop, clear the user screen with

os.system('clear')

and then display your "test" list, the current percentage and the current user input (if there was one being typed). Refresh the display every time the percentage, the list or the user input are updated.

technico
  • 1,192
  • 1
  • 12
  • 22