如何检测控制台是否支持Python中的ANSI转义码?

时间:2022-06-01 21:37:30

In order to detect if console, correctly sys.stderr or sys.stdout, I was doing the following test:

为了检测控制台,正确的sys.stderr或sys.stdout,我正在进行以下测试:

if hasattr(sys.stderr, "isatty") and sys.stderr.isatty():
   if platform.system()=='Windows':
       # win code (ANSI not supported but there are alternatives)
   else:
       # use ANSI escapes
else:
   # no colors, usually this is when you redirect the output to a file

Now the problem became more complex while running this Python code via an IDE (like PyCharm). Recently PyCharm added support for ANSI, but the first test fails: it has the isatty attribute but it is set to False.

现在,通过IDE(如PyCharm)运行此Python代码时,问题变得更加复杂。最近PyCharm添加了对ANSI的支持,但第一次测试失败:它具有isatty属性但设置为False。

I want to modify the logic so it will properly detect if the output supports ANSI coloring. One requirement is that under no circumstance I should output something out when the output is redirected to a file (for console it would be acceptable).

我想修改逻辑,以便正确检测输出是否支持ANSI着色。一个要求是在任何情况下我都不应该在输出重定向到文件时输出一些东西(对于控制台,它是可以接受的)。

Update

Added more complex ANSI test script at https://gist.github.com/1316877

通过https://gist.github.com/1316877添加了更复杂的ANSI测试脚本

2 个解决方案

#1


15  

Django users can use django.core.management.color.supports_color function.

Django用户可以使用django.core.management.color.supports_color函数。

if supports_color():
    ...

The code they use is:

他们使用的代码是:

def supports_color():
    """
    Returns True if the running system's terminal supports color, and False
    otherwise.
    """
    plat = sys.platform
    supported_platform = plat != 'Pocket PC' and (plat != 'win32' or
                                                  'ANSICON' in os.environ)
    # isatty is not always implemented, #6223.
    is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
    if not supported_platform or not is_a_tty:
        return False
    return True

See https://github.com/django/django/blob/master/django/core/management/color.py

#2


9  

I can tell you how others have solved this problem, but it's not pretty. If you look at ncurses as an example (which needs to be able to run on all kinds of different terminals), you'll see that they use a terminal capabilities database to store every kind of terminal and its capabilities. The point being, even they were never able to automatically "detect" these things.

我可以告诉你其他人是如何解决这个问题的,但它并不漂亮。如果你以ncurses为例(它需要能够在各种不同的终端上运行),你会发现他们使用终端功能数据库来存储各种终端及其功能。关键是,即使他们从未能够自动“检测”这些东西。

I don't know if there's a cross-platform termcap, but it's probably worth your time to look for it. Even if it's out there though, it may not have your terminal listed and you may have to manually add it.

我不知道是否有跨平台的termcap,但它可能值得您花时间去寻找它。即使它在那里,它可能没有列出你的终端,你可能不得不手动添加它。

#1


15  

Django users can use django.core.management.color.supports_color function.

Django用户可以使用django.core.management.color.supports_color函数。

if supports_color():
    ...

The code they use is:

他们使用的代码是:

def supports_color():
    """
    Returns True if the running system's terminal supports color, and False
    otherwise.
    """
    plat = sys.platform
    supported_platform = plat != 'Pocket PC' and (plat != 'win32' or
                                                  'ANSICON' in os.environ)
    # isatty is not always implemented, #6223.
    is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
    if not supported_platform or not is_a_tty:
        return False
    return True

See https://github.com/django/django/blob/master/django/core/management/color.py

#2


9  

I can tell you how others have solved this problem, but it's not pretty. If you look at ncurses as an example (which needs to be able to run on all kinds of different terminals), you'll see that they use a terminal capabilities database to store every kind of terminal and its capabilities. The point being, even they were never able to automatically "detect" these things.

我可以告诉你其他人是如何解决这个问题的,但它并不漂亮。如果你以ncurses为例(它需要能够在各种不同的终端上运行),你会发现他们使用终端功能数据库来存储各种终端及其功能。关键是,即使他们从未能够自动“检测”这些东西。

I don't know if there's a cross-platform termcap, but it's probably worth your time to look for it. Even if it's out there though, it may not have your terminal listed and you may have to manually add it.

我不知道是否有跨平台的termcap,但它可能值得您花时间去寻找它。即使它在那里,它可能没有列出你的终端,你可能不得不手动添加它。