Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a static title/border on a Python cmd line application

I'm using the Python cmd module to create a CLI application. Everything works great! However, I'm attempting to tailor the app to a certain type of presence: text colors, title, using alpha-numeric characters as borders, etc.

Is there a standard way to create a screen overrun of sorts: the top of the screen where I have set a border and color title remain static? And from the middle of the screen, or thereabouts, down to the bottom of the screen, any text or commands entered at the prompt will stop being visible as they reach the title/border. Basically, what I'm after is for a user to always see the title/border unless they exit the CLI app. If they type help, of course, they will see the commands below the title/border. But, as they enter commands, ideally, the command menu will disappear behind the screen title/border.

Any direction on the best way I can achieve this is appreciated.

like image 713
suffa Avatar asked Oct 17 '22 22:10

suffa


1 Answers

Check curses

You should be able to decorate CLI/Terminal with colors and static borders.

I have extended example taken from HERE:

import curses
from multiprocessing import Process

p = None
def display(stdscr):
    stdscr.clear()
    stdscr.timeout(500)

    maxy, maxx = stdscr.getmaxyx()
    curses.newwin(2,maxx,3,1)

    # invisible cursor
    curses.curs_set(0)

    if (curses.has_colors()):
        # Start colors in curses
        curses.start_color()
        curses.use_default_colors()
        curses.init_pair(1, curses.COLOR_RED, -1)
    stdscr.refresh()

    curses.init_pair(1, 0, -1)
    curses.init_pair(2, 1, -1)
    curses.init_pair(3, 2, -1)
    curses.init_pair(4, 3, -1)

    bottomBox = curses.newwin(8,maxx-2,maxy-8,1)
    bottomBox.box()
    bottomBox.addstr("BottomBox")
    bottomBox.refresh()
    bottomwindow = curses.newwin(6,maxx-4,maxy-7,2)
    bottomwindow.addstr("This is my bottom view", curses.A_UNDERLINE)
    bottomwindow.refresh()

    stdscr.addstr("{:20s}".format("Hello world !"), curses.color_pair(4))
    stdscr.refresh()

    while True:
        event = stdscr.getch()
        if event == ord("q"):
            break

def hang():
    while True:
        temp = 1 + 1

if __name__ == '__main__':
    p = Process(target = hang)
    curses.wrapper(display)
like image 131
tmdag Avatar answered Nov 09 '22 10:11

tmdag