Skip to content
34 changes: 28 additions & 6 deletions halo/_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
"""Utilities for Halo library.
"""
import builtins
import codecs
import platform
import locale
import os
import sys
import six
try:
from shutil import get_terminal_size
Expand All @@ -23,14 +26,33 @@ def is_supported():
boolean
Whether operating system supports main symbols or not
"""
possible_encodings = (
# the current, possibly redirected stdout
(sys.stdout.encoding if getattr(builtins, '__IPYTHON__', False) else None),
# the original stdout at the time python started
sys.__stdout__.encoding,
(os.device_encoding(sys.__stdout__.fileno()) if sys.__stdout__.isatty() else None),
locale.getpreferredencoding(),
locale.getpreferredencoding(False),
)

for encoding in possible_encodings:
try:
current_encoding = codecs.lookup(encoding)
except:
continue
else:
break
else:
return False

os_arch = platform.system()

if os_arch != 'Windows':
try:
current_encoding.encode('\u280b')
except UnicodeError:
return False
else:
return True

return False


def get_environment():
"""Get the environment in which halo is running
Expand Down