[string] -> [padded_string]
(
strings,
alignment,
minwidth=0,
has_invisible=True,
enable_widechars=False,
is_multiline=False,
preserve_whitespace=False,
)
| 1224 | |
| 1225 | |
| 1226 | def _align_column( |
| 1227 | strings, |
| 1228 | alignment, |
| 1229 | minwidth=0, |
| 1230 | has_invisible=True, |
| 1231 | enable_widechars=False, |
| 1232 | is_multiline=False, |
| 1233 | preserve_whitespace=False, |
| 1234 | ): |
| 1235 | """[string] -> [padded_string]""" |
| 1236 | strings, padfn = _align_column_choose_padfn( |
| 1237 | strings, alignment, has_invisible, preserve_whitespace |
| 1238 | ) |
| 1239 | width_fn = _align_column_choose_width_fn( |
| 1240 | has_invisible, enable_widechars, is_multiline |
| 1241 | ) |
| 1242 | |
| 1243 | s_widths = list(map(width_fn, strings)) |
| 1244 | maxwidth = max(max(_flat_list(s_widths)), minwidth) |
| 1245 | # TODO: refactor column alignment in single-line and multiline modes |
| 1246 | if is_multiline: |
| 1247 | if not enable_widechars and not has_invisible: |
| 1248 | padded_strings = [ |
| 1249 | "\n".join([padfn(maxwidth, s) for s in ms.splitlines()]) |
| 1250 | for ms in strings |
| 1251 | ] |
| 1252 | else: |
| 1253 | # enable wide-character width corrections |
| 1254 | s_lens = [[len(s) for s in re.split("[\r\n]", ms)] for ms in strings] |
| 1255 | visible_widths = [ |
| 1256 | [maxwidth - (w - l) for w, l in zip(mw, ml)] |
| 1257 | for mw, ml in zip(s_widths, s_lens) |
| 1258 | ] |
| 1259 | # wcswidth and _visible_width don't count invisible characters; |
| 1260 | # padfn doesn't need to apply another correction |
| 1261 | padded_strings = [ |
| 1262 | "\n".join([padfn(w, s) for s, w in zip((ms.splitlines() or ms), mw)]) |
| 1263 | for ms, mw in zip(strings, visible_widths) |
| 1264 | ] |
| 1265 | else: # single-line cell values |
| 1266 | if not enable_widechars and not has_invisible: |
| 1267 | padded_strings = [padfn(maxwidth, s) for s in strings] |
| 1268 | else: |
| 1269 | # enable wide-character width corrections |
| 1270 | s_lens = list(map(len, strings)) |
| 1271 | visible_widths = [maxwidth - (w - l) for w, l in zip(s_widths, s_lens)] |
| 1272 | # wcswidth and _visible_width don't count invisible characters; |
| 1273 | # padfn doesn't need to apply another correction |
| 1274 | padded_strings = [padfn(w, s) for s, w in zip(strings, visible_widths)] |
| 1275 | return padded_strings |
| 1276 | |
| 1277 | |
| 1278 | def _more_generic(type1, type2): |
no test coverage detected