1 # vim:fileencoding=utf-8
2 # Copyright 2010 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
5 from portage.output import colorize
6 from itertools import izip_longest
8 __all__ = ['string_rotator', 'colorize_string', 'align_string', 'rotate_dash', 'print_content', 'display']
10 def display(plain_list, rotated_list, plain_width, rotated_height, cp, toplist = 'archlist'):
11 """Render defauld display to show the keywords listing"""
14 output.append('Keywords for %s:' % colorize('blue', cp))
16 corner_image = [''.ljust(plain_width) for x in range(rotated_height)]
17 if toplist != 'archlist':
18 corner_image.extend(plain_list)
19 data_printout = ['%s%s' % (x, y)
20 for x, y in izip_longest(corner_image, rotated_list, fillvalue=corner_image[0])]
21 if toplist == 'archlist':
22 data_printout.extend(plain_list)
23 output.extend(data_printout)
24 print(print_content(output))
26 def align_string(string, align, length):
27 """Align string to the specified alignment (left or right, and after rotation it becames top and bottom)"""
28 if align == 'top' or align == 'left':
29 string = string.ljust(length)
31 string = string.rjust(length)
34 def colorize_string(color, string):
35 """Add coloring for specified string. Due to rotation we need to do that per character rather than per-line"""
37 for char in list(string):
38 # % is whitespace separator so we wont color that :)
40 tmp.append(colorize(color, char))
45 def rotate_dash(string):
46 """Rotate special strings over 90 degrees for better readability."""
50 for x,y in zip(chars, subs):
51 if string.find(x) != -1:
52 out = out.replace(x, y)
55 def print_content(content):
56 """Print out content (strip it out of the temporary %)"""
57 return '\n'.join(content).replace('%','')
61 def __getChar(self, string, position, line, bold_separator = False):
62 """Return specified character from the string position"""
64 # first figure out what character we want to work with
65 # based on order and position in the string
67 if string.startswith('|') or string.startswith('-') or string.startswith('+'):
70 self.__DASH_COUNT += 1
72 split = string.split('%')
73 char = split[position]
75 if not isdash and bold_separator \
76 and (line-self.__DASH_COUNT)%2 == 0 \
78 char = colorize('bold', char)
81 def rotateContent(self, elements, length, bold_separator = False, strip = True):
83 Rotate string over 90 degrees:
91 # join used to have list of lines rather than list of chars
93 for position in range(length):
95 for i, string in enumerate(elements):
96 x += ' ' + self.__getChar(rotate_dash(string), position, i, bold_separator)
97 # spaces on dashed line should be dashed too
98 if x.find('+ -') != -1:
99 x = x.replace(' ', '-')
100 # strip all chars and remove empty lines
101 if not strip or len(x.strip(' |-')) > 0: