From ae71285bebf0702e919b4ed3ab9bd7c3e9d8f1d6 Mon Sep 17 00:00:00 2001 From: Bridget Date: Tue, 14 Apr 2020 01:22:31 +0200 Subject: [PATCH] Render list of dicts as table --- lslsh.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lslsh.py b/lslsh.py index e107383..d2135b2 100755 --- a/lslsh.py +++ b/lslsh.py @@ -9,6 +9,7 @@ from typing import Dict, List import requests from colorama import Back, Fore, Style, deinit, init # type: ignore +from tabulate import tabulate from urllib3.connectionpool import InsecureRequestWarning # type: ignore from lib import ErrorReceived, connect, disconnect, get_available_commands, send_cmd @@ -52,6 +53,13 @@ class Shell(cmd.Cmd): except Exception as e: return f"{Fore.RED}Error{Fore.RESET}: {e}" + # Render as table if list of dicts + if isinstance(result, list) and isinstance(result[0], dict): + rows = [] + for item in result: + rows.append(list(item.keys()) + list(item.values())) + return tabulate(rows, tablefmt="plain") + return result def do_connect(self, url):