Debian Codegraph @

src/fenrirscreenreader/utils/word_utils.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Fenrir TTY screen reader
5 # By Chrys, Storm Dragon, and contributors.
6
7 import string
8
9 from fenrirscreenreader.core import debug
10
11
12 def get_current_word(curr_x, curr_y, curr_text):
13 line_break = False
14 end_of_screen = False
15 if curr_text == "":
16 return -1, -1, "", end_of_screen, line_break
17 if curr_text.strip(string.whitespace) == "":
18 return curr_x, curr_y, "", end_of_screen, line_break
19 x = curr_x
20 y = curr_y
21 curr_word = ""
22 wrapped_lines = curr_text.split("\n")
23 curr_line = wrapped_lines[y]
24 found = False
25 while not found:
26 if not curr_line[x] in string.whitespace:
27 if x == 0:
28 found = True
29 else:
30 if curr_line[x - 1] in string.whitespace:
31 found = True
32 if not found:
33 if x - 1 < 0:
34 if y - 1 < 0:
35 line_break = False
36 end_of_screen = True
37 return curr_x, curr_y, "", end_of_screen, line_break
38 else:
39 y -= 1
40 curr_line = wrapped_lines[y]
41 x = len(curr_line) - 1
42 line_break = True
43 else:
44 x -= 1
45 if found:
46 curr_word = curr_line[x:]
47 for d in string.whitespace:
48 delimiter_pos = curr_word.find(d)
49 if delimiter_pos != -1:
50 curr_word = curr_word[:delimiter_pos]
51 return x, y, curr_word, end_of_screen, line_break
52 return curr_x, curr_y, "", False, False
53
54
55 def get_prev_word(curr_x, curr_y, curr_text):
56 line_break = False
57 end_of_screen = False
58 if curr_text == "":
59 return -1, -1, "", end_of_screen, line_break
60 if curr_text.strip(string.whitespace) == "":
61 return curr_x, curr_y, "", end_of_screen, line_break
62 x, y, curr_word, end_of_screen, line_break_curr_word = get_current_word(
63 curr_x, curr_y, curr_text
64 )
65 if end_of_screen:
66 return x, y, curr_word, end_of_screen, line_break
67 wrapped_lines = curr_text.split("\n")
68 curr_line = wrapped_lines[y]
69 if x - 1 < 0:
70 if y - 1 < 0:
71 line_break = False
72 end_of_screen = True
73 return curr_x, curr_y, "", end_of_screen, line_break
74 else:
75 y -= 1
76 curr_line = wrapped_lines[y]
77 x = len(curr_line) - 1
78 line_break = True
79 else:
80 x -= 1
81 line_break_curr_word = line_break or line_break_curr_word
82 x, y, curr_word, end_of_screen, line_break = get_current_word(
83 x, y, curr_text
84 )
85 line_break = line_break or line_break_curr_word
86 return x, y, curr_word, end_of_screen, line_break
87
88
89 def get_next_word(curr_x, curr_y, curr_text):
90 line_break = False
91 end_of_screen = False
92 if curr_text == "":
93 return -1, -1, "", end_of_screen, line_break
94 if curr_text.strip(string.whitespace) == "":
95 return curr_x, curr_y, "", end_of_screen, line_break
96 x = curr_x
97 y = curr_y
98 curr_word = ""
99 wrapped_lines = curr_text.split("\n")
100 curr_line = wrapped_lines[y]
101 found = False
102 while not found:
103 if not found:
104 if x + 1 > len(curr_line) - 1:
105 if y + 1 > len(wrapped_lines) - 1:
106 line_break = False
107 end_of_screen = True
108 return curr_x, curr_y, "", end_of_screen, line_break
109 else:
110 y += 1
111 curr_line = wrapped_lines[y]
112 x = 0
113 line_break = True
114 else:
115 x += 1
116 if not curr_line[x] in string.whitespace:
117 if x == 0:
118 found = True
119 else:
120 if curr_line[x - 1] in string.whitespace:
121 found = True
122 if found:
123 curr_word = curr_line[x:]
124 for d in string.whitespace:
125 delimiter_pos = curr_word.find(d)
126 if delimiter_pos != -1:
127 curr_word = curr_word[:delimiter_pos]
128 return x, y, curr_word, end_of_screen, line_break
129 return curr_x, curr_y, "", False, False