cli/info: Use aligned texts to make output more readable

Patch by Barış Metin. Modified to use util.get_terminal_size and
str.join besides some cosmetic changes.

BUG:FIXED:12536
This commit is contained in:
Fatih Aşıcı
2010-03-31 12:04:11 +00:00
parent 88a181516e
commit d5426f6451
2 changed files with 45 additions and 8 deletions
+37
View File
@@ -63,6 +63,43 @@ class CLI(pisi.ui.UI):
out.write(msg)
out.flush()
def formatted_output(self, msg, verbose = False, noln = False, column=":"):
key_width = 20
line_format = "%(key)-20s%(column)s%(rest)s"
term_height, term_width = pisi.util.get_terminal_size()
def find_whitespace(s, i):
while s[i] not in (" ", "\t"):
i -= 1
return i
def align(s):
align_width = term_width - key_width - 2
s_width = len(s)
new_s = ""
index = 0
while True:
next_index = index + align_width
if next_index >= s_width:
new_s += s[index:]
break
next_index = find_whitespace(s, next_index)
new_s += s[index:next_index]
index = next_index
if index < s_width:
new_s += "\n" + " " * (key_width + 1)
return new_s
new_msg = ""
for line in msg.split("\n"):
key, column, rest = line.partition(column)
rest = align(rest)
new_msg += line_format % {"key":key, "column":column, "rest":rest}
if not noln:
new_msg = "%s\n" % new_msg
msg = new_msg
self.output(unicode(msg), verbose=verbose)
def info(self, msg, verbose = False, noln = False):
# TODO: need to look at more kinds of info messages
# let's cheat from KDE :)
+8 -8
View File
@@ -121,18 +121,18 @@ Usage: info <package1> <package2> ... <packagen>
def print_metadata(self, metadata, packagedb=None):
if ctx.get_option('short'):
pkg = metadata.package
ctx.ui.info('%15s - %s' % (pkg.name, unicode(pkg.summary)))
ctx.ui.formatted_output(" - ".join((pkg.name, unicode(pkg.summary))))
else:
ctx.ui.info(unicode(metadata.package))
ctx.ui.formatted_output(unicode(metadata.package))
if packagedb:
revdeps = [name for name, dep in packagedb.get_rev_deps(metadata.package.name)]
print _('Reverse Dependencies:'), util.strlist(revdeps)
ctx.ui.formatted_output(" ".join((_("Reverse Dependencies:"), util.strlist(revdeps))))
print
def print_specdata(self, spec, sourcedb=None):
src = spec.source
if ctx.get_option('short'):
ctx.ui.info('%15s - %s' % (src.name, unicode(src.summary)))
ctx.ui.formatted_output(" - ".join((src.name, unicode(src.summary))))
else:
ctx.ui.info(unicode(spec))
if sourcedb:
@@ -142,7 +142,7 @@ Usage: info <package1> <package2> ... <packagen>
def pisifile_info(self, package):
metadata, files = pisi.api.info_file(package)
ctx.ui.info(_('Package file: %s') % package)
ctx.ui.formatted_output(_("Package file: %s") % package)
self.print_metadata(metadata)
if self.options.files or self.options.files_path:
@@ -157,7 +157,7 @@ Usage: info <package1> <package2> ... <packagen>
return
if self.options.short:
ctx.ui.info(_('[inst] '), noln=True)
ctx.ui.formatted_output(_("[inst] "), noln=True, column=" ")
else:
ctx.ui.info(_('Installed package:'))
@@ -169,7 +169,7 @@ Usage: info <package1> <package2> ... <packagen>
if self.packagedb.has_package(package):
metadata, files, repo = pisi.api.info_name(package, False)
if self.options.short:
ctx.ui.info(_('[binary] '), noln=True)
ctx.ui.formatted_output(_("[binary] "), noln=True, column=" ")
else:
ctx.ui.info(_('Package found in %s repository:') % repo)
self.print_metadata(metadata, self.packagedb)
@@ -181,7 +181,7 @@ Usage: info <package1> <package2> ... <packagen>
repo = self.sourcedb.which_repo(package)
spec = self.sourcedb.get_spec(package)
if self.options.short:
ctx.ui.info(_('[source] '), noln=True)
ctx.ui.formatted_output(_("[source] "), noln=True, column=" ")
else:
ctx.ui.info(_('Package found in %s repository:') % repo)
self.print_specdata(spec, self.sourcedb)