summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-08-05 18:05:06 +0100
committerLars Wirzenius <liw@liw.fi>2011-08-05 18:05:06 +0100
commit8f345be1536c2fe9511d32ee68ee0e0908ab8d6a (patch)
tree0a44534148df6ef3f2be91479316dd11f2807066
parent3d9bd7bd336e375c6e11047df072bc4269bfae2a (diff)
parentc36e88610d71126d92a6d2dbc7de0fe757703541 (diff)
downloadobnam-8f345be1536c2fe9511d32ee68ee0e0908ab8d6a.tar.gz
Format ls output nicer when values are wider than minimum column width.
-rw-r--r--NEWS8
-rw-r--r--obnamlib/plugins/show_plugin.py41
2 files changed, 42 insertions, 7 deletions
diff --git a/NEWS b/NEWS
index 055c7817..f3c21bb9 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,14 @@ Obnam NEWS
This file summarizes changes between releases of Obnam.
+Version 0.20, released UNRELEASED; a BETA release
+-------------------------------------------------
+
+USER VISIBLE CHANGES:
+
+* The output of `obnam ls` now formats the columns a little prettier,
+ so that wide values do not cause misalignment.
+
Version 0.19, released 2011-08-03; a BETA release
-------------------------------------------------
diff --git a/obnamlib/plugins/show_plugin.py b/obnamlib/plugins/show_plugin.py
index 052c46c7..13595738 100644
--- a/obnamlib/plugins/show_plugin.py
+++ b/obnamlib/plugins/show_plugin.py
@@ -30,6 +30,9 @@ class ShowPlugin(obnamlib.ObnamPlugin):
objects, or the contents of a backup generation.
'''
+
+ leftists = (2, 3, 6)
+ min_widths = (1, 1, 1, 1, 6, 20, 1)
def enable(self):
self.app.add_subcommand('clients', self.clients)
@@ -92,15 +95,23 @@ class ShowPlugin(obnamlib.ObnamPlugin):
print
print '%s:' % dirname
subdirs = []
+ everything = []
for basename in self.repo.listdir(gen, dirname):
+ fields = self.fields(gen, dirname, basename)
full = os.path.join(dirname, basename)
- print self.format(gen, dirname, basename)
+ everything.append(fields)
if self.isdir(gen, full):
subdirs.append(full)
+
+ if everything:
+ widths = self.widths(everything)
+ for fields in everything:
+ print self.format(widths, fields)
+
for subdir in subdirs:
self.show_objects(gen, subdir)
- def format(self, gen, dirname, basename):
+ def fields(self, gen, dirname, basename):
full = os.path.join(dirname, basename)
metadata = self.repo.get_metadata(gen, full)
@@ -134,12 +145,28 @@ class ShowPlugin(obnamlib.ObnamPlugin):
else:
name = basename
- return ('%s %2d %-8s %-8s %5d %s %s' %
- (perms,
- metadata.st_nlink or 0,
+ return (perms,
+ str(metadata.st_nlink or 0),
metadata.username or '',
metadata.groupname or '',
- metadata.st_size or 0,
+ str(metadata.st_size or 0),
timestamp,
- name))
+ name)
+
+ def widths(self, everything):
+ w = list(self.min_widths)
+ for fields in everything:
+ for i, field in enumerate(fields):
+ w[i] = max(w[i], len(field))
+ return w
+
+ def format(self, widths, fields):
+ return ' '. join(self.align(widths[i], fields[i], i)
+ for i in range(len(fields)))
+
+ def align(self, width, field, field_no):
+ if field_no in self.leftists:
+ return '%-*s' % (width, field)
+ else:
+ return '%*s' % (width, field)