4

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

sorin
  • 161,544
  • 178
  • 535
  • 806
paulski
  • 318
  • 1
  • 7

1 Answers1

-1

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:

Community
  • 1
  • 1
sorin
  • 161,544
  • 178
  • 535
  • 806
  • This statement is not true: there in nothing inherent to Windows to prevent this. The git bash console shows ANSI ESC sequences correctly. NOTE: the git bash console is not relevant to this question; therefore, I did not post that as a solution. I am still looking for the solution to this question. As a possible solution, cf. https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling – Richard Jessop Mar 05 '21 at 18:49