summaryrefslogtreecommitdiff
path: root/text_utils.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-04-23 17:17:14 -0700
committerScott Gasch <[email protected]>2021-04-23 17:17:14 -0700
commit7accf1acb956f55e875b863c53989fd1950e4b5b (patch)
treebfe0e0aa862d53f4f8522ec26faf11252bd63b3d /text_utils.py
parentd042ee0140760e231f6ccdce3026c9563f43b753 (diff)
Columnify text.
Diffstat (limited to 'text_utils.py')
-rw-r--r--text_utils.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/text_utils.py b/text_utils.py
index a71a99d..3b7a43e 100644
--- a/text_utils.py
+++ b/text_utils.py
@@ -2,6 +2,7 @@
"""Utilities for dealing with "text"."""
+from collections import defaultdict
import math
import sys
from typing import List, NamedTuple
@@ -151,3 +152,18 @@ def justify_text(text: str, *, width: int = 80, alignment: str = "c") -> str:
if len(line) > 0:
retval += "\n" + line[1:]
return retval[1:]
+
+
+def generate_padded_columns(text: List[str]) -> str:
+ max_width = defaultdict(int)
+ for line in text:
+ for pos, word in enumerate(line.split()):
+ max_width[pos] = max(max_width[pos], len(word))
+
+ for line in text:
+ out = ""
+ for pos, word in enumerate(line.split()):
+ width = max_width[pos]
+ word = justify_string(word, width=width, alignment='l')
+ out += f'{word} '
+ yield out