From 3607c264392c174915fe1ada3a907c5a65eb0e61 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Sun, 4 May 2014 11:07:16 -0700 Subject: [PATCH] Allow widths in spec format strings. --- lib/spack/spack/cmd/find.py | 8 ++++++-- lib/spack/spack/spec.py | 31 ++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/spack/spack/cmd/find.py b/lib/spack/spack/cmd/find.py index b8448641f3..b6ec123c92 100644 --- a/lib/spack/spack/cmd/find.py +++ b/lib/spack/spack/cmd/find.py @@ -98,5 +98,9 @@ def hasher(): for spec in specs: print spec.tree(indent=4, format='$_$@$+', color=True), else: - for abbrv in abbreviated: - print " %s" % abbrv + max_len = max([len(s.name) for s in specs]) + max_len += 4 + + for spec in specs: + format = '$-' + str(max_len) + '_$@$+$#' + print " " + spec.format(format, color=True) diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index 275372f1ba..6416ff9487 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -1015,6 +1015,10 @@ def format(self, format_string='$_$@$%@$+$=', **kwargs): $# Dependencies' 8-char sha1 prefix $$ $ + Optionally you can provide a width, e.g. $20_ for a 20-wide name. + Like printf, you can provide '-' for left justification, e.g. + $-20_ for a left-justified name. + Anything else is copied verbatim into the output stream. *Example:* ``$_$@$+`` translates to the name, version, and options @@ -1035,27 +1039,40 @@ def write(s, c): else: out.write(s) - for i, c in enumerate(format_string): + iterator = enumerate(format_string) + for i, c in iterator: if escape: + fmt = '%' + if c == '-': + fmt += c + i, c = next(iterator) + + while c in '0123456789': + fmt += c + i, c = next(iterator) + fmt += 's' + if c == '_': - out.write(self.name) + out.write(fmt % self.name) elif c == '@': if self.versions and self.versions != _any_version: - write(c + str(self.versions), c) + write(fmt % (c + str(self.versions)), c) elif c == '%': if self.compiler: - write(c + str(self.compiler.name), c) + write(fmt % (c + str(self.compiler.name)), c) compiler = True elif c == '+': if self.variants: - write(str(self.variants), c) + write(fmt % str(self.variants), c) elif c == '=': if self.architecture: - write(c + str(self.architecture), c) + write(fmt % (c + str(self.architecture)), c) elif c == '#': if self.dependencies: - out.write('-' + self.dep_hash(8)) + out.write(fmt % ('-' + self.dep_hash(8))) elif c == '$': + if fmt != '': + raise ValueError("Can't use format width with $$.") out.write('$') escape = False