| 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 collections import Counter |
| 8 |
|
| 9 |
from fenrirscreenreader.core import debug |
| 10 |
from fenrirscreenreader.core.i18n import _ |
| 11 |
|
| 12 |
|
| 13 |
class AttributeManager: |
| 14 |
def __init__(self): |
| 15 |
self.currAttributes = None |
| 16 |
self.prevAttributes = None |
| 17 |
self.currAttributeDelta = "" |
| 18 |
self.currAttributeCursor = None |
| 19 |
self.prefAttributeCursor = None |
| 20 |
self.init_default_attributes() |
| 21 |
self.prevLastCursorAttribute = None |
| 22 |
self.currLastCursorAttribute = None |
| 23 |
|
| 24 |
def initialize(self, environment): |
| 25 |
self.env = environment |
| 26 |
|
| 27 |
def shutdown(self): |
| 28 |
pass |
| 29 |
|
| 30 |
def set_last_cursor_attribute(self, lastCursorAttribute): |
| 31 |
self.prevLastCursorAttribute = self.currLastCursorAttribute |
| 32 |
self.currLastCursorAttribute = lastCursorAttribute |
| 33 |
|
| 34 |
def reset_last_cursor_attribute(self): |
| 35 |
self.prevLastCursorAttribute = None |
| 36 |
self.currLastCursorAttribute = None |
| 37 |
|
| 38 |
def is_last_cursor_attribute_change(self): |
| 39 |
if self.prevLastCursorAttribute is None: |
| 40 |
return False |
| 41 |
return self.prevLastCursorAttribute != self.currLastCursorAttribute |
| 42 |
|
| 43 |
def get_curr_attribute_cursor(self): |
| 44 |
return self.currAttributeCursor |
| 45 |
|
| 46 |
def is_attribute_cursor_active(self): |
| 47 |
return self.currAttributeCursor is not None |
| 48 |
|
| 49 |
def is_attribute_change(self): |
| 50 |
if not self.prevAttributes: |
| 51 |
return False |
| 52 |
return self.currAttributes != self.prevAttributes |
| 53 |
|
| 54 |
def reset_attribute_all(self): |
| 55 |
self.reset_attribute_delta() |
| 56 |
self.reset_attribute_cursor() |
| 57 |
|
| 58 |
def get_attribute_delta(self): |
| 59 |
return self.currAttributeDelta |
| 60 |
|
| 61 |
def reset_attribute_delta(self): |
| 62 |
self.currAttributeDelta = "" |
| 63 |
|
| 64 |
def set_attribute_delta(self, currAttributeDelta): |
| 65 |
self.currAttributeDelta = currAttributeDelta |
| 66 |
|
| 67 |
def reset_attribute_cursor(self): |
| 68 |
self.currAttributeCursor = None |
| 69 |
self.prefAttributeCursor = None |
| 70 |
|
| 71 |
def set_attribute_cursor(self, currAttributeCursor): |
| 72 |
self.prefAttributeCursor = self.currAttributeCursor |
| 73 |
self.currAttributeCursor = currAttributeCursor.copy() |
| 74 |
|
| 75 |
def reset_attributes(self, currAttributes): |
| 76 |
self.prevAttributes = None |
| 77 |
self.currAttributes = currAttributes |
| 78 |
|
| 79 |
def set_attributes(self, currAttributes): |
| 80 |
self.prevAttributes = self.currAttributes |
| 81 |
self.currAttributes = currAttributes.copy() |
| 82 |
|
| 83 |
def get_attribute_by_xy(self, x, y): |
| 84 |
if not self.currAttributes: |
| 85 |
return None |
| 86 |
if len(self.currAttributes) < y: |
| 87 |
return None |
| 88 |
if len(self.currAttributes[y]) < x - 1: |
| 89 |
return None |
| 90 |
try: |
| 91 |
return self.currAttributes[y][x] |
| 92 |
except KeyError: |
| 93 |
try: |
| 94 |
return self.defaultAttributes[0] |
| 95 |
except Exception as e: |
| 96 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 97 |
"AttributeManager get_attribute_by_xy: Error accessing default attributes: " |
| 98 |
+ str(e), |
| 99 |
debug.DebugLevel.ERROR, |
| 100 |
) |
| 101 |
return None |
| 102 |
|
| 103 |
def append_default_attributes(self, attribute): |
| 104 |
if not attribute: |
| 105 |
return |
| 106 |
if len(attribute) != 10: |
| 107 |
return |
| 108 |
self.defaultAttributes.append(attribute) |
| 109 |
|
| 110 |
def init_default_attributes(self): |
| 111 |
self.defaultAttributes = [None] |
| 112 |
self.defaultAttributes.append( |
| 113 |
[ |
| 114 |
"default", # fg |
| 115 |
"default", # bg |
| 116 |
False, # bold |
| 117 |
False, # italics |
| 118 |
False, # underscore |
| 119 |
False, # strikethrough |
| 120 |
False, # reverse |
| 121 |
False, # blink |
| 122 |
"default", # fontsize |
| 123 |
"default", # fontfamily |
| 124 |
] |
| 125 |
) # end attribute |
| 126 |
|
| 127 |
def is_default_attribute(self, attribute): |
| 128 |
return attribute in self.defaultAttributes |
| 129 |
|
| 130 |
def has_attributes(self, cursor, update=True): |
| 131 |
if not cursor: |
| 132 |
return False |
| 133 |
cursor_pos = cursor.copy() |
| 134 |
try: |
| 135 |
attribute = self.get_attribute_by_xy( |
| 136 |
cursor_pos["x"], cursor_pos["y"] |
| 137 |
) |
| 138 |
|
| 139 |
if update: |
| 140 |
self.set_last_cursor_attribute(attribute) |
| 141 |
if not self.is_last_cursor_attribute_change(): |
| 142 |
return False |
| 143 |
|
| 144 |
if self.is_default_attribute(attribute): |
| 145 |
return False |
| 146 |
|
| 147 |
except Exception as e: |
| 148 |
return False |
| 149 |
return True |
| 150 |
|
| 151 |
def format_attributes(self, attribute, attribute_format_string=""): |
| 152 |
# "black", |
| 153 |
# "red", |
| 154 |
# "green", |
| 155 |
# "brown", |
| 156 |
# "blue", |
| 157 |
# "magenta", |
| 158 |
# "cyan", |
| 159 |
# "white", |
| 160 |
# "default" # white. |
| 161 |
# _order_ |
| 162 |
# "fg", |
| 163 |
# "bg", |
| 164 |
# "bold", |
| 165 |
# "italics", |
| 166 |
# "underscore", |
| 167 |
# "strikethrough", |
| 168 |
# "reverse", |
| 169 |
# "blink" |
| 170 |
# "fontsieze" |
| 171 |
# "fontfamily" |
| 172 |
if attribute_format_string == "": |
| 173 |
attribute_format_string = self.env["runtime"][ |
| 174 |
"SettingsManager" |
| 175 |
].get_setting("general", "attribute_format_string") |
| 176 |
if not attribute_format_string: |
| 177 |
return "" |
| 178 |
if attribute_format_string == "": |
| 179 |
return "" |
| 180 |
if not attribute: |
| 181 |
return "" |
| 182 |
if len(attribute) != 10: |
| 183 |
return "" |
| 184 |
|
| 185 |
# 0 FG color (name) |
| 186 |
try: |
| 187 |
attribute_format_string = attribute_format_string.replace( |
| 188 |
"fenrirFGColor", _(attribute[0]) |
| 189 |
) |
| 190 |
except Exception as e: |
| 191 |
attribute_format_string = attribute_format_string.replace( |
| 192 |
"fenrirFGColor", "" |
| 193 |
) |
| 194 |
|
| 195 |
# 1 BG color (name) |
| 196 |
try: |
| 197 |
attribute_format_string = attribute_format_string.replace( |
| 198 |
"fenrirBGColor", _(attribute[1]) |
| 199 |
) |
| 200 |
except Exception as e: |
| 201 |
attribute_format_string = attribute_format_string.replace( |
| 202 |
"fenrirBGColor", "" |
| 203 |
) |
| 204 |
|
| 205 |
# 2 bold (True/ False) |
| 206 |
try: |
| 207 |
if attribute[2]: |
| 208 |
attribute_format_string = attribute_format_string.replace( |
| 209 |
"fenrirBold", _("bold") |
| 210 |
) |
| 211 |
except Exception as e: |
| 212 |
pass |
| 213 |
attribute_format_string = attribute_format_string.replace( |
| 214 |
"fenrirBold", "" |
| 215 |
) |
| 216 |
|
| 217 |
# 3 italics (True/ False) |
| 218 |
try: |
| 219 |
if attribute[3]: |
| 220 |
attribute_format_string = attribute_format_string.replace( |
| 221 |
"fenrirItalics", _("italic") |
| 222 |
) |
| 223 |
except Exception as e: |
| 224 |
pass |
| 225 |
attribute_format_string = attribute_format_string.replace( |
| 226 |
"fenrirItalics", "" |
| 227 |
) |
| 228 |
|
| 229 |
# 4 underline (True/ False) |
| 230 |
try: |
| 231 |
if attribute[4]: |
| 232 |
attribute_format_string = attribute_format_string.replace( |
| 233 |
"fenrirUnderline", _("underline") |
| 234 |
) |
| 235 |
except Exception as e: |
| 236 |
pass |
| 237 |
attribute_format_string = attribute_format_string.replace( |
| 238 |
"fenrirUnderline", "" |
| 239 |
) |
| 240 |
|
| 241 |
# 5 strikethrough (True/ False) |
| 242 |
try: |
| 243 |
if attribute[5]: |
| 244 |
attribute_format_string = attribute_format_string.replace( |
| 245 |
"fenrirStrikethrough", _("strikethrough") |
| 246 |
) |
| 247 |
except Exception as e: |
| 248 |
pass |
| 249 |
attribute_format_string = attribute_format_string.replace( |
| 250 |
"fenrirStrikethrough", "" |
| 251 |
) |
| 252 |
|
| 253 |
# 6 reverse (True/ False) |
| 254 |
try: |
| 255 |
if attribute[6]: |
| 256 |
attribute_format_string = attribute_format_string.replace( |
| 257 |
"fenrirReverse", _("reverse") |
| 258 |
) |
| 259 |
except Exception as e: |
| 260 |
pass |
| 261 |
attribute_format_string = attribute_format_string.replace( |
| 262 |
"fenrirReverse", "" |
| 263 |
) |
| 264 |
|
| 265 |
# 7 blink (True/ False) |
| 266 |
try: |
| 267 |
if attribute[7]: |
| 268 |
attribute_format_string = attribute_format_string.replace( |
| 269 |
"fenrirBlink", _("blink") |
| 270 |
) |
| 271 |
except Exception as e: |
| 272 |
pass |
| 273 |
attribute_format_string = attribute_format_string.replace( |
| 274 |
"fenrirBlink", "" |
| 275 |
) |
| 276 |
|
| 277 |
# 8 font size (int/ string) |
| 278 |
try: |
| 279 |
try: |
| 280 |
attribute_format_string = attribute_format_string.replace( |
| 281 |
"fenrirFontSize", int(attribute[8]) |
| 282 |
) |
| 283 |
except Exception as e: |
| 284 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 285 |
"AttributeManager formatAttributeToString: Error formatting font size as int: " |
| 286 |
+ str(e), |
| 287 |
debug.DebugLevel.ERROR, |
| 288 |
) |
| 289 |
try: |
| 290 |
attribute_format_string = attribute_format_string.replace( |
| 291 |
"fenrirFontSize", str(attribute[8]) |
| 292 |
) |
| 293 |
except Exception as e: |
| 294 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 295 |
"AttributeManager formatAttributeToString: Error formatting font size as string: " |
| 296 |
+ str(e), |
| 297 |
debug.DebugLevel.ERROR, |
| 298 |
) |
| 299 |
except Exception as e: |
| 300 |
pass |
| 301 |
attribute_format_string = attribute_format_string.replace( |
| 302 |
"fenrirFontSize", _("default") |
| 303 |
) |
| 304 |
|
| 305 |
# 9 font family (string) |
| 306 |
try: |
| 307 |
attribute_format_string = attribute_format_string.replace( |
| 308 |
"fenrirFont", attribute[9] |
| 309 |
) |
| 310 |
except Exception as e: |
| 311 |
pass |
| 312 |
attribute_format_string = attribute_format_string.replace( |
| 313 |
"fenrirFont", _("default") |
| 314 |
) |
| 315 |
|
| 316 |
return attribute_format_string |
| 317 |
|
| 318 |
def track_highlights(self): |
| 319 |
""" |
| 320 |
Detects text with changed attributes (highlighting) between screen updates. |
| 321 |
|
| 322 |
This is crucial for screen readers to announce when text becomes highlighted, |
| 323 |
selected, or changes visual emphasis (bold, reverse video, color changes, etc.) |
| 324 |
|
| 325 |
Enhanced version includes bracket pattern detection for better context in applications |
| 326 |
like ninjam that use patterns like [X]mute or [ ]mute. |
| 327 |
|
| 328 |
Returns: |
| 329 |
tuple: (highlighted_text, cursor_position) |
| 330 |
- highlighted_text: string of characters that gained highlighting |
| 331 |
- cursor_position: dict {'x': col, 'y': row} of first highlighted char |
| 332 |
""" |
| 333 |
result = "" |
| 334 |
curr_cursor = None |
| 335 |
|
| 336 |
# Early exit conditions - no attribute comparison possible |
| 337 |
if self.prevAttributes is None: |
| 338 |
# First screen load - no previous attributes to compare against |
| 339 |
return result, curr_cursor |
| 340 |
if self.prevAttributes == self.currAttributes: |
| 341 |
# No attribute changes detected |
| 342 |
return result, curr_cursor |
| 343 |
if self.currAttributes is None: |
| 344 |
# Error condition - current attributes missing |
| 345 |
return result, curr_cursor |
| 346 |
if len(self.currAttributes) == 0: |
| 347 |
# Special case for PTY environments with no text content |
| 348 |
return result, curr_cursor |
| 349 |
|
| 350 |
# Get current screen text to correlate with attribute changes |
| 351 |
text = self.env["runtime"]["ScreenManager"].get_screen_text() |
| 352 |
text_lines = text.split("\n") |
| 353 |
|
| 354 |
# Sanity check: text lines must match attribute array dimensions |
| 355 |
if len(text_lines) != len(self.currAttributes): |
| 356 |
return result, curr_cursor |
| 357 |
|
| 358 |
# Track highlighted positions for context analysis |
| 359 |
highlighted_positions = [] |
| 360 |
|
| 361 |
# Compare attributes line by line, character by character |
| 362 |
for line in range(len(self.prevAttributes)): |
| 363 |
if self.prevAttributes[line] != self.currAttributes[line]: |
| 364 |
# This line has attribute changes - examine each character |
| 365 |
# position |
| 366 |
for column in range(len(self.prevAttributes[line])): |
| 367 |
if ( |
| 368 |
self.prevAttributes[line][column] |
| 369 |
== self.currAttributes[line][column] |
| 370 |
): |
| 371 |
# No change at this position |
| 372 |
continue |
| 373 |
# Attribute changed at this position - check if it's worth |
| 374 |
# announcing |
| 375 |
if self.is_useful_for_tracking( |
| 376 |
line, column, self.currAttributes, self.prevAttributes |
| 377 |
): |
| 378 |
# First highlighted character becomes cursor position |
| 379 |
# for navigation |
| 380 |
if not curr_cursor: |
| 381 |
curr_cursor = {"x": column, "y": line} |
| 382 |
# Store position for context analysis |
| 383 |
highlighted_positions.append((line, column)) |
| 384 |
# Accumulate highlighted characters |
| 385 |
result += text_lines[line][column] |
| 386 |
# Add space between lines of highlighted text for speech |
| 387 |
# clarity |
| 388 |
result += " " |
| 389 |
|
| 390 |
# Enhanced bracket pattern detection for better context |
| 391 |
if highlighted_positions: |
| 392 |
try: |
| 393 |
enhanced_result = self._detect_bracket_context(text_lines, highlighted_positions, result) |
| 394 |
if enhanced_result and enhanced_result != result: |
| 395 |
# Debug logging for bracket detection |
| 396 |
try: |
| 397 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 398 |
f"AttributeManager bracket detection: Original='{result}' Enhanced='{enhanced_result}'", |
| 399 |
debug.DebugLevel.INFO |
| 400 |
) |
| 401 |
except Exception: |
| 402 |
pass # Don't let debug logging break functionality |
| 403 |
result = enhanced_result |
| 404 |
except Exception as e: |
| 405 |
# If bracket detection fails, fall back to original result |
| 406 |
try: |
| 407 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 408 |
f"AttributeManager bracket detection error: {e}", |
| 409 |
debug.DebugLevel.ERROR |
| 410 |
) |
| 411 |
except Exception: |
| 412 |
pass # Don't let debug logging break functionality |
| 413 |
|
| 414 |
return result, curr_cursor |
| 415 |
|
| 416 |
def _detect_bracket_context(self, text_lines, highlighted_positions, original_result): |
| 417 |
""" |
| 418 |
Analyzes highlighted positions to detect bracket patterns and provide better context. |
| 419 |
|
| 420 |
This method specifically looks for patterns like [X]mute, [ ]mute, [X]xmit, etc. |
| 421 |
that are common in applications like ninjam where the bracket content indicates |
| 422 |
state but the meaningful context is the surrounding text. |
| 423 |
|
| 424 |
Args: |
| 425 |
text_lines: List of text lines from the screen |
| 426 |
highlighted_positions: List of (line, column) tuples of highlighted characters |
| 427 |
original_result: The original highlighted text result |
| 428 |
|
| 429 |
Returns: |
| 430 |
str: Enhanced result with context, or None if no bracket pattern detected |
| 431 |
""" |
| 432 |
if not highlighted_positions: |
| 433 |
return None |
| 434 |
|
| 435 |
# Group positions by line for easier analysis |
| 436 |
positions_by_line = {} |
| 437 |
for line, column in highlighted_positions: |
| 438 |
if line not in positions_by_line: |
| 439 |
positions_by_line[line] = [] |
| 440 |
positions_by_line[line].append(column) |
| 441 |
|
| 442 |
enhanced_results = [] |
| 443 |
|
| 444 |
for line_num, columns in positions_by_line.items(): |
| 445 |
if line_num >= len(text_lines): |
| 446 |
continue |
| 447 |
|
| 448 |
line_text = text_lines[line_num] |
| 449 |
columns.sort() # Process columns in order |
| 450 |
|
| 451 |
# Look for bracket patterns in this line |
| 452 |
bracket_context = self._analyze_bracket_pattern(line_text, columns) |
| 453 |
if bracket_context: |
| 454 |
enhanced_results.append(bracket_context) |
| 455 |
|
| 456 |
# If we found enhanced context, return it; otherwise return None to use original |
| 457 |
if enhanced_results: |
| 458 |
# Remove duplicates while preserving order |
| 459 |
unique_results = [] |
| 460 |
seen = set() |
| 461 |
for result in enhanced_results: |
| 462 |
if result not in seen: |
| 463 |
unique_results.append(result) |
| 464 |
seen.add(result) |
| 465 |
return " ".join(unique_results) |
| 466 |
return None |
| 467 |
|
| 468 |
def _analyze_bracket_pattern(self, line_text, highlighted_columns): |
| 469 |
""" |
| 470 |
Analyzes a single line for bracket patterns around highlighted positions. |
| 471 |
|
| 472 |
Looks for patterns like: |
| 473 |
- master: [X]mute -> "master mute on" |
| 474 |
- metronome: [ ]mute -> "metronome mute off" |
| 475 |
- [Left] [X]xmit -> "Left xmit on" |
| 476 |
|
| 477 |
Args: |
| 478 |
line_text: The text of the line |
| 479 |
highlighted_columns: List of column positions that are highlighted |
| 480 |
|
| 481 |
Returns: |
| 482 |
str: Context-aware description, or None if no pattern found |
| 483 |
""" |
| 484 |
if not line_text or not highlighted_columns: |
| 485 |
return None |
| 486 |
|
| 487 |
# Look for bracket patterns around highlighted positions |
| 488 |
# Process columns in order and only return the first meaningful match |
| 489 |
for col in highlighted_columns: |
| 490 |
bracket_info = self._find_bracket_at_position(line_text, col) |
| 491 |
if bracket_info: |
| 492 |
bracket_start, bracket_end, bracket_content = bracket_info |
| 493 |
|
| 494 |
# Get context before and after the bracket |
| 495 |
context_before = self._get_context_before(line_text, bracket_start) |
| 496 |
context_after = self._get_context_after(line_text, bracket_end) |
| 497 |
|
| 498 |
# Build meaningful description |
| 499 |
description = self._build_bracket_description( |
| 500 |
context_before, bracket_content, context_after |
| 501 |
) |
| 502 |
|
| 503 |
# Only return if we have meaningful context (not just state) |
| 504 |
if description and (context_before or context_after): |
| 505 |
return description |
| 506 |
|
| 507 |
return None |
| 508 |
|
| 509 |
def _find_bracket_at_position(self, line_text, position): |
| 510 |
""" |
| 511 |
Determines if a position is within a bracket pattern like [X] or [ ]. |
| 512 |
|
| 513 |
Args: |
| 514 |
line_text: The text line |
| 515 |
position: Column position to check |
| 516 |
|
| 517 |
Returns: |
| 518 |
tuple: (bracket_start, bracket_end, bracket_content) or None |
| 519 |
""" |
| 520 |
if position >= len(line_text): |
| 521 |
return None |
| 522 |
|
| 523 |
# Look for opening bracket before or at position |
| 524 |
bracket_start = None |
| 525 |
for i in range(max(0, position - 2), min(len(line_text), position + 3)): |
| 526 |
if line_text[i] == '[': |
| 527 |
bracket_start = i |
| 528 |
break |
| 529 |
|
| 530 |
if bracket_start is None: |
| 531 |
return None |
| 532 |
|
| 533 |
# Look for closing bracket after bracket_start |
| 534 |
bracket_end = None |
| 535 |
for i in range(bracket_start + 1, min(len(line_text), bracket_start + 5)): |
| 536 |
if line_text[i] == ']': |
| 537 |
bracket_end = i |
| 538 |
break |
| 539 |
|
| 540 |
if bracket_end is None: |
| 541 |
return None |
| 542 |
|
| 543 |
# Check if our position is within the bracket |
| 544 |
if bracket_start <= position <= bracket_end: |
| 545 |
bracket_content = line_text[bracket_start + 1:bracket_end] |
| 546 |
|
| 547 |
# Filter out brackets that are likely not controls |
| 548 |
# Skip brackets that contain complex content like "[-10.5dB center]" |
| 549 |
if len(bracket_content) > 3 and ('dB' in bracket_content or 'Hz' in bracket_content): |
| 550 |
return None |
| 551 |
|
| 552 |
return bracket_start, bracket_end, bracket_content |
| 553 |
|
| 554 |
return None |
| 555 |
|
| 556 |
def _get_context_before(self, line_text, bracket_start): |
| 557 |
""" |
| 558 |
Gets meaningful context words before a bracket. |
| 559 |
|
| 560 |
Args: |
| 561 |
line_text: The text line |
| 562 |
bracket_start: Position of opening bracket |
| 563 |
|
| 564 |
Returns: |
| 565 |
str: Context words before bracket, or empty string |
| 566 |
""" |
| 567 |
if bracket_start <= 0: |
| 568 |
return "" |
| 569 |
|
| 570 |
# Get text before bracket |
| 571 |
before_text = line_text[:bracket_start].rstrip() |
| 572 |
|
| 573 |
# Special handling for common ninjam patterns |
| 574 |
# Look for patterns like "master: " or "metronome: " or bracketed labels |
| 575 |
|
| 576 |
# Check if we have a colon-separated label immediately before |
| 577 |
if ':' in before_text: |
| 578 |
# Get the last colon-separated part |
| 579 |
parts = before_text.split('|') # Split by pipe first |
| 580 |
last_part = parts[-1].strip() |
| 581 |
|
| 582 |
if ':' in last_part: |
| 583 |
# Extract the label before the colon |
| 584 |
label_parts = last_part.split(':') |
| 585 |
if len(label_parts) >= 2: |
| 586 |
return label_parts[-2].strip() |
| 587 |
|
| 588 |
# Look for bracketed content that might be a label |
| 589 |
# Pattern: [Something] [X]target -> "Something" |
| 590 |
bracket_matches = [] |
| 591 |
i = 0 |
| 592 |
while i < len(before_text): |
| 593 |
if before_text[i] == '[': |
| 594 |
start = i |
| 595 |
i += 1 |
| 596 |
while i < len(before_text) and before_text[i] != ']': |
| 597 |
i += 1 |
| 598 |
if i < len(before_text): # Found closing bracket |
| 599 |
content = before_text[start+1:i] |
| 600 |
if content.strip(): |
| 601 |
bracket_matches.append(content.strip()) |
| 602 |
i += 1 |
| 603 |
|
| 604 |
# If we found bracketed content, use the last one |
| 605 |
if bracket_matches: |
| 606 |
return bracket_matches[-1] |
| 607 |
|
| 608 |
# Fall back to removing bracketed content and getting words |
| 609 |
cleaned_text = "" |
| 610 |
bracket_level = 0 |
| 611 |
for char in before_text: |
| 612 |
if char == '[': |
| 613 |
bracket_level += 1 |
| 614 |
elif char == ']': |
| 615 |
bracket_level -= 1 |
| 616 |
elif bracket_level == 0: |
| 617 |
cleaned_text += char |
| 618 |
|
| 619 |
# Clean up separators and get meaningful words |
| 620 |
words = [] |
| 621 |
for word in cleaned_text.replace(':', '').replace('|', '').strip().split(): |
| 622 |
if word and not word.startswith('[') and not word.endswith(']'): |
| 623 |
words.append(word) |
| 624 |
|
| 625 |
# Return last word as context |
| 626 |
if words: |
| 627 |
return words[-1] |
| 628 |
|
| 629 |
return "" |
| 630 |
|
| 631 |
def _get_context_after(self, line_text, bracket_end): |
| 632 |
""" |
| 633 |
Gets meaningful context words after a bracket. |
| 634 |
|
| 635 |
Args: |
| 636 |
line_text: The text line |
| 637 |
bracket_end: Position of closing bracket |
| 638 |
|
| 639 |
Returns: |
| 640 |
str: Context words after bracket, or empty string |
| 641 |
""" |
| 642 |
if bracket_end >= len(line_text) - 1: |
| 643 |
return "" |
| 644 |
|
| 645 |
# Get text after bracket and find first meaningful word |
| 646 |
after_text = line_text[bracket_end + 1:].lstrip() |
| 647 |
|
| 648 |
# Get first word, removing common separators |
| 649 |
words = after_text.replace(':', '').replace('|', '').strip().split() |
| 650 |
|
| 651 |
if words: |
| 652 |
return words[0] |
| 653 |
|
| 654 |
return "" |
| 655 |
|
| 656 |
def _build_bracket_description(self, context_before, bracket_content, context_after): |
| 657 |
""" |
| 658 |
Builds a human-readable description from bracket context. |
| 659 |
|
| 660 |
Args: |
| 661 |
context_before: Words before the bracket |
| 662 |
bracket_content: Content inside brackets (X, space, etc.) |
| 663 |
context_after: Words after the bracket |
| 664 |
|
| 665 |
Returns: |
| 666 |
str: Human-readable description |
| 667 |
""" |
| 668 |
# Clean up bracket content |
| 669 |
bracket_content = bracket_content.strip() |
| 670 |
|
| 671 |
# Determine state based on bracket content |
| 672 |
if bracket_content == 'X': |
| 673 |
state = "on" |
| 674 |
elif bracket_content == '' or bracket_content == ' ': |
| 675 |
state = "off" |
| 676 |
else: |
| 677 |
state = bracket_content # For other patterns |
| 678 |
|
| 679 |
# Build description prioritizing context_before, then context_after |
| 680 |
# Add spaces between components for better speech clarity |
| 681 |
components = [] |
| 682 |
|
| 683 |
if context_before: |
| 684 |
components.append(context_before) |
| 685 |
if context_after: |
| 686 |
components.append(context_after) |
| 687 |
components.append(state) |
| 688 |
|
| 689 |
# Join with spaces for better speech flow |
| 690 |
return " ".join(components) |
| 691 |
|
| 692 |
def is_useful_for_tracking( |
| 693 |
self, |
| 694 |
line, |
| 695 |
column, |
| 696 |
currAttributes, |
| 697 |
prevAttributes, |
| 698 |
attribute=1, |
| 699 |
mode="zaxe", |
| 700 |
): |
| 701 |
""" |
| 702 |
Determines if an attribute change at a specific position is worth announcing. |
| 703 |
|
| 704 |
This prevents announcing every minor attribute change and focuses on meaningful |
| 705 |
highlighting that indicates user selections, focus changes, or important emphasis. |
| 706 |
|
| 707 |
Args: |
| 708 |
line, column: Position of the attribute change |
| 709 |
currAttributes, prevAttributes: Current and previous attribute arrays |
| 710 |
attribute: Which attribute to examine (1=background color by default) |
| 711 |
mode: Detection algorithm ('zaxe', 'default', 'barrier') |
| 712 |
|
| 713 |
Returns: |
| 714 |
bool: True if this attribute change should be announced to user |
| 715 |
""" |
| 716 |
# Sanity checks for valid position and sufficient screen content |
| 717 |
if len(currAttributes) <= 3: |
| 718 |
return False |
| 719 |
if line < 0: |
| 720 |
return False |
| 721 |
if line > len(currAttributes): |
| 722 |
return False |
| 723 |
|
| 724 |
useful = False |
| 725 |
|
| 726 |
if mode == "default": |
| 727 |
# Simple mode: announce any non-default attribute |
| 728 |
useful = not self.is_default_attribute( |
| 729 |
currAttributes[line][column] |
| 730 |
) |
| 731 |
|
| 732 |
elif (mode == "zaxe") or (mode == ""): |
| 733 |
# Context-aware mode: only announce attributes that stand out from surroundings |
| 734 |
# This prevents announcing entire blocks of highlighted text character by character |
| 735 |
# by checking if the attribute differs from adjacent lines |
| 736 |
|
| 737 |
if line == 0: |
| 738 |
# Top line: compare against lines below |
| 739 |
useful = ( |
| 740 |
currAttributes[line][column][attribute] |
| 741 |
!= currAttributes[line + 1][column][attribute] |
| 742 |
) and ( |
| 743 |
currAttributes[line][column][attribute] |
| 744 |
!= currAttributes[line + 2][column][attribute] |
| 745 |
) |
| 746 |
elif line >= len(prevAttributes): |
| 747 |
# Bottom line: compare against lines above |
| 748 |
useful = ( |
| 749 |
currAttributes[line][column][attribute] |
| 750 |
!= currAttributes[line - 1][column][attribute] |
| 751 |
) and ( |
| 752 |
currAttributes[line][column][attribute] |
| 753 |
!= currAttributes[line - 2][column][attribute] |
| 754 |
) |
| 755 |
else: |
| 756 |
# Middle lines: compare against both directions |
| 757 |
useful = ( |
| 758 |
currAttributes[line][column][attribute] |
| 759 |
!= currAttributes[line + 1][column][attribute] |
| 760 |
) and ( |
| 761 |
currAttributes[line][column][attribute] |
| 762 |
!= currAttributes[line - 1][column][attribute] |
| 763 |
) |
| 764 |
|
| 765 |
elif mode == "barrier": |
| 766 |
# Barrier mode: future enhancement for detecting screen |
| 767 |
# boundaries/separators |
| 768 |
useful = True |
| 769 |
|
| 770 |
return useful |