jquast / blessed

Blessed is an easy, practical library for making python terminal apps
http://pypi.python.org/pypi/blessed
MIT License
1.18k stars 71 forks source link

Forced styling has no affect without a TTY #233

Closed ofek closed 2 years ago

ofek commented 2 years ago

Can be seen in https://github.com/jquast/blessed/pull/232 if you click run

avylove commented 2 years ago

What you're trying to demonstrate can be done with:

>>> import io
>>> import os
>>> import blessed
>>> stream = io.StringIO()
>>> os.environ['TERM'] = ""
>>> term = blessed.Terminal(stream=stream, force_styling=True)
>>> term.bold_red('test TTY')
'test TTY'

The problem is not that it's not a TTY, but that you don't have a TERM environment variable and you haven't specified kind when initializing Terminal. Essentially the library is honoring force_styling, but doesn't know what terminal type to use or terminal capabilities, so assumes a null one with no capabilities.

Restart REPL before running this

>>> import io
>>> import os
>>> import blessed
>>> stream = io.StringIO()
>>> os.environ['TERM'] = ""
>>> term = blessed.Terminal(stream=stream, force_styling=True, kind='xterm')
>>> term.bold_red('test TTY')
'\x1b[1m\x1b[31mtest TTY\x1b(B\x1b[m'
ofek commented 2 years ago

Ah, thanks! So when forcing one must also set kind. Is xterm the preferred value or are there others?

avylove commented 2 years ago

Well, I would say you should set kind if it can not be automatically determined or you want to be specific.

xterm or xterm-256color are the most common and what most terminals emulate these days. If you're just doing basic colors, xterm is fine. It really just depends on your use case.

ofek commented 2 years ago

Thank you!