mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
don't try to detect locale, just asciify on exception
This commit is contained in:
50
jc/cli.py
50
jc/cli.py
@ -4,7 +4,6 @@ JC cli module
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import locale
|
|
||||||
import textwrap
|
import textwrap
|
||||||
import signal
|
import signal
|
||||||
import shlex
|
import shlex
|
||||||
@ -31,13 +30,6 @@ except Exception:
|
|||||||
|
|
||||||
JC_ERROR_EXIT = 100
|
JC_ERROR_EXIT = 100
|
||||||
|
|
||||||
try:
|
|
||||||
UTF_8_SUPPORT = bool(locale.getlocale()[1] == 'UTF-8')
|
|
||||||
except Exception:
|
|
||||||
UTF_8_SUPPORT = False
|
|
||||||
|
|
||||||
CPR = '©' if UTF_8_SUPPORT else '(c)'
|
|
||||||
|
|
||||||
|
|
||||||
class info():
|
class info():
|
||||||
version = __version__
|
version = __version__
|
||||||
@ -45,7 +37,7 @@ class info():
|
|||||||
author = 'Kelly Brazil'
|
author = 'Kelly Brazil'
|
||||||
author_email = 'kellyjonbrazil@gmail.com'
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
website = 'https://github.com/kellyjonbrazil/jc'
|
website = 'https://github.com/kellyjonbrazil/jc'
|
||||||
copyright = f'{CPR} 2019-2022 Kelly Brazil'
|
copyright = f'© 2019-2022 Kelly Brazil'
|
||||||
license = 'MIT License'
|
license = 'MIT License'
|
||||||
|
|
||||||
|
|
||||||
@ -92,6 +84,16 @@ if PYGMENTS_INSTALLED:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def asciify(string):
|
||||||
|
"""
|
||||||
|
Return a string downgraded from Unicode to ASCII with some simple
|
||||||
|
conversions.
|
||||||
|
"""
|
||||||
|
string = string.replace('©', '(c)')
|
||||||
|
string = ascii(string)
|
||||||
|
return string.replace(r'\n', '\n')
|
||||||
|
|
||||||
|
|
||||||
def set_env_colors(env_colors=None):
|
def set_env_colors(env_colors=None):
|
||||||
"""
|
"""
|
||||||
Return a dictionary to be used in Pygments custom style class.
|
Return a dictionary to be used in Pygments custom style class.
|
||||||
@ -277,7 +279,6 @@ def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False):
|
|||||||
"""
|
"""
|
||||||
separators = (',', ':')
|
separators = (',', ':')
|
||||||
indent = None
|
indent = None
|
||||||
ascii = False if UTF_8_SUPPORT else True
|
|
||||||
|
|
||||||
if pretty:
|
if pretty:
|
||||||
separators = None
|
separators = None
|
||||||
@ -288,10 +289,23 @@ def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False):
|
|||||||
class JcStyle(Style):
|
class JcStyle(Style):
|
||||||
styles = set_env_colors(env_colors)
|
styles = set_env_colors(env_colors)
|
||||||
|
|
||||||
return str(highlight(json.dumps(data, indent=indent, separators=separators, ensure_ascii=ascii),
|
try:
|
||||||
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
return str(highlight(json.dumps(data,
|
||||||
|
indent=indent,
|
||||||
|
separators=separators,
|
||||||
|
ensure_ascii=False),
|
||||||
|
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
return str(highlight(json.dumps(data,
|
||||||
|
indent=indent,
|
||||||
|
separators=separators,
|
||||||
|
ensure_ascii=True),
|
||||||
|
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
||||||
|
|
||||||
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=ascii)
|
try:
|
||||||
|
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=False)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=True)
|
||||||
|
|
||||||
|
|
||||||
def magic_parser(args):
|
def magic_parser(args):
|
||||||
@ -437,11 +451,17 @@ def main():
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if help_me:
|
if help_me:
|
||||||
print(help_doc(sys.argv))
|
try:
|
||||||
|
print(help_doc(sys.argv))
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
print(asciify(help_doc(sys.argv)))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if version_info:
|
if version_info:
|
||||||
print(versiontext())
|
try:
|
||||||
|
print(versiontext())
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
print(asciify(versiontext()))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# if magic syntax used, try to run the command and error if it's not found, etc.
|
# if magic syntax used, try to run the command and error if it's not found, etc.
|
||||||
|
Reference in New Issue
Block a user