Merge pull request #34 from ltworf/colourstr

Pretty printing
This commit is contained in:
Salvo 'LtWorf' Tomaselli 2020-08-26 21:22:22 +02:00 committed by GitHub
commit 8270e6517c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 65 deletions

View File

@ -181,8 +181,8 @@ def run_py_test(testname):
print(colorize('ERROR', COLOR_RED)) print(colorize('ERROR', COLOR_RED))
print(colorize('=====================================', COLOR_RED)) print(colorize('=====================================', COLOR_RED))
print ("Expected %s" % exp_result) print("Expected %s" % exp_result.pretty_string(tty=True))
print ("Got %s" % result) print("Got %s" % result.pretty_string(tty=True))
print(colorize('=====================================', COLOR_RED)) print(colorize('=====================================', COLOR_RED))
return False return False
@ -265,11 +265,11 @@ def run_test(testname):
print("Optimized query: %s -> %s" % (o_query, o_expr)) print("Optimized query: %s -> %s" % (o_query, o_expr))
print(colorize('=====================================', COLOR_RED)) print(colorize('=====================================', COLOR_RED))
print(colorize("Expected result", COLOR_GREEN)) print(colorize("Expected result", COLOR_GREEN))
print (result_rel) print(result_rel.pretty_string(tty=True))
print(colorize("Result", COLOR_RED)) print(colorize("Result", COLOR_RED))
print (result) print(result.pretty_string(tty=True))
print(colorize("Optimized result", COLOR_RED)) print(colorize("Optimized result", COLOR_RED))
print (o_result) print(o_result.pretty_string(tty=True))
print(colorize("optimized result match %s" % print(colorize("optimized result match %s" %
str(result_rel == o_result), COLOR_MAGENTA)) str(result_rel == o_result), COLOR_MAGENTA))
print(colorize("result match %s" % print(colorize("result match %s" %

View File

@ -412,22 +412,49 @@ class Relation:
def __len__(self): def __len__(self):
return len(self.content) return len(self.content)
def __str__(self): def __str__(self) -> str:
m_len = [len(i) for i in self.header] # Maximum lenght string return self.pretty_string(tty=False)
def pretty_string(self, tty: bool) -> str:
'''
Returns a printable string.
If tty is enabled, it will attempt to add ANSI color codes
'''
c = lambda i, ansi: i
if not tty:
colorize = c
else:
from os import isatty
if isatty(1) and isatty(2):
try:
from xtermcolor import colorize # type: ignore
except ModuleNotFoundError:
colorize = c
else:
colorize = c
m_len = [len(i) + 2 for i in self.header] # Maximum lenght string
for f in self.content: for f in self.content:
for col, i in enumerate(str(val) for val in f): for col, k in enumerate(str(val) for val in f):
if len(i) > m_len[col]: if len(k) + 2 > m_len[col]:
m_len[col] = len(i) m_len[col] = len(k) + 2
res = "" res = ""
for f, attr in enumerate(self.header): for j, attr in enumerate(self.header):
res += attr.ljust(2 + m_len[f]) res += colorize(attr.ljust(m_len[j]), ansi=3)
for r in self.content: for r in self.content:
res += "\n" res += "\n"
for col, i in enumerate(str(val) for val in r): for col, i in enumerate(r):
res += i.ljust(2 + m_len[col]) cell = str(i).ljust(m_len[col])
if isinstance(i, (int, float)):
cell = colorize(cell, ansi=4)
elif i is None:
cell = colorize(cell, ansi=1)
elif not isinstance(i, str):
cell = colorize(cell, ansi=15)
res += cell
return res return res

View File

@ -165,12 +165,19 @@ def help(command: str) -> None:
p = command.split(' ', 1) p = command.split(' ', 1)
if len(p) == 1: if len(p) == 1:
print( print(
'HELP command\n' 'HELP [command]\n'
'\n'
'Comments are obtained starting with a ;\n'
'\n'
'To execute a query:\n' 'To execute a query:\n'
'[relation =] query\n' '[relation =] query\n'
'\n'
'If the 1st part is omitted, the result will be stored in the relation last_.\n' 'If the 1st part is omitted, the result will be stored in the relation last_.\n'
'\n'
'To prevent from printing the relation, append a ; to the end of the query.\n' 'To prevent from printing the relation, append a ; to the end of the query.\n'
'\n'
'To insert relational operators, type _OPNAME, they will be internally replaced with the correct symbol.\n' 'To insert relational operators, type _OPNAME, they will be internally replaced with the correct symbol.\n'
'\n'
'Rember: completion is enabled and can be very helpful if you can\'t remember something.' 'Rember: completion is enabled and can be very helpful if you can\'t remember something.'
) )
return return
@ -301,7 +308,7 @@ def exec_query(command: str) -> None:
if printrel: if printrel:
print() print()
print(result) print(result.pretty_string(tty=True))
ui.relations[relname] = result ui.relations[relname] = result