Sujet : Re: How to Add ANSI Color to User Response
De : pf (at) *nospam* pfortin.com (Pierre Fortin)
Groupes : comp.lang.pythonDate : 13. Apr 2024, 07:18:07
Autres entêtes
Message-ID : <mailman.102.1712998224.3468.python-list@python.org>
References : 1 2 3 4
User-Agent : Claws Mail 4.2.0git67 (GTK 3.24.41; x86_64-pc-linux-gnu)
On Thu, 11 Apr 2024 05:00:32 +0200 Gisle Vanem via Python-list wrote:
Pierre Fortin wrote:
>
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).
>
Attachments are stripped off in this list.
It would be nice to see this tiny module of yours.
An URL or attach as inline text please.
#!/bin/python
# -*- mode: python; -*-
# Copyright:
# 2024-Present, Pierre Fortin <
pf@pfortin.com>
# License:
# GPLv3 or any later version:
https://www.gnu.org/licenses/gpl-3.0.en.html# Created:
# 2023-11-10 Initial script
# Updated:
# Usage: f"{cR}red text {cG}green text{cO}; colors off"
# or: print( cY, "yellow text", cO )
# VT100 type terminal colors
ESC = "\u001b";
# Foreground Colors
_black = f"{ESC}[30m"; _red = f"{ESC}[31m"; _green = f"{ESC}[32m"; _yellow = f"{ESC}[33m"
_blue = f"{ESC}[34m"; _magenta = f"{ESC}[35m"; _cyan = f"{ESC}[36m"; _white = f"{ESC}[37m"
# Background Colors
_black_ = f"{ESC}[40m"; _red_ = f"{ESC}[41m"; _green_ = f"{ESC}[42m"; _yellow_ = f"{ESC}[43m"
_blue_ = f"{ESC}[44m"; _magenta_ = f"{ESC}[45m"; _cyan_ = f"{ESC}[46m"; _white_ = f"{ESC}[47m"
_off = f"{ESC}[0m"
ANSIEraseLine = '\033[2K\033[1G'
EL = ANSIEraseLine # short alias
# Color abbreviations (shortcuts for f-sting use)
cK=_black; cR=_red; cG=_green; cY=_yellow; cB=_blue; cM=_magenta; cC=_cyan; cW=_white; cO=_off
# background colors; use {cO} to turn off any color
bK=_black_; bR=_red_; bG=_green_; bY=_yellow_; bB=_blue_; bM=_magenta_; bC=_cyan_; bW=_white_