Skip to content

Instantly share code, notes, and snippets.

@shimizukawa
Last active January 24, 2017 07:12
Show Gist options
  • Save shimizukawa/6560350301847813c1e37487c6b79a03 to your computer and use it in GitHub Desktop.
Save shimizukawa/6560350301847813c1e37487c6b79a03 to your computer and use it in GitHub Desktop.
checker for sphinx colorize console
# -*- coding: utf-8 -*-
#----- copy of sphinx/util/console.py -----
"""
sphinx.util.console
~~~~~~~~~~~~~~~~~~~
Format colored console output.
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import print_function
import os
import sys
import re
try:
# check if colorama is installed to support color on Windows
import colorama
except ImportError:
colorama = None
_ansi_re = re.compile('\x1b\\[(\\d\\d;){0,2}\\d\\dm')
codes = {}
def get_terminal_width():
"""Borrowed from the py lib."""
try:
import termios
import fcntl
import struct
call = fcntl.ioctl(0, termios.TIOCGWINSZ,
struct.pack('hhhh', 0, 0, 0, 0))
height, width = struct.unpack('hhhh', call)[:2]
terminal_width = width
except Exception:
# FALLBACK
terminal_width = int(os.environ.get('COLUMNS', 80)) - 1
return terminal_width
_tw = get_terminal_width()
def term_width_line(text):
if not codes:
# if no coloring, don't output fancy backspaces
return text + '\n'
else:
# codes are not displayed, this must be taken into account
return text.ljust(_tw + len(text) - len(_ansi_re.sub('', text))) + '\r'
def color_terminal():
if sys.platform == 'win32' and colorama is not None:
colorama.init()
return True
if not hasattr(sys.stdout, 'isatty'):
return False
if not sys.stdout.isatty():
return False
if 'COLORTERM' in os.environ:
return True
term = os.environ.get('TERM', 'dumb').lower()
if term in ('xterm', 'linux') or 'color' in term:
return True
return False
def nocolor():
if sys.platform == 'win32' and colorama is not None:
colorama.deinit()
codes.clear()
def coloron():
codes.update(_orig_codes)
def colorize(name, text):
return codes.get(name, '') + text + codes.get('reset', '')
def strip_colors(s):
return re.compile('\x1b.*?m').sub('', s)
def create_color_func(name):
def inner(text):
return colorize(name, text)
globals()[name] = inner
_attrs = {
'reset': '39;49;00m',
'bold': '01m',
'faint': '02m',
'standout': '03m',
'underline': '04m',
'blink': '05m',
}
for _name, _value in _attrs.items():
codes[_name] = '\x1b[' + _value
_colors = [
('black', 'darkgray'),
('darkred', 'red'),
('darkgreen', 'green'),
('brown', 'yellow'),
('darkblue', 'blue'),
('purple', 'fuchsia'),
('turquoise', 'teal'),
('lightgray', 'white'),
]
for i, (dark, light) in enumerate(_colors):
codes[dark] = '\x1b[%im' % (i+30)
codes[light] = '\x1b[%i;01m' % (i+30)
_orig_codes = codes.copy()
for _name in codes:
create_color_func(_name)
#-----------------------------------
# check code
import platform
print(sys.version)
print(platform.platform())
try:
import readline
if readline.__doc__ and 'libedit' in readline.__doc__:
print("use libedit")
else:
print("use readline")
except ImportError:
print("no libedit and readline")
if sys.platform == 'win32' and colorama is not None:
print("colorama enabled")
if not hasattr(sys.stdout, 'isatty'):
print("isatty doesn't exist")
else:
print("isatty()", sys.stdout.isatty())
print("COLORTERM in ENV", 'COLORTERM' in os.environ)
term = os.environ.get('TERM', 'dumb').lower()
print("term in ('xterm', 'linux')", term in ('xterm', 'linux'))
print("'color' in term", 'color' in term)
# sphinx/quickstart.py:542
if not color_terminal():
nocolor()
print('COLOR TERMINAL: Off')
else:
print('COLOR TERMINAL: On')
print('----- coloring test -----')
# sphinx/quickstart.py:219
print(bold('Welcome to the Sphinx coloring test utility %s.') % '1.5-util-2')
print('''
Please report a capture of this screen if console is broken.
''')
print('Normal color text')
print(red('Red color text'))
print(green('Green color text'))
print('Normal color text')
term_input = input
try:
term_input = raw_input # py2
except NameError:
pass
while True:
value = term_input(purple('Please enter your favorite color (blank to exit): '))
if not value:
break
print(colorize(value, value))
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment