Created
February 9, 2021 08:44
-
-
Save Jackbennett/20735d525c1f76b791b513a002556903 to your computer and use it in GitHub Desktop.
cli_ui info_table quick note to handle header = keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def info_table( | |
data: Any, *, headers: Union[str, Sequence[str]] = (), fileobj: FileObj = sys.stdout | |
) -> None: | |
if headers == 'keys': | |
colored_data = dict() | |
plain_data = dict() | |
for row in data: | |
colored_row = list() | |
plain_row = list() | |
for item in data[row]: | |
colored_str, plain_str = process_tokens(item, end="") | |
colored_row.append(colored_str) | |
plain_row.append(plain_str) | |
colored_data[row] = colored_row | |
plain_data[row] = plain_row | |
else: | |
colored_data = list() | |
plain_data = list() | |
for row in data: | |
colored_row = list() | |
plain_row = list() | |
for item in row: | |
colored_str, plain_str = process_tokens(item, end="") | |
colored_row.append(colored_str) | |
plain_row.append(plain_str) | |
colored_data.append(colored_row) | |
plain_data.append(plain_row) | |
if config_color(fileobj): | |
data_for_tabulate = colored_data | |
else: | |
data_for_tabulate = plain_data | |
res = tabulate.tabulate(data_for_tabulate, headers=headers) | |
res += "\n" | |
write_and_flush(fileobj, res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment