Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/plotext/_matrix.py: 38%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

138 statements  

1import plotext._utility as ut 

2 

3class matrix_class(): 

4 def __init__(self): 

5 self.set_size(0, 0) 

6 self.set_axes_color() 

7 self.set_canvas_area(0, 0, 0, 0) 

8 self.set_canvas_color() 

9 self.set_matrices() 

10 self.set_canvas() 

11 

12 def set_size(self, cols, rows): 

13 self.rows = rows; self.Rows = range(self.rows) 

14 self.cols = cols; self.Cols = range(self.cols) 

15 self.size = [self.rows, self.cols] 

16 

17 def update_size(self): 

18 self.cols, self.rows = ut.matrix_size(self.marker) 

19 self.Rows = range(self.rows); self.Cols = range(self.cols) 

20 self.size = [self.rows, self.cols] 

21 

22 def set_axes_color(self, color = ut.no_color): 

23 self.axes_color = color 

24 

25 def set_canvas_area(self, col1, col2, row1, row2): 

26 self.Cols_canvas = range(col1, col2) 

27 self.Rows_canvas = range(row1, row2) 

28 

29 def set_canvas_color(self, color = ut.no_color): 

30 self.canvas_color = color 

31 

32 def in_canvas(self, col, row): 

33 return col in self.Cols_canvas and row in self.Rows_canvas 

34 

35 def set_matrices(self): 

36 self.marker = [[ut.space] * self.cols for row in self.Rows] 

37 self.fullground = [[ut.no_color] * self.cols for row in self.Rows] 

38 get_background = lambda col, row: self.canvas_color if self.in_canvas(col, row) else self.axes_color 

39 self.background = [[get_background(col, row) for col in self.Cols] for row in self.Rows] 

40 self.style = [[ut.no_color] * self.cols for row in self.Rows] 

41 

42 def legal(self, col, row): 

43 return col in self.Cols and row in self.Rows 

44 

45 def get_marker(self, col, row): 

46 return self.marker[row][col] #if self.legal(col, row) else "OUT" 

47 

48 def get_marker_row(self, row): 

49 return self.marker[row] #if self.legal(0, row) else "OUT" 

50 

51 def get_marker_col(self, col): 

52 return [self.marker[r][col] for r in self.Rows]#if self.legal(0, row) else "OUT" 

53 

54 def set_marker(self, col, row, marker): 

55 self.marker[row][col] = marker 

56 

57 def set_fullground(self, col, row, fullground = None): 

58 self.fullground[row][col] = fullground 

59 

60 def set_background(self, col, row, background = None): 

61 self.background[row][col] = background 

62 

63 def set_style(self, col, row, style = None): 

64 self.style[row][col] = style 

65 

66 def insert_element(self, col, row, marker, fullground = None, style = None, background = None, check_canvas = False): 

67 test_canvas = True if check_canvas is False else col in self.Cols_canvas and row in self.Rows_canvas 

68 if self.legal(col, row) and test_canvas: 

69 self.set_marker(col, row, marker) 

70 self.set_fullground(col, row, fullground) if fullground is not None else None 

71 self.set_background(col, row, background) if background is not None else None 

72 self.set_style(col, row, style) if style is not None else None 

73 

74 def add_horizontal_string(self, col, row, string, fullground = None, style = None, background = None, alignment = "left", check_space = False, check_canvas = False): 

75 l = len(string); L = range(l) 

76 col = col if alignment == "left" else col - l // 2 if alignment == "center" else col - l + 1 if alignment == "right" else ut.correct_coord(self.get_marker_row(row), string, col) # if dynamic 

77 b, e = max(col - 1, 0), min(col + l + 1, self.cols) 

78 test_space = all([self.get_marker(c, row) == ut.space for c in range(b, e)]) and col >= 0 and col + l <= self.cols if check_space else True 

79 [self.insert_element(col + i, row, string[i], fullground, style, background, check_canvas) for i in L] if test_space else None 

80 return test_space 

81 

82 def add_vertical_string(self, col, row, string, fullground = None, style = None, background = None, alignment = "bottom", check_canvas = False): 

83 l = len(string); L = range(l) 

84 row = row if alignment == "bottom" else row - l // 2 if alignment == "center" else row - l + 1 #if alignment == "top" 

