Skip to content

Instantly share code, notes, and snippets.

Created October 26, 2011 16:18
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
ANSI support detection code for Python.
#!/usr/bin/env python
import sys, os, time, platform
sample_ansi = '\x1b[31mRED' + '\x1b[33mYELLOW' + '\x1b[32mGREEN' + '\x1b[35mPINK' + '\x1b[0m' + '\n'
for handle in [sys.stdout, sys.stderr]:
if (hasattr(handle, "isatty") and handle.isatty()) or \
('TERM' in os.environ and os.environ['TERM']=='ANSI'):
if platform.system()=='Windows' and not ('TERM' in os.environ and os.environ['TERM']=='ANSI'):
handle.write("Windows console, no ANSI support.\n")
handle.write("ANSI output enabled.\n")
handle.write("ANSI output disabled.\n")
Copy link

Et7f3 commented Mar 29, 2020

It is not enough
image on windows since a certain version of windows 10 the C function GetConsoleMode can give you this info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment