Skip to content

Commit 405f88e

Browse files
committed
python: support W presentation type
Problem: When outputting wide characters (e.g. emojis) in flux-jobs, the alignment of output can be poor due to the characters having different output widths. Solution: Add a new W presentation type that can adjust formating of the form "(<|>)N", e.g. {id.emoji:>12W}. The output width will be adjusted given the number of wide characters that exist in the string. Note that this presentation type does not help for all output scenarios, as it depends on the width of the output and width of the alignment, but it definitely helps in some scenarios.
1 parent e57c9cf commit 405f88e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/bindings/python/flux/util.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from typing import Mapping
3030

3131
import yaml
32+
import unicodedata
3233

3334
# tomllib added to standard library in Python 3.11
3435
# flux-core minimum is Python 3.6.
@@ -448,6 +449,24 @@ def format_field(self, value, spec):
448449
basecases = empty_outputs()
449450
value = "-" if str(value) in basecases else str(value)
450451
spec = spec[:-1] + "s"
452+
453+
if spec.endswith("W") and isinstance (value, str):
454+
match = re.search(r"^([<>])(\d+)W", spec)
455+
if match:
456+
align = match[1]
457+
width = int(match[2])
458+
widecount = 0
459+
for chr in value:
460+
if unicodedata.east_asian_width(chr) == 'W':
461+
widecount += 1
462+
if width > widecount:
463+
width -= widecount;
464+
spec = f"{align}{width}s"
465+
# if spec was not modified above, need to convert to "W"
466+
# conversion to "s"
467+
if spec.endswith("W"):
468+
spec = spec[:-1] + "s"
469+
451470
retval = super().format_field(value, spec)
452471

453472
if denote_truncation and len(retval) < len(str(value)):

0 commit comments

Comments
 (0)