Skip to content

Commit

Permalink
colors to report
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat committed Jan 9, 2019
1 parent 15f8f22 commit 991cfdd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/tox/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class Parser:

def __init__(self):
class HelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
def __init__(self, prog: str) -> None:
super().__init__(prog, max_help_position=35, width=190)
def __init__(self, prog):
super(HelpFormatter, self).__init__(prog, max_help_position=35, width=190)

self.argparser = argparse.ArgumentParser(
description="tox options", add_help=False, prog="tox", formatter_class=HelpFormatter
Expand Down
15 changes: 8 additions & 7 deletions src/tox/config/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@

def auto_detect_cpus():
try:
from os import sched_getaffinity
except ImportError: # pragma: no cov
try: # pragma: no cov
from os import cpu_count # pragma: no cov
except ImportError: # pragma: no cov
from multiprocessing import cpu_count # pragma: no cov
else:
from os import sched_getaffinity # python 3 only

def cpu_count():
return len(sched_getaffinity(0))

except ImportError:
# python 2 options
try:
from os import cpu_count
except ImportError:
from multiprocessing import cpu_count

try:
n = cpu_count()
except NotImplementedError: # pragma: no cov
Expand Down
22 changes: 12 additions & 10 deletions src/tox/util/spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from collections import OrderedDict
from datetime import datetime

import py

threads = []

if os.name == "nt":
Expand All @@ -25,7 +27,8 @@ class Spinner(object):
def __init__(self, enabled=True, refresh_rate=0.1):
self.refresh_rate = refresh_rate
self.enabled = enabled
self.stream = sys.stdout
self._file = sys.stdout
self.stream = py.io.TerminalWriter(file=self._file)
self._envs = OrderedDict()
self._frame_index = 0

Expand Down Expand Up @@ -83,29 +86,30 @@ def add(self, name):
self._envs[name] = datetime.now()

def succeed(self, key):
self.finalize(key, "✔ OK")
self.finalize(key, "✔ OK", green=True)

def fail(self, key):
self.finalize(key, "✖ FAIL")
self.finalize(key, "✖ FAIL", red=True)

def skip(self, key):
self.finalize(key, "⚠ SKIP")
self.finalize(key, "⚠ SKIP", white=True)

def finalize(self, key, status):
def finalize(self, key, status, **kwargs):
start_at = self._envs[key]
del self._envs[key]
if self.enabled:
self.clear()
self.stream.write(
"{} {} in {}{}".format(
status, key, td_human_readable(datetime.now() - start_at), os.linesep
)
),
**kwargs
)
if not self._envs:
self.__exit__(None, None, None)

def disable_cursor(self):
if self.stream.isatty():
if self._file.isatty():
if os.name == "nt":
ci = _CursorInfo()
handle = ctypes.windll.kernel32.GetStdHandle(-11)
Expand All @@ -114,10 +118,9 @@ def disable_cursor(self):
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == "posix":
self.stream.write("\033[?25l")
self.stream.flush()

def enable_cursor(self):
if self.stream.isatty():
if self._file.isatty():
if os.name == "nt":
ci = _CursorInfo()
handle = ctypes.windll.kernel32.GetStdHandle(-11)
Expand All @@ -126,7 +129,6 @@ def enable_cursor(self):
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == "posix":
self.stream.write("\033[?25h")
self.stream.flush()


def td_human_readable(delta):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/util/test_spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def test_spinner_atty(capfd, monkeypatch):

@freeze_time("2012-01-14")
def test_spinner_report(capfd, monkeypatch):
monkeypatch.setattr(sys.stdout, "isatty", lambda: False)
with spinner.Spinner(refresh_rate=100) as spin:
monkeypatch.setattr(spin.stream, "isatty", lambda: False)
spin.stream.write(os.linesep)
spin.add("ok")
spin.add("fail")
Expand All @@ -80,9 +80,9 @@ def test_spinner_report(capfd, monkeypatch):


def test_spinner_long_text(capfd, monkeypatch):
monkeypatch.setattr(sys.stdout, "isatty", lambda: False)
with spinner.Spinner(refresh_rate=100) as spin:
spin.stream.write("\n")
monkeypatch.setattr(spin.stream, "isatty", lambda: False)
spin.add("a" * 60)
spin.add("b" * 60)
spin.render_frame()
Expand Down

0 comments on commit 991cfdd

Please sign in to comment.