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,7 +27,14 @@ def colorize(astr, color):
}
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):
"""
Display a success message if isatty

View file

@ -31,7 +31,7 @@ if not __debug__:
sys.path.append('lib')
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
Parse the action_dict and execute the action-specific function
Then print json or pretty result if executed in a tty :)
Parse the action_dict and execute the action-specific function,
then print json or pretty result if executed in a tty :)
Returns:
int -- 0 or error code
@ -206,9 +206,7 @@ def main():
return error.code
else:
if os.isatty(1):
for attr, value in result.items():
if type(value) is str:
print(colorize(attr, 'purple') + ': ' + value)
pretty_print_dict(result)
else:
print(json.dumps(result))
return 0