简体   繁体   English

ANSI 转义序列在 Cygwin 中失败

[英]ANSI Escape Sequences Fail in Cygwin

I am trying to create a 'drop down menu' for a CLI program using ANSI escape sequences in Python 2.7.2.我正在尝试在 Python 2.7.2 中使用 ANSI 转义序列为 CLI 程序创建“下拉菜单”。 I use ANSI escape sequences to change the 'options' to red and display them below the input line, then afterwards clear them.我使用 ANSI 转义序列将“选项”更改为红色并将它们显示在输入行下方,然后清除它们。

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.我能够在运行 Ubuntu 10.04LTS 的系统上运行代码,该系统运行 Python 2.6.5,但无法让程序在运行 Cygwin minTTY 1.0.3 的 Windows XP 机器上运行。 Is there an issue with sys.stdout.flush() in Windows or Cygwin? Windows 或 Cygwin 中的 sys.stdout.flush() 是否存在问题? Is it a Python 2.6 to 2.7 issue?是 Python 2.6 到 2.7 的问题吗? 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.问题是原始输入文本只有在您在 minTTY 中做出选择后才会打印出来(同样,代码在 Ubuntu 上运行良好),这违背了提示文本的目的。 Thanks in advance - Paul提前致谢 - 保罗

You are not able to do this because Windows console does not support ANSI at all.您无法执行此操作,因为 Windows 控制台根本不支持 ANSI。

Back in MSDOS days there was an ANSI.SYS driver that you could load in order to enable them but not anymore.在 MSDOS 时代,有一个ANSI.SYS驱动程序,您可以加载它以启用它们,但现在没有了。

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)我的印象是,如果你想构建一个 TUI 界面(文本用户界面),你将需要调查像https://pypi.python.org/pypi/UniCurses这样的东西的使用

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM