Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

RAM wasn't built in a day.


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

SubjectAuthor
o Re: How to Add ANSI Color to User ResponsePierre Fortin

1
Re: How to Add ANSI Color to User Response

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pf@pfortin.com (Pierre Fortin)
Newsgroups: comp.lang.python
Subject: Re: How to Add ANSI Color to User Response
Date: Wed, 10 Apr 2024 19:44:49 -0400
Lines: 49
Message-ID: <mailman.91.1712796736.3468.python-list@python.org>
References: <E07728FE-3599-462D-B572-107C5D53C72F@yahoo.com>
<20240410194449.0b2ba78f@pfortin.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de WXBrM29l2EOcmQYBnVkX2AM0BFI09ifx2UtXEs86NTig==
Cancel-Lock: sha1:vxUwjzUvUs4F173U6V5bezf32w0= sha256:mFSqTQK454lutdscyHugJ7QYnTQuiTEr0CBndcgXEsU=
Return-Path: <pf@pfortin.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=pfortin.com header.i=@pfortin.com header.b=QMAEnG/O;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.016
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'this:': 0.03;
'subject:Add': 0.04; 'windows,': 0.05; 'ansi': 0.09; 'rarely':
0.09; 'terminal': 0.09; 'those.': 0.09; 'received:173': 0.13;
'import': 0.15; '-*-': 0.16; '2024': 0.16; 'alias': 0.16;
'jython': 0.16; 'license:': 0.16; 'mechanisms': 0.16; 'print(':
0.16; 'python;': 0.16; 'subject:User': 0.16; 'updated:': 0.16;
'windows.': 0.16; 'wrote:': 0.16; 'advance.': 0.17; 'instead':
0.17; 'thu,': 0.19; 'to:addr:python-list': 0.20; 'input': 0.21;
"i've": 0.22; 'code': 0.23; 'subject:How': 0.23; 'list,': 0.24;
'cc:2**0': 0.25; 'tried': 0.26; 'background': 0.28; 'module':
0.31; 'python-list': 0.32; 'skip:2 10': 0.32; 'but': 0.32;
'there': 0.33; 'script': 0.33; 'windows': 0.34; 'header:In-Reply-
To:1': 0.34; 'running': 0.34; 'question.': 0.35; 'following':
0.35; 'applying': 0.36; 'change': 0.36; "skip:' 10": 0.37;
'using': 0.37; 'directory': 0.37; 'way': 0.38; 'text': 0.39;
'use': 0.39; 'initial': 0.61; 'inline': 0.61; 'above': 0.62;
'to:': 0.62; 'color': 0.62; 'simply': 0.63; 'skip:b 10': 0.63;
'your': 0.64; 'content-type:multipart/mixed': 0.68; 'skip:# 10':
0.68; 'conditions': 0.70; "you'll": 0.73; 'skip:f 20': 0.75;
'choice': 0.76; 'url-ip:209/8': 0.76; 'life': 0.77; 'esc': 0.84;
'fortin,': 0.84; 'subject:Color': 0.84; 'tiny': 0.84;
'subject:Response': 0.91; 'colors': 0.93
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=pfortin.com
; s=default;
h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:
Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID:
Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc
:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe:
List-Post:List-Owner:List-Archive;
bh=I5lkxELXuVA3sWTK8ColH+ULvbb8FryMXSXVysmgNlQ=; b=QMAEnG/Ow2zQPhpR1LxJ0MnHPO
czzLbn13kzMpdd5/r9s6JboIoZUXzEi4WkKbeJ48yFWPZ/H2vGtIVFv9av2X4Fi8F/qnVz2ruMExk
FvB5b098jocf7z1Tm4afWQ+8xnrh4qMSrEnu+V5OtDZhLpmrX+0oLbUQ9q84G0dVYjgC2lCwbHqGD
qTwaEPE6Xk09z/bBgoQXES+6xQWgRFzEUuNCajz9khD2Dwkm3Rt/eEiz27PBo6sZLMyAh2me+hRe/
mydO45x2Ou547bgD7/wdBIJzQbl+8RPD2N8CwpgtlOfqgEMka07rYGz9IBs1PwtG4vFjXMOitFD3I
WCrEwPbg==;
In-Reply-To: <E07728FE-3599-462D-B572-107C5D53C72F@yahoo.com>
X-Mailer: Claws Mail 4.2.0git67 (GTK 3.24.41; x86_64-pc-linux-gnu)
X-Get-Message-Sender-Via: ecngx348.inmotionhosting.com: authenticated_id:
pf@pfortin.com
X-Authenticated-Sender: ecngx348.inmotionhosting.com: pf@pfortin.com
X-Originating-IP: 198.46.91.127
X-SpamExperts-Domain: ecngx348.inmotionhosting.com
X-SpamExperts-Username: 198.46.91.127
Authentication-Results: servconfig.com; auth=pass
smtp.auth=198.46.91.127@ecngx348.inmotionhosting.com
X-SpamExperts-Outgoing-Class: ham
X-SpamExperts-Outgoing-Evidence: Combined (0.04)
X-Recommended-Action: accept
X-Filter-ID: Pt3MvcO5N4iKaDQ5O6lkdGlMVN6RH8bjRMzItlySaT+0iCbPJvlnp+efDsGNizXAPUtbdvnXkggZ
3YnVId/Y5jcf0yeVQAvfjHznO7+bT5zrZdyVoOLhYcVso5GiMJOvpCl8L9DTIFp64Frdm8ZYlxD0
vza5BVf2b/8c2siWlGi/1Z6Ne3GE7lONBfpDDdfwdNPZPrZrSqqM2Fpygaj6FFprjQPFk8m4tSTf
ORUp3yn8qwQcwkW9qHuCQjXdrDqXe3R7ZfvkU6zhpxgosbXhv2U372HLLQ4Ug92U2EoY21AtE5fb
vB/SWSolon7ERO4/Z89+hhkjgTJvqbEpnG8j07A8bq8UYipQyYsgzpAU8UXAEhzpeLSOeb7pTEB0
PKENWCEWK4MJjQR0Pz5bwy7RNhYwxG0gRBDGWsqyFzjEAriD5afFc/F49EU9bp0LuEIrHkgzTtAE
xFvabtF6fBRPXozPfXekgt4IQq1JIFshHu2X/HWbVSWaQ11tN/Hgoy8NHQrtppYmxMktfUX7kfB+
UdezYqxGMqsKjARq8PBC4qgp+tMRAhpn5vKY8FZonbpjnXnx5Jyx5UFwVBdQZxxIFxR9Ovbqz/k9
Jlx8RTZkJCspOMQJvQ/Ck3iiU+4DQAj366V+bW1tsfg2xPA2CLYwN9BKAwe6u/Z3EPCB2KCyO1D3
jxVrDrqPZy8U0JQ+6KMqP+kbXXsMr6omyYsEFzRyWlRSe++cIxv9X3JYnOVSIdyw7lWQRGP8jNY5
mbEM6Os9qVSSlyoNRMmKU2qYs/p1Jz2SFokTsRq8oPdvjaG70zLlGAvydmSpKHekEVUug+OCpQ/I
TAxPiFkSmOHQBNUBW7Dpnnr95B+Abefn6S8Xl5Li9CRq57K0uyQT6BrSviwnfFBOqe5cmJnTiof+
6addocA/9xGgj7taJxWtHZ97uYpp19JONavN/Eyo3NGK0HM/w6nIoDr0sXUZ7YZoZ/GZ+rHInKmw
1motEJ2GqZaUiSvt266BaCapkcEMMfcyb/+DKygtVd32N1s3THnwUUyY+xRu+mQb2PJhMQqKqwXy
eXOsVTqPfHRSWbF38diUngZpePQ44lqMDpyryPGheI8wLrk0+W8o3Zor2fUccWgzAl4q5YeN3qyv
xY/UZ67E8UGlMS+4ayUpOtEhdxekWDmK9g==
X-Report-Abuse-To: spam@se1-lax1.servconfig.com
Content-Disposition: inline
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <20240410194449.0b2ba78f@pfortin.com>
X-Mailman-Original-References: <E07728FE-3599-462D-B572-107C5D53C72F@yahoo.com>
 by: Pierre Fortin - Wed, 10 Apr 2024 23:44 UTC

