How to Add ANSI Color to User Response
Pierre Fortin
pf at pfortin.com
Sat Apr 13 02:18:07 EDT 2024
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 at 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_
More information about the Python-list
mailing list