From 0319df25e56c7c96cdedd18f3c52263b954bfdcd Mon Sep 17 00:00:00 2001 From: Bahtiar `kalkin-` Gadimov Date: Sat, 30 Apr 2016 17:51:01 +0200 Subject: [PATCH] Add print_table function to qubes.tools - print_table uses the `column` tool with the ASCII Unit Separator to print a pretty table --- qubes/tools/__init__.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/qubes/tools/__init__.py b/qubes/tools/__init__.py index 81d7b27e..a546c005 100644 --- a/qubes/tools/__init__.py +++ b/qubes/tools/__init__.py @@ -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()