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

44 投票
3 回答
16254 浏览
提问于 2025-04-17 02:26

为了检测控制台是否正确使用了 sys.stderrsys.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

现在问题变得更复杂了,因为我在一个IDE(比如PyCharm)中运行这段Python代码。最近,PyCharm增加了对ANSI的支持,但第一个测试失败了:它有 isatty 属性,但这个属性的值是 False

我想修改逻辑,以便能够正确检测输出是否支持ANSI颜色。一个要求是,在任何情况下,当输出被重定向到文件时,我都不应该输出任何内容(对于控制台来说是可以接受的)。

更新

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

3 个回答

7

\x1B[6n 是一个标准的 ANSI 转义码,用来查询用户光标的位置。发送这个代码到标准输出(stdout)后,终端会在标准输入(stdin)中返回 \x1B[{line};{column}R。如果能得到这个结果,就可以认为支持 ANSI 转义码。主要的问题是如何检测这个回复。

Windows

msvcrt.getch 可以用来从标准输入中获取一个字符,而不需要等到按下回车键。结合 msvcrt.kbhit,这个函数可以检测标准输入是否有数据可以读取,这样就能得到在本帖的 带注释的代码 部分找到的代码。

Unix/使用 termios

警告:我没有测试过这个具体的 tty/select/termios 代码,但我知道类似的代码在过去是有效的。 getchkbhit 可以用 tty.setrawselect.select 来实现。因此,我们可以这样定义这些函数:

from termios import TCSADRAIN, tcgetattr, tcsetattr
from select import select
from tty import setraw
from sys import stdin

def getch() -> bytes:
    fd = stdin.fileno()                        # get file descriptor of stdin
    old_settings = tcgetattr(fd)               # save settings (important!)

    try:                                       # setraw accomplishes a few things,
        setraw(fd)                             # such as disabling echo and wait.

        return stdin.read(1).encode()          # consistency with the Windows func
    finally:                                   # means output should be in bytes
        tcsetattr(fd, TCSADRAIN, old_settings) # finally, undo setraw (important!)

def kbhit() -> bool:                           # select.select checks if fds are
    return bool(select([stdin], [], [], 0)[0]) # ready for reading/writing/error

然后可以用下面的代码。

带注释的代码

from sys import stdin, stdout

def isansitty() -> bool:
    """
    The response to \x1B[6n should be \x1B[{line};{column}R according to
    https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797. If this
    doesn't work, then it is unlikely ANSI escape codes are supported.
    """

    while kbhit():                         # clear stdin before sending escape in
        getch()                            # case user accidentally presses a key

    stdout.write("\x1B[6n")                # alt: print(end="\x1b[6n", flush=True)
    stdout.flush()                         # double-buffered stdout needs flush 

    stdin.flush()                          # flush stdin to make sure escape works
    if kbhit():                            # ANSI won't work if stdin is empty
        if ord(getch()) == 27 and kbhit(): # check that response starts with \x1B[
            if getch() == b"[":
                while kbhit():             # read stdin again, to remove the actual
                    getch()                # value returned by the escape

                return stdout.isatty()     # lastly, if stdout is a tty, ANSI works
                                           # so True should be returned. Otherwise,
    return False                           # return False

没有注释的完整代码

如果你需要,这里是原始代码。

from sys import stdin, stdout
from platform import system


if system() == "Windows":
    from msvcrt import getch, kbhit

else:
    from termios import TCSADRAIN, tcgetattr, tcsetattr
    from select import select
    from tty import setraw
    from sys import stdin

    def getch() -> bytes:
        fd = stdin.fileno()
        old_settings = tcgetattr(fd)

        try:
            setraw(fd)

            return stdin.read(1).encode()
        finally:
            tcsetattr(fd, TCSADRAIN, old_settings)

    def kbhit() -> bool:
        return bool(select([stdin], [], [], 0)[0])

def isansitty() -> bool:
    """
    Checks if stdout supports ANSI escape codes and is a tty.
    """

    while kbhit():
        getch()

    stdout.write("\x1b[6n")
    stdout.flush()

    stdin.flush()
    if kbhit():
        if ord(getch()) == 27 and kbhit():
            if getch() == b"[":
                while kbhit():
                    getch()

                return stdout.isatty()

    return False

来源

以下是一些来源,顺序不分先后:

13

我可以告诉你别人是怎么解决这个问题的,但过程并不简单。以ncurses为例(它需要在各种不同的终端上运行),你会发现他们使用了一个终端能力数据库来存储各种终端及其功能。重点是,即使是他们也无法自动“检测”这些东西。

我不知道是否有跨平台的termcap,但你可以花时间去找找看。即使找到了,它可能也没有你使用的终端的信息,你可能还得手动添加。

27

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

if supports_color():
    ...

他们使用的代码是:

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()
    return supported_platform and is_a_tty

可以查看这个链接了解更多信息:https://github.com/django/django/blob/master/django/core/management/color.py

撰写回答