summaryrefslogtreecommitdiff
path: root/sphinx/util/console.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/util/console.py')
-rw-r--r--sphinx/util/console.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/sphinx/util/console.py b/sphinx/util/console.py
index a9b7ee55d..acba52a9f 100644
--- a/sphinx/util/console.py
+++ b/sphinx/util/console.py
@@ -22,23 +22,26 @@ except ImportError:
_ansi_re = re.compile('\x1b\\[(\\d\\d;){0,2}\\d\\dm')
codes = {}
+
def get_terminal_width():
"""Borrowed from the py lib."""
try:
- import termios, fcntl, struct
+ 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 (SystemExit, KeyboardInterrupt):
- raise
- except:
+ 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
@@ -47,6 +50,7 @@ def term_width_line(text):
# 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()
@@ -68,15 +72,19 @@ def nocolor():
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)