Question

I am trying to create a 'drop down menu' for a CLI program using ANSI escape sequences in Python 2.7.2. I use ANSI escape sequences to change the 'options' to red and display them below the input line, then afterwards clear them.

I am able to run the code on a system running Ubuntu 10.04LTS which runs Python 2.6.5, but am not able to get the program to run on a Windows XP machine running Cygwin minTTY 1.0.3. Is there an issue with sys.stdout.flush() in Windows or Cygwin? Is it a Python 2.6 to 2.7 issue? Don't really know where to start the debug.

#!C:\Python27\python.exe
#!/usr/bin/python

import sys

table = {1:'foo', 2:'bar', 3:'foo'}
print '\n'
for item in table.keys() :
    sys.stdout.write('\033[1;31m    %s) %s\033[0m\n' % (item,table[item]))
    sys.stdout.flush()
sys.stdout.write('%s' %((item+1)*'\033M'))
sys.stdout.flush()
answer = raw_input("Select foobar: ")
sys.stdout.write('\033[J')
sys.stdout.flush()
print 'You have selected %s' % (table[answer])

The problem is that the raw input text does not print out until after you make your selection in minTTY (again, code works fine on Ubuntu), which kind of defeats the purpose of prompt text. Thanks in advance - Paul

Was it helpful?

Solution

You are not able to do this because Windows console does not support ANSI at all.

Back in MSDOS days there was an ANSI.SYS driver that you could load in order to enable them but not anymore.

My impression is that you will need to investigate the use of something like https://pypi.python.org/pypi/UniCurses if you want to build a TUI interface (text-user-interface)

References:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top