reset search hits when entering empty or invalid query

This commit is contained in:
2021-10-28 09:48:38 +02:00
parent 02f1ce3af9
commit a0e6c24098
2 changed files with 16 additions and 14 deletions

View File

@@ -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)
f.truncate(0)