On Thu, 11 Apr 2024 04:50:49 +1000 WordWeaver Evangelist via Python-list
wrote:

>Hello List,
>
>I have a simple question. I use the following textPrompt in some of my Jython modules:
> '\nYour choice is? (A B C D E): ', maxChars=1, autoAccept=False, forceUppercase=True)
>Is there a way to add an ANSI color code to the end where the conditions are, so that the color of the user’s input is of a color of my choosing, instead of just white?
>Thank you very much in advance.
>Kind regards,
>Bill Kochman

Over the years, I've tried different mechanisms for applying colors until
I got my hands on f-stings; then I created a tiny module with all the
colors (cR, cG, etc) which made my life so much simpler (attached). The
module includes background colors (bX); but I very rarely use those.

Then, I just use the module like this:

# place the module in a directory where your script is
# e.g., $ mkdir mymods (rename as desired)
from mymods.colors import *
# or just include the contents inline

# this simply switches from one color to the next
print( f"{cR}red, {cB}blue, {cG}green {cO}are colors." )

# color just the response
ans = input( f"Answer?: {cG}" ) # turn off color on next line
print( f"{cO}You entered: {cY}{ans}{cO}" )
# ^^^^

# to turn off each color (white commas), change the above to:
print( f"{cR}red{cO}, {cB}blue{cO}, {cG}green {cO}are colors." )

On Windows, you'll need to add this *before* using the colors:
import os
if os.name == 'nt': # Only if we are running on Windows
from ctypes import windll
w = windll.kernel32
# enable ANSI VT100 colors on Windows.
w.SetConsoleMode(w.GetStdHandle(-11), 7)

HTH,
Pierre

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor