Skip to content

Commit

Permalink
Remove dependence on ansicolor
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren authored Nov 2, 2023
1 parent e7ec249 commit eae85d9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ classifiers=[
dynamic = ["version"]
dependencies=[
"aiohttp",
"ansicolors==1.1.8",
"beartype > 0.11",
"cloudevents>=1.6.0",
"cloudpickle",
Expand Down
31 changes: 14 additions & 17 deletions src/ert/cli/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datetime import datetime, timedelta
from typing import Dict, Iterator, Optional, TextIO, Tuple, Union

from colors import color as ansi_color
from tqdm import tqdm

from ert.ensemble_evaluator import (
Expand All @@ -24,16 +23,17 @@
Color = Tuple[int, int, int]


def _no_color(
s: str,
fg: Optional[Color] = None,
bg: Optional[Color] = None,
style: Optional[Color] = None,
) -> str:
"""Alternate color method when no coloring is wanted. Conforms to the
signature of ansi_color.color, wherein the first positional argument
is the string to be (un-)colored."""
return s
def _no_color(text: str, color: Color) -> str:
"""Alternate color method when no coloring is wanted"""
return text


def ansi_color(text: str, color: Color) -> str:
"""Returns the text as a colorized text for an ansi terminal.
https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit
"""
return "\x1b[38;2;{};{};{}m".format(*color) + text + "\x1b[0m"


class Monitor:
Expand All @@ -45,9 +45,6 @@ class Monitor:
"""

dot = "■ "
empty_bar_char = " "
filled_bar_char = "█"
bar_length = 30

def __init__(self, out: TextIO = sys.stdout, color_always: bool = False) -> None:
self._out = out
Expand Down Expand Up @@ -100,7 +97,7 @@ def _get_legends(self) -> str:
count = aggregate.get(state_, 0)
_countstring = f"{count}/{total_count}"
out = (
f"{self._colorize(self.dot, fg=REAL_STATE_TO_COLOR[state_])}"
f"{self._colorize(self.dot, color=REAL_STATE_TO_COLOR[state_])}"
f"{state_:10} {_countstring:>10}"
)
statuses += f" {out}\n"
Expand All @@ -109,10 +106,10 @@ def _get_legends(self) -> str:
def _print_result(self, failed: bool, failed_message: Optional[str]) -> None:
if failed:
msg = f"Experiment failed with the following error: {failed_message}"
print(self._colorize(msg, fg=COLOR_FAILED), file=self._out)
print(self._colorize(msg, color=COLOR_FAILED), file=self._out)
else:
print(
self._colorize("Experiment completed.", fg=COLOR_FINISHED),
self._colorize("Experiment completed.", color=COLOR_FINISHED),
file=self._out,
)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/cli/test_cli_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_color_always():
out = StringIO() # not a tty, so coloring is automatically disabled
monitor = Monitor(out=out, color_always=True)

assert monitor._colorize("Foo", fg=(255, 0, 0)) == "\x1b[38;2;255;0;0mFoo\x1b[0m"
assert monitor._colorize("Foo", color=(255, 0, 0)) == "\x1b[38;2;255;0;0mFoo\x1b[0m"


def test_legends():
Expand Down

0 comments on commit eae85d9

Please sign in to comment.