| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Fenrir TTY screen reader |
| 5 |
# By Chrys, Storm Dragon, and contributors. |
| 6 |
|
| 7 |
from fenrirscreenreader.core import debug |
| 8 |
|
| 9 |
|
| 10 |
def get_text_between_marks(firstMark, secondMark, in_text): |
| 11 |
if in_text is None: |
| 12 |
return "" |
| 13 |
if not isinstance(in_text, list): |
| 14 |
in_text = in_text.split("\n") |
| 15 |
if len(in_text) < 1: |
| 16 |
return "" |
| 17 |
if firstMark is None: |
| 18 |
return "" |
| 19 |
if secondMark is None: |
| 20 |
return "" |
| 21 |
if (firstMark["y"] + 1) * (firstMark["x"] + 1) <= (secondMark["y"] + 1) * ( |
| 22 |
secondMark["x"] + 1 |
| 23 |
): |
| 24 |
start_mark = firstMark.copy() |
| 25 |
end_mark = secondMark.copy() |
| 26 |
else: |
| 27 |
end_mark = firstMark.copy() |
| 28 |
start_mark = secondMark.copy() |
| 29 |
text_part = "" |
| 30 |
if start_mark["y"] == end_mark["y"]: |
| 31 |
text_part += in_text[start_mark["y"]][ |
| 32 |
start_mark["x"] : end_mark["x"] + 1 |
| 33 |
] |
| 34 |
else: |
| 35 |
curr_y = start_mark["y"] |
| 36 |
while curr_y <= end_mark["y"]: |
| 37 |
if curr_y < end_mark["y"]: |
| 38 |
if curr_y == start_mark["y"]: |
| 39 |
text_part += in_text[curr_y][start_mark["x"] :] |
| 40 |
else: |
| 41 |
text_part += in_text[curr_y] |
| 42 |
if len(in_text[curr_y].strip()) != 0: |
| 43 |
if len(text_part) - len(text_part.rstrip()) > 0: |
| 44 |
text_part = text_part[: len(text_part.rstrip())] + "\n" |
| 45 |
else: |
| 46 |
text_part += "\n" |
| 47 |
else: |
| 48 |
text_part += in_text[curr_y][: end_mark["x"] + 1] |
| 49 |
curr_y += 1 |
| 50 |
return text_part |
| 51 |
|
| 52 |
|
| 53 |
def get_text_before_mark(mark, in_text): |
| 54 |
if in_text is None: |
| 55 |
return "" |
| 56 |
if mark is None: |
| 57 |
return "" |
| 58 |
return get_text_between_marks({"x": 0, "y": 0}, mark, in_text) |
| 59 |
|
| 60 |
|
| 61 |
def get_text_after_mark(mark, in_text): |
| 62 |
if in_text is None: |
| 63 |
return "" |
| 64 |
if mark is None: |
| 65 |
return "" |
| 66 |
in_text = in_text.split("\n") |
| 67 |
return get_text_between_marks( |
| 68 |
mark, {"x": len(in_text[0]) - 1, "y": len(in_text) - 1}, in_text |
| 69 |
) |