How to Add ANSI Color to User Response

Cameron Simpson cs at cskk.id.au
Thu Apr 11 17:41:05 EDT 2024


On 10Apr2024 23:41, Alan Gauld <learn2program at gmail.com> wrote:
>Normally, for any kind of fancy terminal work, I'd say use curses.

My problem with curses is that it takes over the whole terminal - you 
need to manage everything from that point on. Great if you want it (eg 
some full-terminal tool like `top`) but complex overkill for small 
interactive (or not interactive) commands which are basicly printing 
lines of text. Which is what many of my scripts are.

That said, you don't _have_ to use curses to run the whole terminal. You 
can use it to just look up the terminal capabilities and use those 
strings. I haven't tried that for colours, but here's some same code 
from my `cs.upd` module using curses to look up various cursor motion 
type things:

     ... up the top ...
     try:
       import curses
     except ImportError as curses_e:
       warning("cannot import curses: %s", curses_e)
       curses = None

     ... later we cache the available motions ...
     try:
       # pylint: disable=no-member
       curses.setupterm(fd=backend_fd)
     except TypeError:
       pass
     else:
       for ti_name in (
           'vi',  # cursor invisible
           'vs',  # cursor visible
           'cuu1',  # cursor up one line
           'dl1',  # delete one line
           'il1',  # insert one line
           'el',  # clear to end of line
       ):
         # pylint: disable=no-member
         s = curses.tigetstr(ti_name)
         if s is not None:
           s = s.decode('ascii')
         self._ti_strs[ti_name] = s

     ... then a method to access the cache ...
     def ti_str(self, ti_name):
       ''' Fetch the terminfo capability string named `ti_name`.
           Return the string or `None` if not available.
       '''
       return self._ti_strs.get(ti_name, None)

     ... and even later, use the method ...
     # emit cursor_up
     cursor_up = self.ti_str('cuu1')
     movetxts.append(cursor_up * (to_slot - from_slot))

Generally, when I add ANSI colours I do it via a little module of my 
own, `cs.ansi_colour`, which you can get from PyPI using `pip`.

The two most useful items in it for someone else are probably 
`colourise` and `colourise_patterns`. Link:
https://github.com/cameron-simpson/css/blob/26504f1df55e1bbdef00c3ff7f0cb00b2babdc01/lib/python/cs/ansi_colour.py#L96

I particularly use it to automatically colour log messages on a 
terminal, example code:
https://github.com/cameron-simpson/css/blob/26504f1df55e1bbdef00c3ff7f0cb00b2babdc01/lib/python/cs/logutils.py#L824


More information about the Python-list mailing list