def __analyse(self, table: list, header: bool, border: bool, aligning="left", title="", pos=None, max_width=50, gap=' ', indent=' ', autocolor=True) -> str: table, pos = self.__wrap(table, pos, header) if autocolor: for line in table: # 对单元格进行着色 for c in range(len(table[0])): if header and c == 0: continue cell = str(line[c]) if line[c] is None: line[c] = colour.colorize(cell, 'note', 'red') else: line[c] = colour(cell, self._color_rule) columns = len(table[0]) result = '' width_list = [0 for i in range(columns)] # 对应每列的字符最大宽度 max_width_list = [(max_width if isinstance(max_width, int) else 50) for i in range(columns)] # 限制每列的最大宽度,超过将折叠行 if isinstance(max_width, str): match = re.findall(r'(\d+)-(\d+?)', max_width) if match: for m in match: col = int(m[1]) if col > columns or col <= 0: continue max_width_list[col - 1] = int(m[0]) # 检查每个单元格是否超过最大宽度,有的话添加一个换行符 for row in table: for c in range(columns): cell = str(row[c]) length = self._str_width(colour.normalize(cell)) if width_list[c] < length: width_list[c] = length if width_list[c] > max_width_list[c]: width_list[c] = max_width_list[c] col = '' color_list = [ ] # like [[match, True], ...] True表示颜色代码为默认代码\033[0m tmp = re.finditer(r'\033\[[\d;]+m', cell) for ma in tmp: if re.fullmatch(r'\033\[[0;]+m', ma.group(0)): color_list.append([ma, True]) else: color_list.append([ma, False]) chunk = 0 start = 0 end = 0 if color_list: for color_tmp in color_list: # 着色代码不占显示宽度 ma = color_tmp[0] tmp_chunk = ma.start(0) - end if chunk + tmp_chunk == max_width_list[c]: end = ma.end(0) if color_tmp[1] else ma.start( 0) col += cell[start:end] + '\n' start = end chunk = 0 elif chunk + tmp_chunk > max_width_list[c]: end = max_width_list[c] - chunk + end col += cell[start:end] + '\n' start = end chunk = ma.start(0) - start while chunk > max_width_list[c]: end = start + max_width_list[c] col += cell[start:end] + '\n' start = end chunk = ma.start(0) - start if chunk == max_width_list[c]: end = ma.end( 0) if color_tmp[1] else ma.start(0) col += cell[start:end] + '\n' start = end chunk = 0 else: chunk += tmp_chunk end = ma.end(0) tmp_chunk = len(cell) - end while chunk + tmp_chunk > max_width_list[c]: end = max_width_list[c] - chunk + end col += cell[start:end] + '\n' start = end chunk = 0 tmp_chunk = len(cell) - end col += cell[start:] + '\n' else: while start + max_width_list[c] < length: end = start + max_width_list[c] col += cell[start:end] + '\n' start = end col += cell[start:] + '\n' row[c] = col.rstrip('\r\n ') table, pos = self.__wrap(table, pos, header) if title != '' and title is not None: # 添加标题 l = self._str_width(title) result += title + '\n' result += '=' * l + '\n\n' aligning_list = ['left' for i in width_list] # 对齐方式列表 match = re.findall(r'(left|center|right)-(\d+)', aligning) if match: for m in match: col = int(m[1]) if col > columns or col <= 0: continue aligning_list[col - 1] = m[0] border_line = ['-' * i for i in width_list] if border: result += indent + \ self.__draw_line(border_line, width_list, aligning_list, '+', padding='-')+'\n' if header: result += indent + \ self.__draw_line(table[0], width_list, aligning_list, '|' if border else gap)+'\n' # 画头部和数据的分割线 tmp_border_line = ['-' * len(t) for t in table[0]] result += indent + \ self.__draw_line(tmp_border_line, width_list, aligning_list, '+' if border else gap, padding=' ')+'\n' table = table[1:] i = 1 for row in table: if i == pos: result += colour.colorize('=> '.rjust(len(indent), ' '), 'bold', 'green') + \ self.__draw_line( row, width_list, aligning_list, '|' if border else gap)+'\n' else: result += indent + \ self.__draw_line( row, width_list, aligning_list, '|' if border else gap)+'\n' i += 1 if border: result += indent + \ self.__draw_line(border_line, width_list, aligning_list, '+', padding='-')+'\n' return '\n' + result
def warning(self, msg: str): self._logger.warning(colour.colorize(colour(f"[#] {msg}", self._color_rule), 'bold'))
def error(self, msg: str): self._logger.error(colour(f"[!] {msg}", self._color_rule))
def trace(self, msg: str, exc_info=False): '''比debug更详细的信息, 记录每个关键动作产生的冗余信息或临时信息 ''' self._logger.log(self.TRACE, colour(f"[T] {msg}", self._color_rule), exc_info=exc_info)
def debug(self, msg: str, exc_info=False): '''记录关键操作信息,如配置的修改 ''' self._logger.debug(colour(f"[D] {msg}", self._color_rule), exc_info=exc_info)
def info(self, msg: str, add=None): if add is not None: self._logger.info(colour(f"{'[+]' if add else '[-]' } {msg}", self._color_rule)) else: self._logger.info(colour(f"[*] {msg}", self._color_rule))