Color tweaks for find.

This commit is contained in:
Todd Gamblin 2013-12-20 16:56:51 -08:00
parent a63482be73
commit dfd0440a7e
2 changed files with 47 additions and 22 deletions

View File

@ -1,9 +1,12 @@
import collections import collections
import argparse import argparse
from StringIO import StringIO
import spack import spack
import spack.spec
import spack.packages as packages import spack.packages as packages
import spack.colify import spack.colify
from spack.color import *
from spack.colify import colify from spack.colify import colify
description ="Find installed spack packages" description ="Find installed spack packages"
@ -21,7 +24,7 @@ def setup_parser(subparser):
# TODO: move this and colify to tty. # TODO: move this and colify to tty.
def hline(label, char): def hline(label, char, color=''):
max_width = 64 max_width = 64
cols, rows = spack.colify.get_terminal_size() cols, rows = spack.colify.get_terminal_size()
if not cols: if not cols:
@ -31,9 +34,18 @@ def hline(label, char):
cols = min(max_width, cols) cols = min(max_width, cols)
label = str(label) label = str(label)
out = char * 2 + " " + label + " " prefix = char * 2 + " " + label + " "
out += (cols - len(out)) * char suffix = (cols - len(prefix)) * char
return out
out = StringIO()
if color:
prefix = char * 2 + " " + color + cescape(label) + "@. "
cwrite(prefix, stream=out, color=True)
else:
out.write(prefix)
out.write(suffix)
return out.getvalue()
def find(parser, args): def find(parser, args):
@ -56,14 +68,14 @@ def hasher():
# Traverse the index and print out each package # Traverse the index and print out each package
for architecture in index: for architecture in index:
print hline(architecture, "=") print hline(architecture, "=", spack.spec.architecture_color)
for compiler in index[architecture]: for compiler in index[architecture]:
print hline(compiler, "-") print hline(compiler, "-", spack.spec.compiler_color)
specs = index[architecture][compiler] specs = index[architecture][compiler]
specs.sort() specs.sort()
abbreviated = [s.format('$_$@$+$#') for s in specs] abbreviated = [s.format('$_$@$+$#', color=True) for s in specs]
if args.paths: if args.paths:
# Print one spec per line along with prefix path # Print one spec per line along with prefix path
@ -76,7 +88,7 @@ def hasher():
elif args.full_specs: elif args.full_specs:
for spec in specs: for spec in specs:
print spec.tree(indent=4, format='$_$@$+'), print spec.tree(indent=4, format='$_$@$+', color=True),
else: else:
for abbrv in abbreviated: for abbrv in abbreviated:
print " %s" % abbrv print " %s" % abbrv

View File

@ -83,15 +83,23 @@
from spack.util.string import * from spack.util.string import *
# Convenient names for color formats so that other things can use them
compiler_color = '@g'
version_color = '@c'
architecture_color = '@m'
enabled_variant_color = '@B'
disabled_variant_color = '@r'
dependency_color = '@.'
"""This map determines the coloring of specs when using color output. """This map determines the coloring of specs when using color output.
We make the fields different colors to enhance readability. We make the fields different colors to enhance readability.
See spack.color for descriptions of the color codes. """ See spack.color for descriptions of the color codes. """
color_formats = {'%' : '@g', # compiler color_formats = {'%' : compiler_color,
'@' : '@c', # version '@' : version_color,
'=' : '@m', # architecture '=' : architecture_color,
'+' : '@B', # enable variant '+' : enabled_variant_color,
'~' : '@r', # disable variant '~' : disabled_variant_color,
'^' : '@.'} # dependency '^' : dependency_color }
"""Regex used for splitting by spec field separators.""" """Regex used for splitting by spec field separators."""
separators = '[%s]' % ''.join(color_formats.keys()) separators = '[%s]' % ''.join(color_formats.keys())
@ -823,27 +831,34 @@ def format(self, format_string='$_$@$%@$+$=', **kwargs):
of the package, but no dependencies, arch, or compiler. of the package, but no dependencies, arch, or compiler.
""" """
color = kwargs.get('color', False) color = kwargs.get('color', False)
length = len(format_string) length = len(format_string)
out = StringIO() out = StringIO()
escape = compiler = False escape = compiler = False
def write(s, c):
if color:
f = color_formats[c] + cescape(s) + '@.'
cwrite(f, stream=out, color=color)
else:
out.write(s)
for i, c in enumerate(format_string): for i, c in enumerate(format_string):
if escape: if escape:
if c == '_': if c == '_':
out.write(self.name) out.write(self.name)
elif c == '@': elif c == '@':
if self.versions and self.versions != VersionList([':']): if self.versions and self.versions != VersionList([':']):
out.write(c + str(self.versions)) write(c + str(self.versions), c)
elif c == '%': elif c == '%':
if self.compiler: if self.compiler:
out.write(c + str(self.compiler.name)) write(c + str(self.compiler.name), c)
compiler = True compiler = True
elif c == '+': elif c == '+':
if self.variants: if self.variants:
out.write(str(self.variants)) write(str(self.variants), c)
elif c == '=': elif c == '=':
if self.architecture: if self.architecture:
out.write(c + str(self.architecture)) write(c + str(self.architecture), c)
elif c == '#': elif c == '#':
if self.dependencies: if self.dependencies:
out.write('-' + self.dependencies.sha1()[:6]) out.write('-' + self.dependencies.sha1()[:6])
@ -854,7 +869,7 @@ def format(self, format_string='$_$@$%@$+$=', **kwargs):
elif compiler: elif compiler:
if c == '@': if c == '@':
if self.compiler and self.compiler.versions: if self.compiler and self.compiler.versions:
out.write(c + str(self.compiler.versions)) write(c + str(self.compiler.versions), '%')
elif c == '$': elif c == '$':
escape = True escape = True
else: else:
@ -870,8 +885,6 @@ def format(self, format_string='$_$@$%@$+$=', **kwargs):
out.write(c) out.write(c)
result = out.getvalue() result = out.getvalue()
if color:
result = colorize_spec(result)
return result return result