diff --git a/filterwidget.py b/filterwidget.py index 2fa2c2b..f2f88bb 100644 --- a/filterwidget.py +++ b/filterwidget.py @@ -123,6 +123,7 @@ class FilterWidget(QWidget): query = self.query_field.text() ignore_case = self.ignore_case.isChecked() if len(query) == 0: + self.filter_model.truncate() return # cancel previous search @@ -135,7 +136,8 @@ class FilterWidget(QWidget): else: regex = re.compile(re.escape(query), flags=flags) except: - # query was not a valid regex -> abort + # query was not a valid regex -> clear search hits, then abort + self.filter_model.truncate() return self.filter_task = FilterTask( diff --git a/logFileModel.py b/logFileModel.py index f8b26a8..edd2aa2 100644 --- a/logFileModel.py +++ b/logFileModel.py @@ -9,7 +9,7 @@ from settings import Settings class LogFileModel: - def __init__(self, file:str, settings: Settings): + def __init__(self, file: str, settings: Settings): self.settings = settings self._file = os.path.realpath(file) self._lock = threading.RLock() @@ -34,28 +34,29 @@ class LogFileModel: return bytes.decode("utf8", errors="ignore") def data(self, byte_offset, scroll_lines, lines) -> List[Line]: - #print("data(%s, %s, %s)" % (byte_offset, scroll_lines, lines)) + # print("data(%s, %s, %s)" % (byte_offset, scroll_lines, lines)) lines_before_offset: List[Line] = [] lines_after_offset: List[Line] = [] - lines_to_find = lines + abs(scroll_lines) + lines_to_find = lines + abs(scroll_lines) lines_to_return = math.ceil(lines) - #start = time.time() + # start = time.time() with self._lock: - #print("data lock acquision %.4f" % (time.time() -start)) + # print("data lock acquision %.4f" % (time.time() -start)) # TODO handle lines longer than 4096 bytes # TODO abort file open after a few secons: https://docs.python.org/3/library/signal.html#example with open(self._file, 'rb') as f: offset = min(byte_offset, self.byte_count()) offset = max(0, offset - self.settings.max_line_length()) - #print("offset: %s" % (offset)) + # print("offset: %s" % (offset)) eof_reached = True f.seek(offset) while l := f.readline(): new_offset = f.tell() - line = Line(offset, new_offset, l.decode("utf8", errors="ignore").replace("\r", "").replace("\n", "")) + line = Line(offset, new_offset, + l.decode("utf8", errors="ignore").replace("\r", "").replace("\n", "")) # print("%s %s" %(line.byte_offset(), line.line())) if offset < byte_offset: lines_before_offset.append(line) @@ -68,12 +69,12 @@ class LogFileModel: all_lines = lines_before_offset + lines_after_offset start = max(0, len(lines_before_offset) + scroll_lines) - if start + lines_to_return-1 < len(all_lines): - result = all_lines[start:start+lines_to_return] + if start + lines_to_return - 1 < len(all_lines): + result = all_lines[start:start + lines_to_return] else: - result = all_lines[-lines_to_return+1:] + result = all_lines[-lines_to_return + 1:] - #print("returning %s lines" % (len(result))) + # print("returning %s lines" % (len(result))) return result def byte_count(self) -> int: @@ -87,5 +88,4 @@ class LogFileModel: def truncate(self): with open(self._file, 'a') as f: - print("truncating") - f.truncate(0) \ No newline at end of file + f.truncate(0)