Skip to content

Commit

Permalink
Fix color printing for Python 3.11 (#236)
Browse files Browse the repository at this point in the history
Fix for Python 3.11 (not sure what changed, but there were some str and
format-related changes in the enum module). Since 3.11 using
f"{Color.foo}" prints "Color.foo" instead of inserting Color.foo.value .
One solution is to do that everywhere. Here we opt for a
minimally invasive fix and add __str__ to the info.Color enum.

Also remove the str mixin, so replace

    class Color(str, Enum)

by

    class Color(Enum)

Not sure what the str mixin was supposed to solve, but the issue above
occurs with or without it and the version without passes all tests.

Modify test_main.py::TestLsLl.test_ll() to use Color.foo.value .
  • Loading branch information
elcorto authored Jan 31, 2023
1 parent fd6295c commit 6389470
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion gita/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from . import common


class Color(str, Enum):
class Color(Enum):
"""
Terminal color
"""
Expand All @@ -32,6 +32,12 @@ class Color(str, Enum):
b_white = '\x1b[37;1m'
underline = '\x1B[4m'

# Make f"{Color.foo}" expand to Color.foo.value .
#
# See https://stackoverflow.com/a/24487545
def __str__(self):
return f"{self.value}"


default_colors = {
'no-remote': Color.white.name,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ def side_effect(input, _=None):
out, err = capfd.readouterr()
assert err == ""
assert "gita" in out
assert info.Color.end in out
assert info.Color.end.value in out

# no color on branch name
__main__.main(["ll", "-C"])
out, err = capfd.readouterr()
assert err == ""
assert "gita" in out
assert info.Color.end not in out
assert info.Color.end.value not in out

__main__.main(["ls", "gita"])
out, err = capfd.readouterr()
Expand Down

0 comments on commit 6389470

Please sign in to comment.