Skip to content

Commit

Permalink
Fallback to ascii if terminal doesn't support emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
mar10 committed Jul 12, 2020
1 parent ce8eeca commit 1d5be9c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
29 changes: 26 additions & 3 deletions src/yabs/snazzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ class Snazzy:
"""

#: (bool)
_enabled = False
#: (bool) True if Snazzy feature detection was run and initialion finished
_initialized = False
#: (bool) True if `enable(True)` was called
_enabled = False
#: (bool) True if the terminal supports fancy unicode
_support_emoji = None

def __init__(
self, fg=None, bg=None, bold=False, underline=False, italic=False, stream=None
Expand All @@ -183,12 +186,22 @@ def _initialize(cls):
# See https://github.com/feluxe/sty/issues/2
if sys.platform == "win32":
os.system("color")
# TODO: this is a dumb/pessimistic guess.
# The new Windows Terminal for example *will* support emojis, for example
cls._support_emoji = not sys.platform == "win32"

# TODO:
# https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

@classmethod
def enable(cls, flag, force=False):
def enable(cls, flag, force=False, support_emoji=None):
"""Set 'enabled'-status.
Args:
flag (bool):
force (bool):
support_emoji (bool, optional):
"""
if flag and not force and not sys.stdout.isatty():
flag = False

Expand Down Expand Up @@ -276,6 +289,12 @@ def wrap(cls, text, fg=None, bg=None, bold=False, underline=False, italic=False)
text = "".join(str(s) for s in sl)
return text

@classmethod
def emoji(cls, s, fallback="", force=None):
"""Return an emoji-string if the terminal supports it, fallback otherwise."""
enable = cls._support_emoji if force is None else force
return s if enable else fallback

# @classmethod
# def set_cursor(cls, x, y, apply=True):
# # https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
Expand All @@ -295,6 +314,10 @@ def colors_enabled():
return Snazzy.is_enabled()


def emoji(s, fallback="", force=None):
return Snazzy.emoji(s, fallback, force)


def ansi_reset(fg=True, bg=True, bold=True, underline=True, italic=True):
"""Reset color attributes to console default."""
return Snazzy.reset(fg, bg, bold, underline, italic)
Expand Down
10 changes: 4 additions & 6 deletions src/yabs/task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .cmd_push import PushTask
from .cmd_pypi_release import PypiReleaseTask
from .cmd_tag import TagTask
from .snazzy import colors_enabled
from .snazzy import colors_enabled, emoji
from .util import (
NO_DEFAULT,
ConfigError,
Expand Down Expand Up @@ -156,15 +156,13 @@ def run(self):

elap = time.monotonic() - start
if ok:
emoji = " ✨ 🍰 ✨" if colors_enabled() else ""
log_ok(
"Workkflow finished successfully in {}{}".format(
format_elap(elap), emoji
"Workflow finished successfully in {}{}".format(
format_elap(elap), emoji(" ✨ 🍰 ✨")
)
)
else:
emoji = " 💥 💔 💥" if colors_enabled() else ""
msg = "Workkflow failed in {}{}".format(format_elap(elap), emoji)
msg = "Workflow failed in {}{}".format(format_elap(elap), emoji(" 💥 💔 💥"))
log_error(msg)
context.errors.append(msg)

Expand Down
10 changes: 5 additions & 5 deletions src/yabs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pathlib import Path
from shutil import rmtree

from yabs.snazzy import red, green, yellow, gray, Snazzy
from yabs.snazzy import emoji, red, green, yellow, gray, Snazzy

logger = logging.getLogger("yabs")

Expand Down Expand Up @@ -111,12 +111,12 @@ def write(msg, level="info", prefix=False, output=None, output_level=None):
"warning": yellow("WARNING") + ": ",
"error": red("ERROR") + ": ",
},
# Use emoji if terminal supports it, colored ASCII otherwise
"check": {
"info": green(" ✔ "),
"warning": yellow(" ! "),
"error": red(" ✘ "),
"info": " {} ".format(emoji("✅", green(" * "))),
"warning": " {} ".format(emoji("❗", yellow(" ! "))),
"error": " {} ".format(emoji("❌", red(" X "))),
},
# "check": {"info": " ✅ ", "warning": " ❗ ", "error": " ❌ "},
}
if Snazzy._initialized:
_prefix_map_valid = True
Expand Down

0 comments on commit 1d5be9c

Please sign in to comment.