projects
/
python_utils.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
d042ee0
)
Columnify text.
author
Scott Gasch
<
[email protected]
>
Sat, 24 Apr 2021 00:17:14 +0000
(17:17 -0700)
committer
Scott Gasch
<
[email protected]
>
Sat, 24 Apr 2021 00:17:14 +0000
(17:17 -0700)
text_utils.py
patch
|
blob
|
history
diff --git
a/text_utils.py
b/text_utils.py
index a71a99dff943df0058cf080100c7feeedf97a6ed..3b7a43ee4417409094e9f9c6504d4032796cace7 100644
(file)
--- a/
text_utils.py
+++ b/
text_utils.py
@@
-2,6
+2,7
@@
"""Utilities for dealing with "text"."""
"""Utilities for dealing with "text"."""
+from collections import defaultdict
import math
import sys
from typing import List, NamedTuple
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:]
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