mirror of https://gitlab.com/ita1024/waf.git
New option --color to control the coloring, and split the coloring from the output synchronization code
This commit is contained in:
parent
fa5f149775
commit
4107a0e29d
|
@ -7,51 +7,18 @@ logging, colors, terminal width and pretty-print
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os, re, traceback, sys
|
import os, re, traceback, sys
|
||||||
|
from waflib import ansiterm
|
||||||
|
|
||||||
_nocolor = os.environ.get('NOCOLOR', 'no') not in ('no', '0', 'false')
|
if not os.environ.get('NOSYNC', False):
|
||||||
try:
|
# synchronized output is nearly mandatory to prevent garbled output
|
||||||
if not _nocolor:
|
if sys.stdout.isatty() and id(sys.stdout) == id(sys.__stdout__):
|
||||||
import waflib.ansiterm
|
sys.stdout = ansiterm.AnsiTerm(sys.stdout)
|
||||||
except ImportError:
|
os.environ['TERM'] = 'vt100' # <- not sure about this
|
||||||
pass
|
if sys.stderr.isatty() and id(sys.stderr) == id(sys.__stderr__):
|
||||||
|
sys.stderr = ansiterm.AnsiTerm(sys.stderr)
|
||||||
|
os.environ['TERM'] = 'vt100' # <- not sure about this
|
||||||
|
|
||||||
try:
|
import logging # the logging module keeps holds reference on sys.stderr
|
||||||
import threading
|
|
||||||
except ImportError:
|
|
||||||
if not 'JOBS' in os.environ:
|
|
||||||
# no threading :-(
|
|
||||||
os.environ['JOBS'] = '1'
|
|
||||||
else:
|
|
||||||
wlock = threading.Lock()
|
|
||||||
|
|
||||||
class sync_stream(object):
|
|
||||||
def __init__(self, stream):
|
|
||||||
self.stream = stream
|
|
||||||
self.encoding = self.stream.encoding
|
|
||||||
|
|
||||||
def write(self, txt):
|
|
||||||
try:
|
|
||||||
wlock.acquire()
|
|
||||||
self.stream.write(txt)
|
|
||||||
self.stream.flush()
|
|
||||||
finally:
|
|
||||||
wlock.release()
|
|
||||||
|
|
||||||
def fileno(self):
|
|
||||||
return self.stream.fileno()
|
|
||||||
|
|
||||||
def flush(self):
|
|
||||||
self.stream.flush()
|
|
||||||
|
|
||||||
def isatty(self):
|
|
||||||
return self.stream.isatty()
|
|
||||||
|
|
||||||
if not os.environ.get('NOSYNC', False):
|
|
||||||
if id(sys.stdout) == id(sys.__stdout__):
|
|
||||||
sys.stdout = sync_stream(sys.stdout)
|
|
||||||
sys.stderr = sync_stream(sys.stderr)
|
|
||||||
|
|
||||||
import logging # import other modules only after
|
|
||||||
|
|
||||||
LOG_FORMAT = "%(asctime)s %(c1)s%(zone)s%(c2)s %(message)s"
|
LOG_FORMAT = "%(asctime)s %(c1)s%(zone)s%(c2)s %(message)s"
|
||||||
HOUR_FORMAT = "%H:%M:%S"
|
HOUR_FORMAT = "%H:%M:%S"
|
||||||
|
@ -73,15 +40,17 @@ colors_lst = {
|
||||||
'cursor_off' :'\x1b[?25l',
|
'cursor_off' :'\x1b[?25l',
|
||||||
}
|
}
|
||||||
|
|
||||||
got_tty = not os.environ.get('TERM', 'dumb') in ['dumb', 'emacs']
|
def enable_colors(level):
|
||||||
if got_tty:
|
if level == 0:
|
||||||
try:
|
colors_lst['USE'] = False
|
||||||
got_tty = sys.stderr.isatty() and sys.stdout.isatty()
|
elif level == 1:
|
||||||
except AttributeError:
|
# and here we guess
|
||||||
got_tty = False
|
term = os.environ.get('TERM', '')
|
||||||
|
if term in ['dumb', 'emacs']:
|
||||||
if (not got_tty and os.environ.get('TERM', 'dumb') != 'msys') or _nocolor:
|
colors_lst['USE'] = False
|
||||||
colors_lst['USE'] = False
|
pass
|
||||||
|
else:
|
||||||
|
colors_lst['USE'] = True
|
||||||
|
|
||||||
def get_term_cols():
|
def get_term_cols():
|
||||||
return 80
|
return 80
|
||||||
|
|
|
@ -14,6 +14,13 @@ as well as custom ones, used by the ``options`` wscript function.
|
||||||
import os, tempfile, optparse, sys, re
|
import os, tempfile, optparse, sys, re
|
||||||
from waflib import Logs, Utils, Context
|
from waflib import Logs, Utils, Context
|
||||||
|
|
||||||
|
try:
|
||||||
|
import threading
|
||||||
|
except ImportError:
|
||||||
|
if not 'JOBS' in os.environ:
|
||||||
|
# no threading :-(
|
||||||
|
os.environ['JOBS'] = '1'
|
||||||
|
|
||||||
cmds = 'distclean configure build install clean uninstall check dist distcheck'.split()
|
cmds = 'distclean configure build install clean uninstall check dist distcheck'.split()
|
||||||
"""
|
"""
|
||||||
Constant representing the default waf commands displayed in::
|
Constant representing the default waf commands displayed in::
|
||||||
|
@ -51,6 +58,7 @@ class opt_parser(optparse.OptionParser):
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
|
|
||||||
jobs = ctx.jobs()
|
jobs = ctx.jobs()
|
||||||
|
p('-c', '--color', dest='colors', default='auto', action='store', help='whether to use colors (always/auto/never) [default: auto]')
|
||||||
p('-j', '--jobs', dest='jobs', default=jobs, type='int', help='amount of parallel jobs (%r)' % jobs)
|
p('-j', '--jobs', dest='jobs', default=jobs, type='int', help='amount of parallel jobs (%r)' % jobs)
|
||||||
p('-k', '--keep', dest='keep', default=0, action='count', help='keep running happily even if errors are found')
|
p('-k', '--keep', dest='keep', default=0, action='count', help='keep running happily even if errors are found')
|
||||||
p('-v', '--verbose', dest='verbose', default=0, action='count', help='verbosity level -v -vv or -vvv [default: 0]')
|
p('-v', '--verbose', dest='verbose', default=0, action='count', help='verbosity level -v -vv or -vvv [default: 0]')
|
||||||
|
@ -242,6 +250,15 @@ class OptionsContext(Context.Context):
|
||||||
if options.verbose >= 1:
|
if options.verbose >= 1:
|
||||||
self.load('errcheck')
|
self.load('errcheck')
|
||||||
|
|
||||||
|
try:
|
||||||
|
colors = {'always' : 2, 'auto' : 1, 'never' : 0}[options.colors]
|
||||||
|
except KeyError:
|
||||||
|
self.parser.error('Bad option "%s" for --color option' % options.colors)
|
||||||
|
else:
|
||||||
|
if os.environ.get('NOCOLOR', ''):
|
||||||
|
colors = 0
|
||||||
|
Logs.enable_colors(colors)
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
"""
|
"""
|
||||||
See :py:func:`waflib.Context.Context.execute`
|
See :py:func:`waflib.Context.Context.execute`
|
||||||
|
|
|
@ -11,12 +11,35 @@ console commands.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys, os, re, threading
|
import re
|
||||||
|
from waflib.Utils import threading
|
||||||
|
|
||||||
|
wlock = threading.Lock()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from ctypes import Structure, windll, c_short, c_ulong, c_int, byref, c_wchar
|
from ctypes import Structure, windll, c_short, c_ulong, c_int, byref, c_wchar
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
class AnsiTerm(object):
|
||||||
|
def __init__(self, stream):
|
||||||
|
self.stream = stream
|
||||||
|
self.encoding = self.stream.encoding
|
||||||
|
|
||||||
|
def write(self, txt):
|
||||||
|
try:
|
||||||
|
wlock.acquire()
|
||||||
|
self.stream.write(txt)
|
||||||
|
self.stream.flush()
|
||||||
|
finally:
|
||||||
|
wlock.release()
|
||||||
|
|
||||||
|
def fileno(self):
|
||||||
|
return self.stream.fileno()
|
||||||
|
|
||||||
|
def flush(self):
|
||||||
|
self.stream.flush()
|
||||||
|
|
||||||
|
def isatty(self):
|
||||||
|
return self.stream.isatty()
|
||||||
else:
|
else:
|
||||||
|
|
||||||
class COORD(Structure):
|
class COORD(Structure):
|
||||||
|
@ -37,7 +60,6 @@ else:
|
||||||
_type = str
|
_type = str
|
||||||
|
|
||||||
to_int = lambda number, default: number and int(number) or default
|
to_int = lambda number, default: number and int(number) or default
|
||||||
wlock = threading.Lock()
|
|
||||||
|
|
||||||
STD_OUTPUT_HANDLE = -11
|
STD_OUTPUT_HANDLE = -11
|
||||||
STD_ERROR_HANDLE = -12
|
STD_ERROR_HANDLE = -12
|
||||||
|
@ -230,6 +252,8 @@ else:
|
||||||
else:
|
else:
|
||||||
self.writeconsole(txt)
|
self.writeconsole(txt)
|
||||||
else:
|
else:
|
||||||
|
# no support for colors in the console, just output the text:
|
||||||
|
# eclipse or msys may be able to interpret the escape sequences
|
||||||
self.stream.write(text)
|
self.stream.write(text)
|
||||||
finally:
|
finally:
|
||||||
wlock.release()
|
wlock.release()
|
||||||
|
@ -265,8 +289,4 @@ else:
|
||||||
def isatty(self):
|
def isatty(self):
|
||||||
return self._isatty
|
return self._isatty
|
||||||
|
|
||||||
if sys.stdout.isatty() or sys.stderr.isatty():
|
|
||||||
sys.stderr = AnsiTerm(sys.stderr)
|
|
||||||
sys.stdout = AnsiTerm(sys.stdout)
|
|
||||||
os.environ['TERM'] = 'vt100'
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue