Render list of dicts as table
This commit is contained in:
8
lslsh.py
8
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):
|
||||
|
||||
Reference in New Issue
Block a user