jquast / blessed

Blessed is an easy, practical library for making python terminal apps

Home Page:http://pypi.python.org/pypi/blessed

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: Can one have multiple Terminal() windows on the same console screen at the same time?

pjfarleyiii opened this issue · comments

I am a little confused by the blessed docs. I am not sure after reading them whether I can have multiple Terminal() objects open at the same time all displaying on the same console window at the same time. The project I am working on requires 5 separate windows on screen at the same time (no position overlap among them).

In plain curses, I just use newwin() for each window and can addstr() separately to each window. Four of the five use curses,idlok() and curses.scrollok() to display multi-line output correctly. The fifth window is for entering commands and consumes all keyboard input.

Can blessed do the same kind of thing with multiple Terminal() objects on the same screen at the same time? A simple example would be helpful.

TIA for your guidance on this.

Peter

Environment: Win10, python 3.8.7, blessed 1.18.0

Blessed provides abstraction for the terminal, but not for curses windows as you described. You can can likely combine the two to simplify your curses implementation. Otherwise, you may want to look at what others have done.

commented

I came here with an almost identical need. Ironically, the further I searched for answers in the documentation here for generating boxes/panels/pads, the more I was funneled towards the Blessed codebase that exists in NodeJS, which has all such capabilities in spades. Alas, back to curses I begrudgingly go.

Blessed provides abstraction for the terminal, but not for curses windows as you described. You can can likely combine the two to simplify your curses implementation. Otherwise, you may want to look at [what others have done] (https://blessed.readthedocs.io/en/stable/intro.html).

Thanks for the examples link but the only example that vaguely resembles my project is Voltron, and browsing their code repository it appears to me that they only use blessed directly via print functions, not in combination with curses addstr().

Below I pasted a very simple curses + blessed example that does not work and I do not understand why not. I am feeding a blessed term.red() formatted string to curses addstr() but the escape sequences simply appear on the curses screen AS SEQUENCES, and do NOT color the text red.

E.G., when I press "w" in the curses screen I get "^[[31mI pressed 119=w^[(B^[[m" on screen, not red text.saying "I pressed 119=w".

My question is simple - How do I pass term.red (or any other attribute-formatted string) to curses addstr() and have the formatted result appear on screen in the format I requested?

Or am I missing some basic idea of the available functionality entirely (which is certainly possible)? When you said "but not for curses windows as you described" does that mean that blessed formatted strings can NOT be sent directly to the screen using the curses function addstr()? Do I have to parse every input to addstr() for each escape sequence and somehow translate each sequence to attribute inputs to curses functions like window.chgat() (e.g., as the AnsiToWin32 parser in colorama does but using curses attribute functions instead of win32 functions)?

I tried the below script in both a cmd.exe window and in a Windows Terminal window and it failed the same way in both. It also fails the same way in WSL2 Ubuntu 20.04 with curses libncurses6/focal,now 6.2-0ubuntu2 amd64.

Tested Environments:

  1. WIn10, windows-curses 2.2.0, python 3.8.7, blessed 1.18.0
  2. WIn10, WSL2, ubuntu 20.04, python 3.8.5, blessed 1.18.0

TIA for your patience with someone a little new to python and curses. If my code shows any obvious bugs or omissions, please, by all means correct me.

Peter

Example curses + blessed code:

import blessed
term = blessed.Terminal()
print(term.cyan("blessed print output"))
try:
    stdscr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    stdscr.keypad(1)
    if curses.can_change_color():
        curses.start_color()
        curses.use_default_colors()
        stdscr.addstr("curses.can_change_color() is True\n")
    else:
        stdscr.addstr("curses.can_change_color() is False\n")
    while True:
        c = stdscr.getch()
        if 32 <= c <= 255:
            stdscr.addstr(term.red("I pressed {}={}".format(c, chr(c))) + "\n")
        if chr(c) == 'q':
            break
finally:
    curses.nocbreak(); stdscr.keypad(0); curses.echo()
    curses.endwin()```

addstr() and related methods do not interpret escape sequences. That's a curses-ism. If you search you'll see lots of people asking similar questions. While you could likely complete your project with Blessed, it may not be ideal for your use case.

addstr() and related methods do not interpret escape sequences. That's a curses-ism. If you search you'll see lots of people asking similar questions. While you could likely complete your project with Blessed, it may not be ideal for your use case.

Thank you for the clear answer. I believe that I now understand, and I agree, Blessed is not ideal for my use case.

Thanks also for your patience and help in curing my ignorance.

Peter