blob: c53b9efb42d46d8bde906fb2a8d18b49ab9b1102 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
"""A text made fit to show: on one line, cut short, or with what does not print written out."""
from __future__ import annotations
def single_line(text: str) -> str:
"""`text` on one line: each run of white space is one space, and none is at either end."""
return " ".join(text.split())
def capped(text: str, cap: int) -> str:
"""`text` trimmed to `cap` characters, the cut marked with how many were dropped."""
return text if len(text) <= cap else f"{text[:cap]}...[+{len(text) - cap} chars]"
def counted(number: int, noun: str) -> str:
"""`number` and `noun`, with an s on the noun unless the number is 1."""
return f"{number} {noun}{'' if number == 1 else 's'}"
def written_out(text: str) -> str:
"""`text` with each character that does not print, other than a tab, written as \\u{hex}."""
if text.replace("\t", "").isprintable():
return text
return "".join(ch if ch.isprintable() or ch == "\t" else f"\\u{{{ord(ch):x}}}" for ch in text)
|