Skip to content

Commit 802acd0

Browse files
author
boonhapus
committed
🐛 implement same table style in interactive tql
1 parent 027a82d commit 802acd0

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

cs_tools/cli/tools/rtql/interactive.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44

55
from rich.console import Console
6+
import rich
67
import httpx
78
import typer
89

@@ -15,6 +16,16 @@
1516
log = logging.getLogger(__name__)
1617

1718

19+
def _to_table(headers, rows=None):
20+
header = [column['name'] for column in headers]
21+
22+
if rows is None:
23+
rows = [{'v': (' ',) * len(header)}]
24+
25+
data = [dict(zip(header, row['v'])) for row in rows]
26+
return data
27+
28+
1829
class InteractiveTQL:
1930
"""
2031
An interactive TQL client.
@@ -150,6 +161,11 @@ def _handle_query(self, lines: List[str]) -> None:
150161
"""
151162
152163
"""
164+
color_map = {
165+
'INFO': '[white]',
166+
'WARNING': '[yellow]',
167+
'ERROR': '[red]'
168+
}
153169
new_ctx = {}
154170

155171
for line in lines:
@@ -172,12 +188,22 @@ def _handle_query(self, lines: List[str]) -> None:
172188
continue
173189

174190
if 'message' in data['result']:
175-
msg = self.ts.api.ts_dataservice._parse_api_messages(data['result']['message'])
176-
self.print(msg)
191+
for message in data['result']['message']:
192+
c = color_map.get(message['type'], '[yellow]')
193+
m = message['value']
194+
195+
if m.strip() == 'Statement executed successfully.':
196+
c = '[bold green]'
197+
if m.strip().endswith(';'):
198+
c = '[cyan]'
199+
200+
self.print(c + m + '[/]', end='')
177201

178202
if 'table' in data['result']:
179-
msg = self.ts.api.ts_dataservice._parse_tql_query(data['result']['table'])
180-
self.print(msg)
203+
d = _to_table(**data['result']['table'])
204+
t = rich.table.Table(*d[0].keys(), box=rich.box.HORIZONTALS)
205+
[t.add_row(*_.values()) for _ in d]
206+
self.print(t)
181207

182208
return new_ctx
183209

0 commit comments

Comments
 (0)