diff --git a/bigtext.py b/bigtext.py index 3f8b084..4212cf0 100644 --- a/bigtext.py +++ b/bigtext.py @@ -1,31 +1,43 @@ import math -from typing import Optional +import re +from typing import Optional, List import PyQt6.QtGui from PyQt6.QtCore import * from PyQt6.QtGui import * from PyQt6.QtWidgets import * from highlight import Highlight -from highlight_selection import HightlightSelection +from highlight_regex import HighlightRegex +from highlight_selection import HighlightSelection from highlighted_range import HighlightedRange from line import Line from logFileModel import LogFileModel +import re + +from settings import Settings class BigText(QWidget): _byte_offset = 0 _left_offset = 0 - highlights : [Highlight] = [] + highlights: [Highlight] = [] def __init__(self, model: LogFileModel): super(BigText, self).__init__() self.model = model - self.font = QFont("monospace", 20) + self.font = QFont("monospace", 12) self.update_font_metrics(QPainter(self)) self.lines = [] - self.selection_highlight= HightlightSelection(0,0) - self.highlights = [self.selection_highlight] + self.selection_highlight = HighlightSelection(0, 0) + self.highlights = [ + HighlightRegex( + r"INFO", + brush=QBrush(QColor(220, 112, 122)), + brush_full_line=QBrush(QColor(255, 112, 122)) + ), + self.selection_highlight, + ] def paintEvent(self, event: QPaintEvent) -> None: self.draw() @@ -43,7 +55,7 @@ class BigText(QWidget): if self.selection_highlight.end_byte != current_byte: self.selection_highlight.set_end_byte(current_byte) self.update() - #print("-> %s,%s" %(self._selection_start_byte, self._selection_end_byte)) + # print("-> %s,%s" %(self._selection_start_byte, self._selection_end_byte)) def to_byte_offset(self, e: QMouseEvent) -> int: line_number = int(e.pos().y() / self.char_height) @@ -52,10 +64,10 @@ class BigText(QWidget): line = self.lines[line_number] char_in_line = round(e.pos().x() / self.char_width) + self._left_offset char_in_line = min(char_in_line, line.length()) - #print("%s in line %s" % (char_in_line, line_number)) + # print("%s in line %s" % (char_in_line, line_number)) current_byte = line.byte_offset() + char_in_line - #print("%s + %s = %s" % (line.byte_offset(), char_in_line, current_byte)) + # print("%s + %s = %s" % (line.byte_offset(), char_in_line, current_byte)) else: current_byte = self.model.byte_count() return current_byte @@ -75,12 +87,11 @@ class BigText(QWidget): # draws over a character y_line_offset = self.char_height; for l in self.lines: - #if l.intersects(self._selection_start_byte, self._selection_end_byte): - # self.draw_selection(painter, l, y_line_offset) for h in self.highlights: optional_highlight_range = h.compute_highlight(l) if optional_highlight_range: - self.draw_highlight(optional_highlight_range, painter, y_line_offset) + for highlight in optional_highlight_range: + self.draw_highlight(highlight, painter, y_line_offset) y_line_offset = y_line_offset + self.char_height y_line_offset = self.char_height; @@ -90,41 +101,26 @@ class BigText(QWidget): painter.end() - def draw_selection(self, painter: QPainter, line: Line, y_line_offset: int): - hightlight_color = QColor(255, 255, 0) + def draw_highlight(self, highlight: HighlightedRange, painter: QPainter, y_line_offset: int): + x1 = highlight.get_start() * self.char_width + width = highlight.get_width() * self.char_width - if line.includes_byte(self._selection_start_byte): - x1 = (self._selection_start_byte - line.byte_offset() - self._left_offset) * self.char_width - else: - x1 = 0 - - if line.includes_byte(self._selection_end_byte): - width = (self._selection_end_byte - line.byte_offset() - self._left_offset) * self.char_width - x1 - #print("char in line: %d -> width: %d" % (self._selection_end_byte - line.byte_offset() - self._left_offset, width)) - else: - width = len(line.line().rstrip()) * self.char_width -x1 - - y1 = y_line_offset- self.char_height + self.char_height / 7 + y1 = y_line_offset - self.char_height + self.char_height / 7 height = self.char_height - rect = QRect(x1,y1,width,height) - #print(rect) - self.hightlight(painter,rect , hightlight_color) - def draw_highlight(self, highlight: HighlightedRange, painter: QPainter, y_line_offset:int): - x1 = highlight.get_start()*self.char_width - width = highlight.get_width()*self.char_width + if highlight.is_highlight_full_line(): + full_width = Settings.max_line_length() * self.char_width + rect = QRect(0, y1, full_width, height) + self.highlight_background(painter, rect, highlight.get_brush_full_line()) - y1 = y_line_offset- self.char_height + self.char_height / 7 - height = self.char_height - rect = QRect(x1,y1,width,height) - #print(rect) - self.hightlight(painter,rect , highlight.get_brush(), highlight.get_pen()) + rect = QRect(x1, y1, width, height) + self.highlight_background(painter, rect, highlight.get_brush()) - def hightlight(self, painter: QPainter,rect: QRect, brush: QBrush, pen:QPen): + def highlight_background(self, painter: QPainter, rect: QRect, brush: QBrush): old_brush = painter.brush() old_pen = painter.pen() painter.setBrush(brush) - painter.setPen(pen) + painter.setPen(Qt.PenStyle.NoPen) painter.drawRoundedRect(rect, 3.0, 3.0) painter.setBrush(old_brush) painter.setPen(old_pen) @@ -132,8 +128,8 @@ class BigText(QWidget): def update_font_metrics(self, painter: QPainter): fm: QFontMetrics = painter.fontMetrics() self.char_height = fm.height() - self.char_width = fm.averageCharWidth()# all chars have same with for monospace font + self.char_width = fm.averageCharWidth() # all chars have same with for monospace font text = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" - self.char_width = fm.horizontalAdvance(text)/float(len(text)) - #print("bounding rect: %s"%(boundingRect)) - #print("font width=%s height=%s" % (self.char_width, self.char_height)) \ No newline at end of file + self.char_width = fm.horizontalAdvance(text) / float(len(text)) + # print("bounding rect: %s"%(boundingRect)) + # print("font width=%s height=%s" % (self.char_width, self.char_height)) diff --git a/example.log b/example.log index 43f29b3..d32fd36 100644 --- a/example.log +++ b/example.log @@ -9,6 +9,16 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa| ...............................| | äääääääääääääääääääääääääääääää| +2019-08-07 00:00:10,391 [catalina-exec-40] INFO c.r.c.u.l.PerformancePointcut - Executed HealthCheckController.checkOperativeness in 1 ms successful. [jv3fw7r2.m1u5] +2019-08-07 00:00:16,377 [catalina-exec-56] INFO c.r.c.u.l.PerformancePointcut - Executed HealthCheckController.checkOperativeness in 1 ms successful. [jv3fw7r2.m1u6] +2019-08-07 00:00:40,403 [catalina-exec-26] INFO c.r.c.u.l.PerformancePointcut - Executed HealthCheckController.checkOperativeness in 1 ms successful. [jv3fw7r2.m1ud] +2019-08-07 00:02:10,598 [catalina-exec-16] INFO c.r.c.u.l.PerformancePointcut - Executed SecurityController.loginIndex in 0 ms successful. [jv3fw7r2.m1uf] +2019-08-07 00:02:16,467 [catalina-exec-36] INFO c.r.c.u.l.PerformancePointcut - Executed HealthCheckController.checkOperativeness in 1 ms successful. [jv3fw7r2.m1ug] +2019-08-07 00:02:23,519 [catalina-exec-53] INFO c.r.c.u.l.PerformancePointcut - Executed AuthenticationProvider.authenticate in 224 ms successful. [jv3fw7r2.m1uh] +2019-08-07 00:02:24,195 [catalina-exec-43] INFO c.r.c.u.l.PerformancePointcut - Executed SecurityController.accessDenied in 0 ms successful. [jv3fw7r2.m1uj] +2019-08-07 00:02:24,937 [catalina-exec-74] INFO c.r.c.u.l.PerformancePointcut - Executed RedirectController.redirect in 1159 ms successful. [jv3fw7r2.m1ui] +2019-08-07 00:02:25,674 [catalina-exec-29] INFO c.r.c.u.l.PerformancePointcut - Executed AssignmentsController.index in 683 ms successful. [jv3fw7r2.m1uk] +2019-08-07 00:02:26,825 [catalina-exec-20] INFO c.r.c.u.l.PerformancePointcut - Executed I18NController.getI18NKeyValues in 1 ms successful. [jv3fw7r2.m1ul] 2018-09-06T00:00:16.381Z,0,vapfacbk01,HealthCheckService.isOperable,AXC_5.14_526,,successful 2018-09-06T00:00:18.096Z,0,vapfacbk01,HealthCheckService.isOperable,AXC_5.14_526,,successful 2018-09-06T00:00:27.114Z,64,vapfacbk01,MatterApplicationService.search,AXC_5.14_526,,successful diff --git a/highlight.py b/highlight.py index 49ff93a..f399a73 100644 --- a/highlight.py +++ b/highlight.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Optional, List from line import Line from highlighted_range import HighlightedRange @@ -8,5 +8,5 @@ class Highlight: def __init__(self): pass - def compute_highlight(self, line: Line) -> Optional[HighlightedRange]: + def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]: return None diff --git a/highlight_regex.py b/highlight_regex.py new file mode 100644 index 0000000..9e02d49 --- /dev/null +++ b/highlight_regex.py @@ -0,0 +1,42 @@ +from typing import Optional + +from highlight import Highlight +from highlighted_range import HighlightedRange +from line import Line +from PyQt6.QtCore import * +from PyQt6.QtGui import * +from PyQt6.QtWidgets import * + +from settings import Settings +from typing import List +import re + + +class HighlightRegex(Highlight): + + def __init__(self, regex: str, brush: QBrush = QBrush(), pen: QPen = Qt.PenStyle.NoPen, brush_full_line=QBrush()): + self.regex = regex + self.brush = brush + self.pen = pen + self.brush_full_line = brush_full_line + + def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]: + result = [] + #print("execute regex: %s in %s" % (self.regex, line.line())) + match_iter = re.finditer(self.regex, line.line()) + for match in match_iter: + #print("%s" % match) + group0 = match.group(0) + start = match.start(0) + end = match.end(0) + #print("regex: %s" % (group0)) + result.append(HighlightedRange( + start, + end-start, + highlight_full_line=True, + brush=self.brush, + pen=self.pen, + brush_full_line=self.brush_full_line + )) + + return result \ No newline at end of file diff --git a/highlight_selection.py b/highlight_selection.py index afd6d1f..174bb38 100644 --- a/highlight_selection.py +++ b/highlight_selection.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Optional, List from highlight import Highlight from highlighted_range import HighlightedRange @@ -10,7 +10,7 @@ from PyQt6.QtWidgets import * from settings import Settings -class HightlightSelection(Highlight): +class HighlightSelection(Highlight): def __init__(self, start_byte: int, end_byte: int): self.start_byte = start_byte @@ -22,7 +22,7 @@ class HightlightSelection(Highlight): def set_end_byte(self, end_byte): self.end_byte = end_byte - def compute_highlight(self, line: Line) -> Optional[HighlightedRange]: + def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]: begin = min(self.start_byte, self.end_byte) end = max(self.start_byte, self.end_byte) @@ -37,6 +37,6 @@ class HightlightSelection(Highlight): else: length = Settings.max_line_length() -start - return HighlightedRange(start, length, QBrush(QColor(255, 255, 0)), Qt.PenStyle.NoPen) + return [HighlightedRange(start, length, brush=QBrush(QColor(255, 255, 0)), pen=Qt.PenStyle.NoPen)] else: return None diff --git a/highlighted_range.py b/highlighted_range.py index 1d45ffb..44f2c16 100644 --- a/highlighted_range.py +++ b/highlighted_range.py @@ -4,11 +4,24 @@ from PyQt6.QtWidgets import * class HighlightedRange: - def __init__(self, start: int, width: int, brush: QBrush = QBrush(), pen: QPen = Qt.PenStyle.NoPen): + def __init__( + self, + start: int, + width: int, + highlight_full_line=False, + brush: QBrush = QBrush(), + pen: QPen = Qt.PenStyle.NoPen, + brush_full_line: QBrush = QBrush() + ): self.start = start self.width = width self.brush = brush self.pen = pen + self.highlight_full_line = highlight_full_line + self.brush_full_line = brush_full_line + + def is_highlight_full_line(self): + return self.highlight_full_line def get_start(self): return self.start @@ -21,3 +34,6 @@ class HighlightedRange: def get_pen(self): return self.pen + + def get_brush_full_line(self): + return self.brush_full_line diff --git a/main.py b/main.py index dc278e7..95d705d 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,5 @@ +import re + from PyQt6.QtWidgets import * from PyQt6.QtCore import * from PyQt6.QtGui import *