Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

* * * * * THIS TERMINAL IS IN USE * * * * *


devel / comp.lang.python / Re: How to Add ANSI Color to User Response

SubjectAuthor
o Re: How to Add ANSI Color to User ResponseCameron Simpson

1
Re: How to Add ANSI Color to User Response

<mailman.99.1712872248.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29850&group=comp.lang.python#29850

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: How to Add ANSI Color to User Response
Date: Fri, 12 Apr 2024 07:41:05 +1000
Lines: 67
Message-ID: <mailman.99.1712872248.3468.python-list@python.org>
References: <1b305568-7ba1-4329-9f39-a0d22d769272@yahoo.co.uk>
<ZhhY8bhhA8Q1bb0U@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 5RjDBui2HOoWwc0jx0a9zgYkUrMGG+hBw8G2NwSoUDSg==
Cancel-Lock: sha1:a1P32KJ/X0eLjoR2kHa0MSCMHPI= sha256:RJ0knZJnw+c/5iZoXdcBZqnWpQTTDKhNmnKNy83PZTg=
Return-Path: <cameron@cskk.id.au>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url-ip:140.82/16': 0.03;
'def': 0.04; 'subject:Add': 0.04; 'pypi': 0.05; 'string': 0.07;
"'''": 0.09; 'ansi': 0.09; 'else:': 0.09; 'later,': 0.09; 'skip:`
10': 0.09; 'skip:` 20': 0.09; 'terminal': 0.09; 'text.': 0.09;
'typeerror:': 0.09; 'log': 0.12; 'url:github': 0.14; 'import':
0.15; 'url-ip:140/8': 0.15; '(eg': 0.16; 'are.': 0.16; 'colour':
0.16; 'colours': 0.16; 'fetch': 0.16; 'from:addr:cs': 0.16;
'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson': 0.16;
'gauld': 0.16; 'generally,': 0.16; 'message-id:@cskk.homeip.net':
0.16; 'none)': 0.16; 'none:': 0.16; 'received:13.237': 0.16;
'received:13.237.201': 0.16; 'received:13.237.201.189': 0.16;
'received:cskk.id.au': 0.16; 'received:id.au': 0.16;
'received:mail.cskk.id.au': 0.16; 'subject:User': 0.16; 'things:':
0.16; 'wrote:': 0.16; 'problem': 0.16; 'probably': 0.17; 'to:addr
:python-list': 0.20; 'code': 0.23; 'lines': 0.23; 'subject:How':
0.23; 'run': 0.23; "i'd": 0.24; 'tried': 0.26; 'else': 0.27;
'header:User-Agent:1': 0.30; 'whole': 0.30; 'takes': 0.31;
'module': 0.31; 'said,': 0.32; 'but': 0.32; 'someone': 0.34;
'same': 0.34; 'printing': 0.34; 'header:In-Reply-To:1': 0.34;
'complex': 0.35; 'particularly': 0.35; 'received:au': 0.35;
'work,': 0.36; 'those': 0.36; 'using': 0.37; 'two': 0.39; 'use':
0.39; 'on.': 0.39; 'alan': 0.40; 'want': 0.40; 'method': 0.61;
'great': 0.63; 'skip:m 20': 0.63; 'everything': 0.63; 'pass':
0.64; 'clear': 0.64; 'received:13': 0.64; 'top': 0.65; 'tool':
0.65; 'look': 0.65; 'named': 0.65; 'received:userid': 0.66;
'items': 0.68; 'url:css': 0.69; 'little': 0.73; 'manage': 0.73;
'url:lib': 0.81; 'subject:Color': 0.84; 'subject:Response': 0.91
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <1b305568-7ba1-4329-9f39-a0d22d769272@yahoo.co.uk>
User-Agent: Mutt/2.2.7 (2022-08-07)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <ZhhY8bhhA8Q1bb0U@cskk.homeip.net>
X-Mailman-Original-References: <1b305568-7ba1-4329-9f39-a0d22d769272@yahoo.co.uk>
 by: Cameron Simpson - Thu, 11 Apr 2024 21:41 UTC

On 10Apr2024 23:41, Alan Gauld <learn2program@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

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor