Re: How to Add ANSI Color to User Response
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 # 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_ -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On 10Apr2024 23:41, Alan Gauld 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On 4/10/2024 6:41 PM, Alan Gauld via Python-list wrote: On 10/04/2024 19:50, WordWeaver Evangelist via Python-list wrote: I have a simple question. I use the following textPrompt in some of my Jython modules: '\n[1;33mYour 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 Normally, for any kind of fancy terminal work, I'd say use curses. But I suspect Jython may not support curses? On the offchance it does do curses it would look like: import curses def main(scr): if curses.has_colors(): # check the terminal supports color curses.start_color(). # init the color system curses.init_pair(1,curses.COLOR_YELLOW,curses.COLOR_BLUE) # Now start adding text coloring as desired... scr.addstr(0,0,"This string is yellow and blue", curses.color_pair(1)) scr.refresh(). # make it visible else: scr.addstr("Sorry, no colors available") curses.wrapper(main) HTH Curses is a C module, and there is a Python interface to it. Jython would have to find an equivalent Java library. Still, isn't the case that the terminal color output commands are pretty standard? They could just be stuck into the output string. Doing more fancy things, like moving the cursor arbitrarily, probably differ but the OP just mentioned colors. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On 2024-04-10, Alan Gauld via Python-list wrote: > On 10/04/2024 19:50, WordWeaver Evangelist via Python-list wrote: > >> I have a simple question. I use the following textPrompt in some of my >> Jython modules: >> '\n[1;33mYour 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 > > Normally, for any kind of fancy terminal work, I'd say use curses. If you want to use the terminal escape sequences provided by terminfo and ncurses, but don't want to use the ncurses windowing functions, here are some notes on how to do that: https://github.com/GrantEdwards/Python-curses-and-terminfo That too is C-Python oriented, and I don't really know how to do the same things using Jython. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On Wed, 10 Apr 2024 at 18:51, WordWeaver Evangelist via Python-list wrote: > I have a simple question. I use the following textPrompt in some of my > Jython modules: > '\n [1;33mYour 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? Hi Bill, Here's a tutorial: https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html Note the sentence: "once you print out the special code enabling a color, the color persists forever until someone else prints out the code for a different color, or prints out the Reset code to disable it." Here's a more detailed specification: https://en.wikipedia.org/wiki/ANSI_escape_code And here's a conversation: http://mywiki.wooledge.org/BashFAQ/037 (see the first sentence, and then under the heading "Discussion") that might help you decide whether this approach will satisy your need in your particular circumstances of operating system, Python version, and terminal settings. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
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: > '\n[1;33mYour 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On 10/04/2024 19:50, WordWeaver Evangelist via Python-list wrote: > I have a simple question. I use the following textPrompt in some of my Jython > modules: > '\n[1;33mYour 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 Normally, for any kind of fancy terminal work, I'd say use curses. But I suspect Jython may not support curses? On the offchance it does do curses it would look like: import curses def main(scr): if curses.has_colors(): # check the terminal supports color curses.start_color(). # init the color system curses.init_pair(1,curses.COLOR_YELLOW,curses.COLOR_BLUE) # Now start adding text coloring as desired... scr.addstr(0,0,"This string is yellow and blue", curses.color_pair(1)) scr.refresh(). # make it visible else: scr.addstr("Sorry, no colors available") curses.wrapper(main) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On 11/04/24 06:50, WordWeaver Evangelist via Python-list wrote: I have a simple question. I use the following textPrompt in some of my Jython modules: '\n[1;33mYour 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 Haven't tried using any of theses techniques, but may define input() color, as well as print():- How to print colored terminal text in Python MAR 06, 2024 ... https://byby.dev/py-print-colored-terminal-text -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On 2024-04-10, WordWeaver Evangelist via Python-list wrote: > I have a simple question. I use the following textPrompt in some of my Jython > modules: > '\n[1;33mYour 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? I'm not sure what is meant by "the end where the conditions are", nor do I know what "textPrompt" refers to. Are you asking how to put a second escape sequence at the end of the string literal after the colon? -- Grant -- https://mail.python.org/mailman/listinfo/python-list