• Re: How to Add ANSI Color to User Response

    From dn@21:1/5 to WordWeaver Evangelist via Python-li on Thu Apr 11 09:43:37 2024
    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:
    '\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

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pierre Fortin@21:1/5 to All on Wed Apr 10 19:44:49 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Alan Gauld via Python-list on Wed Apr 10 20:01:51 2024
    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:
    '\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

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to WordWeaver Evangelist on Thu Apr 11 11:47:35 2024
    WordWeaver Evangelist <[email protected]> wrote or quoted:
    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?

    Now sonny, I ain't gonna give you a straight answer to your question,
    but let me tell you what I know. You Windows users out there would
    do well to look into that there "colorama" Python module! It'll help
    you cook up them fancy ANSI escape sequences to get some color goin'
    on in your Windows command prompt (or maybe even cross-platform).

    Now, I can't rightly say if that's still needed these days or
    if the new-fangled consoles even require it anymore. But if
    you're hankerin' to add a little pizzazz to your text, that
    colorama gizmo might just do the trick.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From rthiebaud@21:1/5 to Stefan Ram on Thu Apr 11 13:23:12 2024
    On 4/11/24 07:47, Stefan Ram wrote:
    WordWeaver Evangelist <[email protected]> wrote or quoted:
    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?

    Now sonny, I ain't gonna give you a straight answer to your question,
    but let me tell you what I know. You Windows users out there would
    do well to look into that there "colorama" Python module! It'll help
    you cook up them fancy ANSI escape sequences to get some color goin'
    on in your Windows command prompt (or maybe even cross-platform).

    Now, I can't rightly say if that's still needed these days or
    if the new-fangled consoles even require it anymore. But if
    you're hankerin' to add a little pizzazz to your text, that
    colorama gizmo might just do the trick.

    This answer seems rather insulting.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to rthiebaud on Thu Apr 11 17:44:35 2024
    rthiebaud <[email protected]> wrote or quoted:
    On 4/11/24 07:47, Stefan Ram wrote:
    WordWeaver Evangelist <[email protected]> wrote or quoted:
    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?
    Now sonny, I ain't gonna give you a straight answer to your question,
    but let me tell you what I know. You Windows users out there would
    do well to look into that there "colorama" Python module! It'll help
    you cook up them fancy ANSI escape sequences to get some color goin'
    on in your Windows command prompt (or maybe even cross-platform).
    Now, I can't rightly say if that's still needed these days or
    if the new-fangled consoles even require it anymore. But if
    you're hankerin' to add a little pizzazz to your text, that
    colorama gizmo might just do the trick.
    This answer seems rather insulting.

    Could you explain how exactly you came to this conclusion?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Alan Gauld on Fri Apr 12 07:41:05 2024
    On 10Apr2024 23:41, Alan Gauld <[email protected]> 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gisle Vanem@21:1/5 to Pierre Fortin on Thu Apr 11 05:00:32 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pierre Fortin@21:1/5 to All on Sat Apr 13 02:18:07 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 <[email protected]>
    # 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_

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Mon Apr 15 15:50:53 2024
    [email protected] (Stefan Ram) wrote or quoted:
    rthiebaud <[email protected]> wrote or quoted:
    On 4/11/24 07:47, Stefan Ram wrote:
    WordWeaver Evangelist <[email protected]> wrote or quoted:
    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?
    Now sonny, I ain't gonna give you a straight answer to your question,
    but let me tell you what I know. You Windows users out there would
    do well to look into that there "colorama" Python module! It'll help
    you cook up them fancy ANSI escape sequences to get some color goin'
    on in your Windows command prompt (or maybe even cross-platform).
    Now, I can't rightly say if that's still needed these days or
    if the new-fangled consoles even require it anymore. But if
    you're hankerin' to add a little pizzazz to your text, that
    colorama gizmo might just do the trick.
    This answer seems rather insulting.
    Could you explain how exactly you came to this conclusion?

    I'm currently tinkering with different ways to express myself
    in English. In my last attempt, I tried to come across like
    an older person. I didn't mean to offend anyone, but going
    forward I'll do my darndest to steer clear of anything that
    could be perceived as an insult, as far as I can reckon.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)