| 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 difflib |
| 8 |
import re |
| 9 |
import time |
| 10 |
from fenrirscreenreader.core.i18n import _ |
| 11 |
|
| 12 |
|
| 13 |
class command: |
| 14 |
def __init__(self): |
| 15 |
self._update_times = [] |
| 16 |
self._line_count_times = [] |
| 17 |
self._batched_text = [] |
| 18 |
self._last_flush_time = 0 |
| 19 |
self._in_flood_mode = False |
| 20 |
self._pending_focus_text = "" |
| 21 |
self._pending_focus_time = 0 |
| 22 |
|
| 23 |
def initialize(self, environment): |
| 24 |
self.env = environment |
| 25 |
|
| 26 |
def shutdown(self): |
| 27 |
pass |
| 28 |
|
| 29 |
def get_description(self): |
| 30 |
return _("Announces incoming text changes") |
| 31 |
|
| 32 |
def _reset_flood_state(self): |
| 33 |
self._update_times = [] |
| 34 |
self._line_count_times = [] |
| 35 |
self._batched_text = [] |
| 36 |
self._last_flush_time = 0 |
| 37 |
self._in_flood_mode = False |
| 38 |
|
| 39 |
def _is_rapid_updates(self): |
| 40 |
current_time = time.time() |
| 41 |
window = self.env["runtime"]["SettingsManager"].get_setting_as_float( |
| 42 |
"speech", "rapid_update_window" |
| 43 |
) |
| 44 |
threshold = self.env["runtime"]["SettingsManager"].get_setting_as_int( |
| 45 |
"speech", "rapid_update_threshold" |
| 46 |
) |
| 47 |
|
| 48 |
self._update_times = [ |
| 49 |
ts for ts in self._update_times if current_time - ts < window |
| 50 |
] |
| 51 |
self._update_times.append(current_time) |
| 52 |
|
| 53 |
return len(self._update_times) >= threshold |
| 54 |
|
| 55 |
def _is_high_volume(self, delta_text): |
| 56 |
current_time = time.time() |
| 57 |
window = self.env["runtime"]["SettingsManager"].get_setting_as_float( |
| 58 |
"speech", "rapid_update_window" |
| 59 |
) |
| 60 |
threshold = self.env["runtime"]["SettingsManager"].get_setting_as_int( |
| 61 |
"speech", "flood_line_threshold" |
| 62 |
) |
| 63 |
|
| 64 |
line_count = max(1, delta_text.count("\n") + 1) |
| 65 |
self._line_count_times = [ |
| 66 |
(ts, count) |
| 67 |
for ts, count in self._line_count_times |
| 68 |
if current_time - ts < window |
| 69 |
] |
| 70 |
self._line_count_times.append((current_time, line_count)) |
| 71 |
|
| 72 |
total_lines = sum(count for _, count in self._line_count_times) |
| 73 |
return total_lines >= threshold |
| 74 |
|
| 75 |
def _add_to_batch(self, text): |
| 76 |
new_lines = text.splitlines() |
| 77 |
if text.endswith("\n"): |
| 78 |
new_lines.append("") |
| 79 |
self._batched_text.extend(new_lines) |
| 80 |
|
| 81 |
max_lines = self.env["runtime"]["SettingsManager"].get_setting_as_int( |
| 82 |
"speech", "max_batch_lines" |
| 83 |
) |
| 84 |
if len(self._batched_text) > max_lines: |
| 85 |
self._batched_text = self._batched_text[-max_lines:] |
| 86 |
|
| 87 |
def _flush_batch(self): |
| 88 |
if not self._batched_text: |
| 89 |
return |
| 90 |
|
| 91 |
text = "\n".join(self._batched_text) |
| 92 |
self._batched_text = [] |
| 93 |
self._last_flush_time = time.time() |
| 94 |
|
| 95 |
self.env["runtime"]["OutputManager"].present_text( |
| 96 |
text, interrupt=False, flush=False |
| 97 |
) |
| 98 |
|
| 99 |
def _was_handled_by_tab_completion(self, delta_text): |
| 100 |
"""Check if this delta was already handled by tab completion to avoid duplicates""" |
| 101 |
if "tabCompletion" not in self.env["commandBuffer"]: |
| 102 |
return False |
| 103 |
|
| 104 |
tab_state = self.env["commandBuffer"]["tabCompletion"] |
| 105 |
|
| 106 |
# Check if this exact delta was processed recently by tab completion |
| 107 |
if (tab_state.get("lastProcessedDelta") == delta_text and |
| 108 |
tab_state.get("lastProcessedTime")): |
| 109 |
|
| 110 |
# Only suppress if processed within the last 50ms to avoid stale suppression |
| 111 |
# Reduced from 100ms to minimize false positives with rapid multi-line updates |
| 112 |
time_since_processed = time.time() - tab_state["lastProcessedTime"] |
| 113 |
if time_since_processed <= 0.05: |
| 114 |
return True |
| 115 |
|
| 116 |
return False |
| 117 |
|
| 118 |
def _normalize_line(self, line): |
| 119 |
return " ".join(line.split()) |
| 120 |
|
| 121 |
def _is_focus_control_line(self, text): |
| 122 |
stripped = self._normalize_line(text) |
| 123 |
if not stripped: |
| 124 |
return False |
| 125 |
if re.fullmatch(r"<\s*[A-Za-z][A-Za-z0-9 _.-]{0,30}\s*>", stripped): |
| 126 |
return True |
| 127 |
return stripped.lower() in { |
| 128 |
"ok", |
| 129 |
"cancel", |
| 130 |
"yes", |
| 131 |
"no", |
| 132 |
"retry", |
| 133 |
"abort", |
| 134 |
"ignore", |
| 135 |
"continue", |
| 136 |
} |
| 137 |
|
| 138 |
def _is_transient_focus_only_update(self, delta_text): |
| 139 |
delta_lines = [ |
| 140 |
self._normalize_line(line) |
| 141 |
for line in delta_text.splitlines() |
| 142 |
if line.strip() |
| 143 |
] |
| 144 |
if len(delta_lines) != 1: |
| 145 |
return False |
| 146 |
if not self._is_focus_control_line(delta_lines[0]): |
| 147 |
return False |
| 148 |
|
| 149 |
screen_lines = [ |
| 150 |
self._normalize_line(line) |
| 151 |
for line in self.env["screen"]["new_content_text"].split("\n") |
| 152 |
if line.strip() |
| 153 |
] |
| 154 |
return screen_lines == delta_lines |
| 155 |
|
| 156 |
def _store_pending_focus_text(self, delta_text): |
| 157 |
delta_lines = [ |
| 158 |
self._normalize_line(line) |
| 159 |
for line in delta_text.splitlines() |
| 160 |
if line.strip() |
| 161 |
] |
| 162 |
self._pending_focus_text = "\n".join(delta_lines) |
| 163 |
self._pending_focus_time = time.time() |
| 164 |
|
| 165 |
def _take_pending_focus_text(self): |
| 166 |
if not self._pending_focus_text: |
| 167 |
return "" |
| 168 |
if time.time() - self._pending_focus_time > 0.5: |
| 169 |
self._pending_focus_text = "" |
| 170 |
self._pending_focus_time = 0 |
| 171 |
return "" |
| 172 |
pending = self._pending_focus_text |
| 173 |
self._pending_focus_text = "" |
| 174 |
self._pending_focus_time = 0 |
| 175 |
return pending |
| 176 |
|
| 177 |
def _is_subsequence(self, subset_lines, source_lines): |
| 178 |
subset_index = 0 |
| 179 |
for source_line in source_lines: |
| 180 |
if source_line == subset_lines[subset_index]: |
| 181 |
subset_index += 1 |
| 182 |
if subset_index == len(subset_lines): |
| 183 |
return True |
| 184 |
return False |
| 185 |
|
| 186 |
def _prefer_inserted_lower_screen_text(self, delta_text): |
| 187 |
delta_lines = [ |
| 188 |
line for line in delta_text.splitlines() if line.strip() != "" |
| 189 |
] |
| 190 |
if len(delta_lines) < 2: |
| 191 |
return delta_text |
| 192 |
|
| 193 |
old_lines = self.env["screen"]["old_content_text"].split("\n") |
| 194 |
new_lines = self.env["screen"]["new_content_text"].split("\n") |
| 195 |
if len(old_lines) != len(new_lines) or len(new_lines) < 4: |
| 196 |
return delta_text |
| 197 |
|
| 198 |
normalized_old_lines = [ |
| 199 |
self._normalize_line(line) for line in old_lines |
| 200 |
] |
| 201 |
normalized_new_lines = [ |
| 202 |
self._normalize_line(line) for line in new_lines |
| 203 |
] |
| 204 |
matcher = difflib.SequenceMatcher( |
| 205 |
None, normalized_old_lines, normalized_new_lines, autojunk=False |
| 206 |
) |
| 207 |
|
| 208 |
top_lines_changed = False |
| 209 |
inserted_lines = [] |
| 210 |
lower_screen_start = max(2, len(new_lines) // 2) |
| 211 |
|
| 212 |
for tag, old_start, old_end, new_start, new_end in matcher.get_opcodes(): |
| 213 |
if tag in {"replace", "delete"} and old_start < 2: |
| 214 |
top_lines_changed = True |
| 215 |
if tag != "insert" or new_start < lower_screen_start: |
| 216 |
continue |
| 217 |
inserted_lines.extend( |
| 218 |
line for line in new_lines[new_start:new_end] if line.strip() != "" |
| 219 |
) |
| 220 |
|
| 221 |
if not top_lines_changed or not inserted_lines: |
| 222 |
return delta_text |
| 223 |
if all( |
| 224 |
self._is_focus_control_line(line) for line in inserted_lines |
| 225 |
): |
| 226 |
return delta_text |
| 227 |
|
| 228 |
normalized_delta_lines = [ |
| 229 |
self._normalize_line(line) for line in delta_lines |
| 230 |
] |
| 231 |
normalized_inserted_lines = [ |
| 232 |
self._normalize_line(line) for line in inserted_lines |
| 233 |
] |
| 234 |
if not self._is_subsequence( |
| 235 |
normalized_inserted_lines, normalized_delta_lines |
| 236 |
): |
| 237 |
return delta_text |
| 238 |
if normalized_delta_lines == normalized_inserted_lines: |
| 239 |
return delta_text |
| 240 |
|
| 241 |
return "\n".join(inserted_lines) |
| 242 |
|
| 243 |
def run(self): |
| 244 |
if not self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 245 |
"speech", "auto_read_incoming" |
| 246 |
): |
| 247 |
return |
| 248 |
# is there something to read? |
| 249 |
if not self.env["runtime"]["ScreenManager"].is_delta(ignoreSpace=True): |
| 250 |
return |
| 251 |
if self.env["screen"].get("new_delta_is_typing", False): |
| 252 |
return |
| 253 |
|
| 254 |
delta_text = self.env["screen"]["new_delta"] |
| 255 |
|
| 256 |
if self.env["runtime"]["ScreenManager"].is_screen_change(): |
| 257 |
self._reset_flood_state() |
| 258 |
self._pending_focus_text = "" |
| 259 |
self._pending_focus_time = 0 |
| 260 |
|
| 261 |
# Skip if tab completion already handled this delta |
| 262 |
if self._was_handled_by_tab_completion(delta_text): |
| 263 |
return |
| 264 |
|
| 265 |
# this must be a keyecho or something |
| 266 |
# if len(self.env['screen']['new_delta'].strip(' \n\t')) <= 1: |
| 267 |
x_move = abs( |
| 268 |
self.env["screen"]["new_cursor"]["x"] |
| 269 |
- self.env["screen"]["old_cursor"]["x"] |
| 270 |
) |
| 271 |
y_move = abs( |
| 272 |
self.env["screen"]["new_cursor"]["y"] |
| 273 |
- self.env["screen"]["old_cursor"]["y"] |
| 274 |
) |
| 275 |
|
| 276 |
if (x_move >= 1) and x_move == len(delta_text): |
| 277 |
# if len(self.env['screen']['new_delta'].strip(' \n\t0123456789')) |
| 278 |
# <= 2: |
| 279 |
if "\n" not in delta_text: |
| 280 |
return |
| 281 |
|
| 282 |
if self._is_transient_focus_only_update(delta_text): |
| 283 |
self._store_pending_focus_text(delta_text) |
| 284 |
return |
| 285 |
|
| 286 |
rapid = self._is_rapid_updates() |
| 287 |
high_volume = self._is_high_volume(delta_text) |
| 288 |
|
| 289 |
if (rapid and high_volume) or self._in_flood_mode: |
| 290 |
if not self._in_flood_mode: |
| 291 |
self._last_flush_time = time.time() |
| 292 |
self._in_flood_mode = True |
| 293 |
|
| 294 |
self._add_to_batch(delta_text) |
| 295 |
|
| 296 |
interval = self.env["runtime"][ |
| 297 |
"SettingsManager" |
| 298 |
].get_setting_as_float("speech", "batch_flush_interval") |
| 299 |
if time.time() - self._last_flush_time >= interval: |
| 300 |
self._flush_batch() |
| 301 |
|
| 302 |
if not rapid or not high_volume: |
| 303 |
if self._batched_text: |
| 304 |
self._flush_batch() |
| 305 |
self._in_flood_mode = False |
| 306 |
return |
| 307 |
|
| 308 |
# print(x_move, y_move, len(self.env['screen']['new_delta']), len(self.env['screen']['newNegativeDelta'])) |
| 309 |
delta_text = self._prefer_inserted_lower_screen_text(delta_text) |
| 310 |
pending_focus_text = self._take_pending_focus_text() |
| 311 |
if pending_focus_text: |
| 312 |
delta_text = "\n".join([delta_text, pending_focus_text]) |
| 313 |
self.env["runtime"]["OutputManager"].present_text( |
| 314 |
delta_text, interrupt=False, flush=False |
| 315 |
) |
| 316 |
|
| 317 |
def set_callback(self, callback): |
| 318 |
pass |