Pretty list to fit the terminal, and add header. :param rtlst: a list of tuples. each tuple contains a value which can be either a string or a list of string. :param sortBy: the column id (starting with 0) which will be used for ordering :param borders: whether to p
(rtlst, # type: List[Tuple[Union[str, List[str]], ...]]
header, # type: List[Tuple[str, ...]]
sortBy=0, # type: Optional[int]
borders=False, # type: bool
)
| 3304 | |
| 3305 | |
| 3306 | def pretty_list(rtlst, # type: List[Tuple[Union[str, List[str]], ...]] |
| 3307 | header, # type: List[Tuple[str, ...]] |
| 3308 | sortBy=0, # type: Optional[int] |
| 3309 | borders=False, # type: bool |
| 3310 | ): |
| 3311 | # type: (...) -> str |
| 3312 | """ |
| 3313 | Pretty list to fit the terminal, and add header. |
| 3314 | |
| 3315 | :param rtlst: a list of tuples. each tuple contains a value which can |
| 3316 | be either a string or a list of string. |
| 3317 | :param sortBy: the column id (starting with 0) which will be used for |
| 3318 | ordering |
| 3319 | :param borders: whether to put borders on the table or not |
| 3320 | """ |
| 3321 | if borders: |
| 3322 | _space = "|" |
| 3323 | else: |
| 3324 | _space = " " |
| 3325 | cols = len(header[0]) |
| 3326 | # Windows has a fat terminal border |
| 3327 | _spacelen = len(_space) * (cols - 1) + int(WINDOWS) |
| 3328 | _croped = False |
| 3329 | if sortBy is not None: |
| 3330 | # Sort correctly |
| 3331 | rtlst.sort(key=lambda x: x[sortBy]) |
| 3332 | # Resolve multi-values |
| 3333 | for i, line in enumerate(rtlst): |
| 3334 | ids = [] # type: List[int] |
| 3335 | values = [] # type: List[Union[str, List[str]]] |
| 3336 | for j, val in enumerate(line): |
| 3337 | if isinstance(val, list): |
| 3338 | ids.append(j) |
| 3339 | values.append(val or " ") |
| 3340 | if values: |
| 3341 | del rtlst[i] |
| 3342 | k = 0 |
| 3343 | for ex_vals in zip_longest(*values, fillvalue=" "): |
| 3344 | if k: |
| 3345 | extra_line = [" "] * cols |
| 3346 | else: |
| 3347 | extra_line = list(line) # type: ignore |
| 3348 | for j, h in enumerate(ids): |
| 3349 | extra_line[h] = ex_vals[j] |
| 3350 | rtlst.insert(i + k, tuple(extra_line)) |
| 3351 | k += 1 |
| 3352 | rtslst = cast(List[Tuple[str, ...]], rtlst) |
| 3353 | # Append tag |
| 3354 | rtslst = header + rtslst |
| 3355 | # Detect column's width |
| 3356 | colwidth = [max(len(y) for y in x) for x in zip(*rtslst)] |
| 3357 | # Make text fit in box (if required) |
| 3358 | width = get_terminal_width() |
| 3359 | if conf.auto_crop_tables and width: |
| 3360 | width = width - _spacelen |
| 3361 | while sum(colwidth) > width: |
| 3362 | _croped = True |
| 3363 | # Needs to be cropped |