Pretty printer

This commit is contained in:
Kload 2012-10-10 14:19:17 +02:00
parent 933e0d240f
commit f0d4b918b9
2 changed files with 12 additions and 7 deletions

View file

@ -27,6 +27,13 @@ def colorize(astr, color):
} }
return "\033["+ color_dict[color] +"m\033[1m" + astr + "\033[m" return "\033["+ color_dict[color] +"m\033[1m" + astr + "\033[m"
def pretty_print_dict(d, depth=0):
for k,v in sorted(d.items(), key=lambda x: x[0]):
if isinstance(v, dict):
print (" ")*depth + ("%s: " % k)
pretty_print_dict(v, depth+1)
else:
print (" ")*depth + "%s: %s" % (colorize(k, 'purple'), v)
def win_msg(astr): def win_msg(astr):
""" """

View file

@ -31,7 +31,7 @@ if not __debug__:
sys.path.append('lib') sys.path.append('lib')
gettext.install('YunoHost') gettext.install('YunoHost')
from yunohost import YunoHostError, str_to_func, colorize from yunohost import YunoHostError, str_to_func, colorize, pretty_print_dict
""" """
@ -186,8 +186,8 @@ def main():
""" """
Main instructions Main instructions
Parse the action_dict and execute the action-specific function Parse the action_dict and execute the action-specific function,
Then print json or pretty result if executed in a tty :) then print json or pretty result if executed in a tty :)
Returns: Returns:
int -- 0 or error code int -- 0 or error code
@ -206,9 +206,7 @@ def main():
return error.code return error.code
else: else:
if os.isatty(1): if os.isatty(1):
for attr, value in result.items(): pretty_print_dict(result)
if type(value) is str:
print(colorize(attr, 'purple') + ': ' + value)
else: else:
print(json.dumps(result)) print(json.dumps(result))
return 0 return 0