summaryrefslogtreecommitdiffstats
path: root/cli_helpers/utils.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-09-06 04:12:52 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-09-06 04:12:52 +0000
commitf42dcf1d6578df945fb52fb24703e88de54ad45b (patch)
treeefd58347c05908c103cfdf8a2b3321c158a7e360 /cli_helpers/utils.py
parentAdding upstream version 2.1.0. (diff)
downloadcli-helpers-upstream/2.2.0.tar.xz
cli-helpers-upstream/2.2.0.zip
Adding upstream version 2.2.0.upstream/2.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'cli_helpers/utils.py')
-rw-r--r--cli_helpers/utils.py34
1 files changed, 22 insertions, 12 deletions
diff --git a/cli_helpers/utils.py b/cli_helpers/utils.py
index f11fa40..3f09cb5 100644
--- a/cli_helpers/utils.py
+++ b/cli_helpers/utils.py
@@ -7,6 +7,7 @@ from functools import lru_cache
from typing import Dict
from typing import TYPE_CHECKING
+
if TYPE_CHECKING:
from pygments.style import StyleMeta
@@ -20,10 +21,16 @@ def bytes_to_string(b):
"""
if isinstance(b, binary_type):
+ needs_hex = False
try:
- return b.decode('utf8')
+ result = b.decode("utf8")
+ needs_hex = not result.isprintable()
except UnicodeDecodeError:
- return '0x' + binascii.hexlify(b).decode('ascii')
+ needs_hex = True
+ if needs_hex:
+ return "0x" + binascii.hexlify(b).decode("ascii")
+ else:
+ return result
return b
@@ -37,16 +44,20 @@ def to_string(value):
def truncate_string(value, max_width=None, skip_multiline_string=True):
"""Truncate string values."""
- if skip_multiline_string and isinstance(value, text_type) and '\n' in value:
+ if skip_multiline_string and isinstance(value, text_type) and "\n" in value:
return value
- elif isinstance(value, text_type) and max_width is not None and len(value) > max_width:
- return value[:max_width-3] + "..."
+ elif (
+ isinstance(value, text_type)
+ and max_width is not None
+ and len(value) > max_width
+ ):
+ return value[: max_width - 3] + "..."
return value
def intlen(n):
"""Find the length of the integer part of a number *n*."""
- pos = n.find('.')
+ pos = n.find(".")
return len(n) if pos < 0 else pos
@@ -61,12 +72,12 @@ def unique_items(seq):
return [x for x in seq if not (x in seen or seen.add(x))]
-_ansi_re = re.compile('\033\\[((?:\\d|;)*)([a-zA-Z])')
+_ansi_re = re.compile("\033\\[((?:\\d|;)*)([a-zA-Z])")
def strip_ansi(value):
"""Strip the ANSI escape sequences from a string."""
- return _ansi_re.sub('', value)
+ return _ansi_re.sub("", value)
def replace(s, replace):
@@ -98,9 +109,8 @@ def filter_style_table(style: "StyleMeta", *relevant_styles: str) -> Dict:
'Token.Output.OddRow': "",
}
"""
- _styles_iter = ((str(key), val) for key, val in getattr(style, 'styles', {}).items())
- _relevant_styles_iter = filter(
- lambda tpl: tpl[0] in relevant_styles,
- _styles_iter
+ _styles_iter = (
+ (str(key), val) for key, val in getattr(style, "styles", {}).items()
)
+ _relevant_styles_iter = filter(lambda tpl: tpl[0] in relevant_styles, _styles_iter)
return {key: val for key, val in _relevant_styles_iter}