How to Add ANSI Color to User Response

Alan Gauld learn2program at gmail.com
Wed Apr 10 18:41:23 EDT 2024


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




More information about the Python-list mailing list