Fix wrong value parsing
[gentoolkit.git] / pym / gentoolkit / eshowkw / display_pretty.py
1 #       vim:fileencoding=utf-8
2 # Copyright 2010 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 from portage.output import colorize
6 from itertools import izip_longest
7
8 __all__ = ['string_rotator', 'colorize_string', 'align_string', 'rotate_dash', 'print_content', 'display']
9
10 def display(plain_list, rotated_list, plain_width, rotated_height, cp, toplist = 'archlist'):
11         """Render defauld display to show the keywords listing"""
12         # header
13         output = []
14         output.append('Keywords for %s:' % colorize('blue', cp))
15         # data
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))
25
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)
30         else:
31                 string = string.rjust(length)
32         return string
33
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"""
36         tmp = []
37         for char in list(string):
38                 # % is whitespace separator so we wont color that :)
39                 if char != '%':
40                         tmp.append(colorize(color, char))
41                 else:
42                         tmp.append(char)
43         return ''.join(tmp)
44
45 def rotate_dash(string):
46         """Rotate special strings over 90 degrees for better readability."""
47         chars = ['-', '|']
48         subs = ['|', '-']
49         out = string
50         for x,y  in zip(chars, subs):
51                 if string.find(x) != -1:
52                         out = out.replace(x, y)
53         return out
54
55 def print_content(content):
56         """Print out content (strip it out of the temporary %)"""
57         return '\n'.join(content).replace('%','')
58
59 class string_rotator:
60         __DASH_COUNT = 0
61         def __getChar(self, string, position, line, bold_separator = False):
62                 """Return specified character from the string position"""
63
64                 # first figure out what character we want to work with
65                 # based on order and position in the string
66                 isdash = False
67                 if string.startswith('|') or string.startswith('-') or string.startswith('+'):
68                         split = list(string)
69                         isdash = True
70                         self.__DASH_COUNT += 1
71                 else:
72                         split = string.split('%')
73                 char = split[position]
74                 # bolding
75                 if not isdash and bold_separator \
76                                 and (line-self.__DASH_COUNT)%2 == 0 \
77                                 and char != ' ':
78                         char = colorize('bold', char)
79                 return char
80
81         def rotateContent(self, elements, length, bold_separator = False, strip = True):
82                 """
83                         Rotate string over 90 degrees:
84                         string -> s
85                                                 t
86                                                 r
87                                                 i
88                                                 n
89                                                 g
90                 """
91                 # join used to have list of lines rather than list of chars
92                 tmp = []
93                 for position in range(length):
94                         x = ''
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:
102                                 tmp.append(x)
103                 return tmp