select word on double click

This commit is contained in:
2021-11-01 15:13:41 +01:00
parent 2f1aabb379
commit f471f4785e
5 changed files with 88 additions and 15 deletions

View File

@@ -76,19 +76,40 @@ class LogFileModel:
target.write(buffer)
offset = new_offset
def data(self, byte_offset, scroll_lines, lines) -> List[Line]:
def read_word_at(self, byte_offset: int) -> (str, int, int):
lines = self.data(byte_offset, 0, 1)
if len(lines) == 0:
return ("", -1, -1)
line: Line = lines[0]
if not lines[0].includes_byte(byte_offset):
return ("", -1, -1)
offset_in_line = byte_offset - line.byte_offset()
current_char = line.line()[line.byte_index_to_char_index(offset_in_line)]
if not self._is_word_char(current_char):
return (current_char, byte_offset, byte_offset + 1)
start_in_line = byte_offset - line.byte_offset()
while start_in_line - 1 >= 0 and self._is_word_char(line.line()[start_in_line - 1]):
start_in_line = start_in_line - 1
end_in_line = byte_offset - line.byte_offset()
while end_in_line < len(line.line()) and self._is_word_char(line.line()[end_in_line]):
end_in_line = end_in_line + 1
start_byte = start_in_line + line.byte_offset()
end_byte = end_in_line + line.byte_offset()
return (line.line()[start_in_line:end_in_line], start_byte, end_byte)
def _is_word_char(self, char: str) -> bool:
return re.match("\w", char)
def data(self, byte_offset: int, scroll_lines: int, lines: int) -> List[Line]:
# 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_return = math.ceil(lines)
start = time.time()
# with self._lock:
if True:
duration = time.time() - start
if duration > 10:
print("data lock acquision %.4f" % ())
# 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:
@@ -102,8 +123,8 @@ class LogFileModel:
new_offset = f.tell()
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:
# print("%s %s %s" %(line.byte_offset(), line.line(), line.byte_end()))
if line.byte_end() <= byte_offset: # line.byte_end() returns the end byte +1
lines_before_offset.append(line)
else:
lines_after_offset.append(line)
@@ -120,6 +141,8 @@ class LogFileModel:
result = all_lines[-lines_to_return + 1:]
# print("returning %s lines" % (len(result)))
# if len(result) > 0:
# print("returning %s %d -> %d" % (result[0].line(), result[0].byte_offset(), result[0].byte_end()))
return result
def byte_count(self) -> int: