[formatter] implement 'X' format specifier (#5770)

This commit is contained in:
Mike Fährmann
2024-06-21 20:49:52 +02:00
parent b707b31264
commit 8f50c04af2
3 changed files with 32 additions and 1 deletions

View File

@@ -423,6 +423,19 @@ def _parse_sort(format_spec, default):
return sort_asc
def _parse_limit(format_spec, default):
limit, hint, format_spec = format_spec.split(_SEPARATOR, 2)
limit = int(limit[1:])
limit_hint = limit - len(hint)
fmt = _build_format_func(format_spec, default)
def apply_limit(obj):
if len(obj) > limit:
obj = obj[:limit_hint] + hint
return fmt(obj)
return apply_limit
def _default_format(format_spec, default):
def wrap(obj):
return format(obj, format_spec)
@@ -469,9 +482,10 @@ _FORMAT_SPECIFIERS = {
"[": _parse_slice,
"C": _parse_conversion,
"D": _parse_datetime,
"L": _parse_maxlen,
"J": _parse_join,
"L": _parse_maxlen,
"O": _parse_offset,
"R": _parse_replace,
"S": _parse_sort,
"X": _parse_limit,
}