| 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 time |
| 9 |
|
| 10 |
|
| 11 |
class TabCompletionManager: |
| 12 |
def __init__(self, timeout=0.5): |
| 13 |
self.timeout = timeout |
| 14 |
|
| 15 |
def initialize(self, environment): |
| 16 |
self.env = environment |
| 17 |
self._ensure_state() |
| 18 |
|
| 19 |
def _ensure_state(self): |
| 20 |
if "tabCompletion" not in self.env["commandBuffer"]: |
| 21 |
self.env["commandBuffer"]["tabCompletion"] = {} |
| 22 |
state = self.env["commandBuffer"]["tabCompletion"] |
| 23 |
state.setdefault("pending", None) |
| 24 |
state.setdefault("lastProcessedDelta", "") |
| 25 |
state.setdefault("lastProcessedTime", 0) |
| 26 |
|
| 27 |
def capture_if_tab(self): |
| 28 |
if not self._last_event_is_tab_press(): |
| 29 |
return |
| 30 |
|
| 31 |
self._ensure_state() |
| 32 |
self.env["commandBuffer"]["tabCompletion"]["pending"] = { |
| 33 |
"timestamp": time.time(), |
| 34 |
"cursor": self.env["screen"]["new_cursor"].copy(), |
| 35 |
"content": self.env["screen"]["new_content_text"], |
| 36 |
"screen": self.env["screen"]["newTTY"], |
| 37 |
} |
| 38 |
|
| 39 |
def process_update(self): |
| 40 |
self._ensure_state() |
| 41 |
state = self.env["commandBuffer"]["tabCompletion"] |
| 42 |
if self._was_recently_processed(state): |
| 43 |
return "" |
| 44 |
|
| 45 |
pending = state.get("pending") |
| 46 |
if not pending: |
| 47 |
pending = self._build_pending_from_recent_tab_update() |
| 48 |
if not pending: |
| 49 |
return "" |
| 50 |
|
| 51 |
if time.time() - pending["timestamp"] > self.timeout: |
| 52 |
state["pending"] = None |
| 53 |
return "" |
| 54 |
|
| 55 |
if pending.get("screen") != self.env["screen"]["newTTY"]: |
| 56 |
state["pending"] = None |
| 57 |
return "" |
| 58 |
|
| 59 |
spoken_text = self._get_completion_text(pending) |
| 60 |
if not spoken_text: |
| 61 |
return "" |
| 62 |
|
| 63 |
state["pending"] = None |
| 64 |
state["lastProcessedDelta"] = self.env["screen"]["new_delta"] |
| 65 |
state["lastProcessedTime"] = time.time() |
| 66 |
return spoken_text |
| 67 |
|
| 68 |
def _was_recently_processed(self, state): |
| 69 |
if state.get("lastProcessedDelta") != self.env["screen"]["new_delta"]: |
| 70 |
return False |
| 71 |
last_time = state.get("lastProcessedTime") |
| 72 |
if not last_time: |
| 73 |
return False |
| 74 |
return time.time() - last_time <= self.timeout |
| 75 |
|
| 76 |
def _build_pending_from_recent_tab_update(self): |
| 77 |
if not self._recent_tab_input(): |
| 78 |
return None |
| 79 |
return { |
| 80 |
"timestamp": time.time(), |
| 81 |
"cursor": self.env["screen"]["old_cursor"].copy(), |
| 82 |
"content": self.env["screen"]["old_content_text"], |
| 83 |
"screen": self.env["screen"]["newTTY"], |
| 84 |
} |
| 85 |
|
| 86 |
def _recent_tab_input(self): |
| 87 |
input_manager = self.env["runtime"]["InputManager"] |
| 88 |
if input_manager.get_last_deepest_input() in [["KEY_TAB"]]: |
| 89 |
return True |
| 90 |
|
| 91 |
last_event = input_manager.get_last_event() |
| 92 |
if not last_event or last_event.get("event_name") != "KEY_TAB": |
| 93 |
return False |
| 94 |
|
| 95 |
try: |
| 96 |
return ( |
| 97 |
time.time() - input_manager.get_last_input_time() |
| 98 |
<= self.timeout |
| 99 |
) |
| 100 |
except Exception: |
| 101 |
return False |
| 102 |
|
| 103 |
def _last_event_is_tab_press(self): |
| 104 |
input_manager = self.env["runtime"]["InputManager"] |
| 105 |
last_event = input_manager.get_last_event() |
| 106 |
if not last_event: |
| 107 |
return False |
| 108 |
return ( |
| 109 |
last_event.get("event_name") == "KEY_TAB" |
| 110 |
and last_event.get("event_state") == 1 |
| 111 |
) |
| 112 |
|
| 113 |
def _get_completion_text(self, pending): |
| 114 |
old_lines = pending["content"].split("\n") |
| 115 |
new_lines = self.env["screen"]["new_content_text"].split("\n") |
| 116 |
cursor = pending["cursor"] |
| 117 |
cursor_y = cursor["y"] |
| 118 |
|
| 119 |
if cursor_y < len(old_lines) and cursor_y < len(new_lines): |
| 120 |
inserted_text = self._get_cursor_line_inserted_text( |
| 121 |
old_lines[cursor_y], |
| 122 |
new_lines[cursor_y], |
| 123 |
cursor["x"], |
| 124 |
self.env["screen"]["new_cursor"]["x"], |
| 125 |
) |
| 126 |
if inserted_text: |
| 127 |
return self._clean_text(inserted_text) |
| 128 |
|
| 129 |
candidate_text = self._get_candidate_text( |
| 130 |
old_lines, new_lines, cursor_y |
| 131 |
) |
| 132 |
if candidate_text: |
| 133 |
return self._clean_text(candidate_text) |
| 134 |
|
| 135 |
return "" |
| 136 |
|
| 137 |
def _get_cursor_line_inserted_text( |
| 138 |
self, old_line, new_line, old_cursor_x, new_cursor_x |
| 139 |
): |
| 140 |
if new_cursor_x <= old_cursor_x: |
| 141 |
return "" |
| 142 |
if old_line == new_line: |
| 143 |
return "" |
| 144 |
|
| 145 |
matcher = difflib.SequenceMatcher( |
| 146 |
None, old_line, new_line, autojunk=False |
| 147 |
) |
| 148 |
inserted_parts = [] |
| 149 |
for ( |
| 150 |
tag, |
| 151 |
old_start, |
| 152 |
old_end, |
| 153 |
new_start, |
| 154 |
new_end, |
| 155 |
) in matcher.get_opcodes(): |
| 156 |
if tag == "equal": |
| 157 |
continue |
| 158 |
if tag == "insert" and old_cursor_x <= new_start <= new_cursor_x: |
| 159 |
inserted_parts.append(new_line[new_start:new_end]) |
| 160 |
continue |
| 161 |
if ( |
| 162 |
tag == "replace" |
| 163 |
and old_line[old_start:old_end].strip() == "" |
| 164 |
and old_cursor_x <= new_start <= new_cursor_x |
| 165 |
): |
| 166 |
inserted_parts.append(new_line[new_start:new_end]) |
| 167 |
continue |
| 168 |
if ( |
| 169 |
tag == "delete" |
| 170 |
and old_line[old_start:old_end].strip() == "" |
| 171 |
and old_start >= old_cursor_x |
| 172 |
): |
| 173 |
continue |
| 174 |
if old_start >= old_cursor_x: |
| 175 |
return "" |
| 176 |
|
| 177 |
return "".join(inserted_parts) |
| 178 |
|
| 179 |
def _get_candidate_text(self, old_lines, new_lines, cursor_y): |
| 180 |
old_cursor_line = ( |
| 181 |
old_lines[cursor_y].strip() if cursor_y < len(old_lines) else "" |
| 182 |
) |
| 183 |
return self._get_inserted_lines( |
| 184 |
old_lines, |
| 185 |
new_lines, |
| 186 |
self.env["screen"]["new_cursor"]["y"], |
| 187 |
old_cursor_line, |
| 188 |
) |
| 189 |
|
| 190 |
def _get_inserted_lines( |
| 191 |
self, old_lines, new_lines, new_cursor_y, old_cursor_line |
| 192 |
): |
| 193 |
matcher = difflib.SequenceMatcher( |
| 194 |
None, old_lines, new_lines, autojunk=False |
| 195 |
) |
| 196 |
inserted_lines = [] |
| 197 |
for ( |
| 198 |
tag, |
| 199 |
old_start, |
| 200 |
old_end, |
| 201 |
new_start, |
| 202 |
new_end, |
| 203 |
) in matcher.get_opcodes(): |
| 204 |
if tag not in ["insert", "replace"]: |
| 205 |
continue |
| 206 |
if new_start > new_cursor_y: |
| 207 |
continue |
| 208 |
if tag == "replace" and any( |
| 209 |
line.strip() for line in old_lines[old_start:old_end] |
| 210 |
): |
| 211 |
continue |
| 212 |
for line in new_lines[new_start:new_end]: |
| 213 |
stripped_line = line.strip() |
| 214 |
if stripped_line and stripped_line != old_cursor_line: |
| 215 |
inserted_lines.append(line.rstrip()) |
| 216 |
return "\n".join(inserted_lines) |
| 217 |
|
| 218 |
def _clean_text(self, text): |
| 219 |
if not text: |
| 220 |
return "" |
| 221 |
if len(text.strip()) != len(text) and text.strip() != "": |
| 222 |
return text.strip() |
| 223 |
return text |