• How to Add ANSI Color to User Response

    From WordWeaver Evangelist@billkochman777@gmail.com to comp.lang.python on Thu Apr 11 04:50:49 2024
    From Newsgroup: comp.lang.python


    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--- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grant.b.edwards@gmail.com to comp.lang.python on Wed Apr 10 15:53:20 2024
    From Newsgroup: comp.lang.python

    On 2024-04-10, WordWeaver Evangelist via Python-list <python-list@python.org> 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?

    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




    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From dn@PythonList@DancesWithMice.info to comp.lang.python on Thu Apr 11 09:43:37 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Alan Gauld@learn2program@gmail.com to comp.lang.python on Wed Apr 10 23:41:23 2024
    From Newsgroup: comp.lang.python

    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
    --
    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


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Pierre Fortin@pf@pfortin.com to comp.lang.python on Wed Apr 10 19:44:49 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From David@bouncingcats@gmail.com to comp.lang.python on Thu Apr 11 02:24:59 2024
    From Newsgroup: comp.lang.python

    On Wed, 10 Apr 2024 at 18:51, WordWeaver Evangelist via Python-list <python-list@python.org> 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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grant.b.edwards@gmail.com to comp.lang.python on Wed Apr 10 22:37:31 2024
    From Newsgroup: comp.lang.python

    On 2024-04-10, Alan Gauld via Python-list <python-list@python.org> 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.

    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

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Thomas Passin@list1@tompassin.net to comp.lang.python on Wed Apr 10 20:01:51 2024
    From Newsgroup: comp.lang.python

    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.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Thu Apr 11 11:47:35 2024
    From Newsgroup: comp.lang.python

    WordWeaver Evangelist <billkochman777@gmail.com> 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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From rthiebaud@geodandw@aol.com to comp.lang.python on Thu Apr 11 13:23:12 2024
    From Newsgroup: comp.lang.python

    On 4/11/24 07:47, Stefan Ram wrote:
    WordWeaver Evangelist <billkochman777@gmail.com> 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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Thu Apr 11 17:44:35 2024
    From Newsgroup: comp.lang.python

    rthiebaud <geodandw@aol.com> wrote or quoted:
    On 4/11/24 07:47, Stefan Ram wrote:
    WordWeaver Evangelist <billkochman777@gmail.com> 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?
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Cameron Simpson@cs@cskk.id.au to comp.lang.python on Fri Apr 12 07:41:05 2024
    From Newsgroup: comp.lang.python

    On 10Apr2024 23:41, Alan Gauld <learn2program@gmail.com> 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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Gisle Vanem@gvanem@yahoo.no to comp.lang.python on Thu Apr 11 05:00:32 2024
    From Newsgroup: comp.lang.python

    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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Pierre Fortin@pf@pfortin.com to comp.lang.python on Sat Apr 13 02:18:07 2024
    From Newsgroup: comp.lang.python

    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_
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Mon Apr 15 15:50:53 2024
    From Newsgroup: comp.lang.python

    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    rthiebaud <geodandw@aol.com> wrote or quoted:
    On 4/11/24 07:47, Stefan Ram wrote:
    WordWeaver Evangelist <billkochman777@gmail.com> 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.
    --- Synchronet 3.20a-Linux NewsLink 1.114