From d5426f64516bed0588f69cd7b9d0af52a7a9e6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20A=C5=9F=C4=B1c=C4=B1?= Date: Wed, 31 Mar 2010 12:04:11 +0000 Subject: [PATCH] cli/info: Use aligned texts to make output more readable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patch by Barış Metin. Modified to use util.get_terminal_size and str.join besides some cosmetic changes. BUG:FIXED:12536 --- pisi/cli/__init__.py | 37 +++++++++++++++++++++++++++++++++++++ pisi/cli/info.py | 16 ++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/pisi/cli/__init__.py b/pisi/cli/__init__.py index b9974c48..ed123a9c 100644 --- a/pisi/cli/__init__.py +++ b/pisi/cli/__init__.py @@ -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 :) diff --git a/pisi/cli/info.py b/pisi/cli/info.py index 564ee72d..d3e1b170 100644 --- a/pisi/cli/info.py +++ b/pisi/cli/info.py @@ -121,18 +121,18 @@ Usage: info ... 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 ... 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 ... 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 ... 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 ... 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)