Allow widths in spec format strings.

This commit is contained in:
Todd Gamblin 2014-05-04 11:07:16 -07:00
parent 703e611043
commit 3607c26439
2 changed files with 30 additions and 9 deletions

View file

@ -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)

View file

@ -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