• Uncategorized

About python : Make-a-pinned-print-to-the-terminal-with-python

Question Detail

How can I print to the terminal messages that will be
pinned to the top or bottom on the terminal console?

Thanks !

Question Answer

You can use curses module to make a pinned message I found this solution in an already asked question here I believe you can add your string in the window.addstr function

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)

You can get the terminal size you are running your code with help of the os module:

import os

def ptb(top_text, bottom_text):
    ts = os.get_terminal_size()
    n = ts.lines()
    os.system('clear') # Linux terminal only
    print(top_text + '\n'*(n-2) + bottom_text)

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.