1

This is a question for python language, and the terminal/console applies to Unix-like systems. It would be great if the solution is platform independent but that is not required.

The scenario is when the program keeps printing lines to terminal. However, among the many lines to print, some of them are special, like progress bar or status indicator: they have to be placed only at the bottom of the console, while all other lines can still be printed where they normally are, one after another and the screen scrolls as normal too.

An example code solution would be much better than a theoretic one. For this purpose here is an example:

def print_status(msg):
  # implement me
  print msg

def print_many_lines():
  print 'line one\n'
  print 'line two\n'
  print_status('i am here')
  print 'line three\n'
  print 'line four\n'
  print 'line five\n'
  print_status('i am changed')
  print 'line six\n'

Could you help me implement the print_status function so that the msg passed to it will always be printed at the bottom of terminal?

Please note that this is very different from another similar question that, when multiple lines are printed consecutively to the terminal, how can we make sure they are printed at the same line. Using \r can be useful in that scenario but it cannot solve this problem, because (1) these special lines may more likely not be printed consecutively and (2) there will be other lines printed after these special lines but these special lines should be kept at the bottom of terminal, still.

Thanks!

Community
  • 1
  • 1
Bruce
  • 1,608
  • 2
  • 17
  • 29
  • 2
    Possible duplicate of [Replace console output in python](http://stackoverflow.com/questions/6169217/replace-console-output-in-python) – Ian Jul 08 '16 at 10:40
  • I saw that question already before posting this one. No it is not. Please note that I do not just want all are printed at the same line when they are outputed *consecutively*, but these outputs are in random order. – Bruce Jul 08 '16 at 10:44
  • have you looked at ncurses? – Foon Jul 08 '16 at 11:22
  • Not yet, but it seems an overkill... – Bruce Jul 08 '16 at 11:27

2 Answers2

3

For a Windows terminal try the console module For unix the curses module would do.

This is how you do it on Windows.

c = Console.getconsole()
c.text(0, -1, 'And this is the string at the bottom of the console')

By specifying -1 for the second argument, string goes at bottom line.

For Linux, a working code which prints at last line.

import time
import curses

def pbar(window):
    height, width = window.getmaxyx()
    for i in range(10):
        window.addstr(height -1, 0, "[" + ("=" * i) + ">" + (" " * (10 - i )) + "]")
        window.refresh()
        time.sleep(0.5)

curses.wrapper(pbar)
dazzieta
  • 662
  • 4
  • 20
  • Could you please provide sample code for curses module to do the job? This question is asking for Unix-like systems specifically. – Bruce Jul 08 '16 at 10:53
  • It looks better this time. In your pbar function, though, there is a Window object passed in. If I understand correctly, in my provided code scenario, I have to initialize a window first before doing anything? It looks like the curses can only be initialized after the window is initialized. – Bruce Jul 08 '16 at 11:26
  • I dont know that much about curses, but it seems that curses open a new window and prints string on it. – dazzieta Jul 08 '16 at 11:36
  • I think that makes sense now. Without using a window object to control the whole screen, print at the bottom of screen may cause issues after program terminates. For example, if there are only a few lines printed out and they are far from the bottom which has a line for status, then after termination which should the console prompt be. Logically it should be after the bottom of screen and scroll one line automatically, but... not that magically. I'd try it and if all works that way I'll accept the answer. – Bruce Jul 08 '16 at 11:40
  • Yeah sure, and check documentation https://docs.python.org/2/library/curses.html if stuck. – dazzieta Jul 08 '16 at 11:44
0

It sounds like you want to use a curses library, which handles text based UIs on Unix systems. There's a module for it in python's standard library:

https://docs.python.org/2/library/curses.html#module-curses

How to use it is beyond the scope of an answer here I'm afraid.

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
  • I did see this one, too. However I did not find a way to put the output at the bottom of terminal and move back where it was. I now doubt if that can do the job so I asked for a solution with a sample code provided. – Bruce Jul 08 '16 at 10:46