Add print_table function to qubes.tools

- print_table uses the `column` tool with the ASCII Unit Separator to print a
pretty table
This commit is contained in:
Bahtiar `kalkin-` Gadimov 2016-04-30 17:51:01 +02:00
parent 0484be518c
commit 0319df25e5
No known key found for this signature in database
GPG Key ID: 96ED3C3BA19C3DEE

View File

@ -31,6 +31,7 @@ import argparse
import importlib
import logging
import os
import subprocess
import sys
import textwrap
@ -398,3 +399,17 @@ class VmNameGroup(argparse._MutuallyExclusiveGroup):
default=[]) # the default parameter is important! see
# https://stackoverflow.com/questions/35044288
# and `argparse.ArgumentParser.parse_args()`
def print_table(table):
''' Uses the unix column command to print pretty table.
:param str text: list of lists/sets
'''
unit_separator = chr(31)
cmd = ['column', '-t', '-s', unit_separator]
text_table = '\n'.join([unit_separator.join(row) for row in table])
p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
p.stdin.write(text_table)
p.communicate()