85 [self.insert_element(col, row + i, string[i], fullground, style, background, check_canvas) for i in L] 

86 

87 def add_multiple_horizontal_strings(self, col, row, string, fullground = None, style = None, background = None, alignment = "left", check_space = False, check_canvas = False): 

88 strings = ''.join(string).split('\n'); S = len(strings) 

89 [self.add_horizontal_string(col, row - s, strings[s], fullground, style, background, alignment, check_space, check_canvas) for s in range(S)] 

90 

91 def add_multiple_vertical_strings(self, col, row, string, fullground = None, style = None, background = None, alignment = "left", check_canvas = False): 

92 strings = ''.join(string).split('\n'); S = len(strings) 

93 [self.add_vertical_string(col + s, row, strings[s], fullground, style, background, alignment, check_canvas) for s in range(S)] 

94 

95 def get_colors(self, col, row): 

96 return [self.fullground[row][col], self.style[row][col], self.background[row][col]] #if self.legal(col, row) else ["OUT"] * 3 

97 

98 def set_canvas(self): 

99 canvas = '' 

100 for row in self.Rows[::-1]: 

101 for col in self.Cols: 

102 marker = self.marker[row][col] 

103 colors = self.get_colors(col, row) 

104 if col == 0 or colors != self.get_colors(col - 1, row): 

105 ansi = ut.colors_to_ansi(*colors) 

106 canvas += ansi 

107 canvas += marker 

108 if col == self.cols - 1 or colors != self.get_colors(col + 1, row): 

109 #ansi_next = colors_to_ansi(*colors_next) 

110 canvas += ut.ansi_end #+ ansi_next 

111 canvas += '\n' 

112 

113 self.canvas = canvas 

114 #print(repr(canvas)) 

115 #self.canvas = '\n'.join([''.join(self.marker[row]) for row in self.Rows]) 

116 return self.canvas 

117 

118 def get_canvas(self): 

119 return self.canvas 

120 

121 def hstack(self, extra): 

122 self.marker = ut.hstack(self.marker, extra.marker) 

123 self.fullground = ut.hstack(self.fullground, extra.fullground) 

124 self.background = ut.hstack(self.background, extra.background) 

125 self.style = ut.hstack(self.style, extra.style) 

126 self.update_size() 

127 self.canvas = ''; 

128 

129 def vstack(self, extra): 

130 self.marker = ut.vstack(self.marker, extra.marker) 

131 self.fullground = ut.vstack(self.fullground, extra.fullground) 

132 self.background = ut.vstack(self.background, extra.background) 

133 self.style = ut.vstack(self.style, extra.style) 

134 self.update_size() 

135 self.canvas = '' 

136 

137 def to_html(self): # turns a matrix of character in html form 

138 code = lambda color: "rgb" + str(color).replace(" ", "") 

139 #html = "<body>\n<p style=\"font-family:courier; font-size:11pt;\">\n\n" 

140 html = "<body> \n <code style = style=\"font-family:courier; \"font-size : 10pt;\"> \n\n" 

141 for r in range(self.rows - 1, -1, -1): 

142 for c in range(self.cols): 

143 marker = self.get_marker(c, r) 

144 color, style, background = self.get_colors(c, r) 

145 marker = "&nbsp;" if marker == ut.space else marker 

146 marker = '<b>' + marker + '</b>' if style == 'bold' else marker 

147 marker = '<i>' + marker + '</i>' if style == 'italic' else marker 

148 color = 'black' if color == ut.no_color else color 

149 background = 'white' if background == ut.no_color else background 

150 color = ut.to_rgb(color) 

151 background = ut.to_rgb(background) 

152 color = code(color) 

153 background = code(background) 

154 html += "<span style = \"color:" + color + "; background-color: " + background + "\">" + marker + "</span>" 

155 html += " <br>\n\n" 

156 html += "<code> \n </body>" 

157 return html 

158 

159def join_matrices(matrices): # from a matrix of matrix_class() objects to a single matrix 

160 cols, rows = ut.matrix_size(matrices) 

161 M = matrix_class() 

162 for r in range(rows): 

163 m_rows = matrices[r][0].rows 

164 m = matrix_class() 

165 m.set_size(0, m_rows) 

166 m.set_matrices() 

167 for c in range(cols): 

168 m.hstack(matrices[r][c]) 

169 M.vstack(m) 

170 return M 

171