| 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 |
from fenrirscreenreader.core.i18n import _ |
| 9 |
|
| 10 |
|
| 11 |
class command: |
| 12 |
def __init__(self): |
| 13 |
pass |
| 14 |
|
| 15 |
def initialize(self, environment): |
| 16 |
self.env = environment |
| 17 |
|
| 18 |
def shutdown(self): |
| 19 |
pass |
| 20 |
|
| 21 |
def get_description(self): |
| 22 |
return "Detects progress patterns for progress bar monitoring" |
| 23 |
|
| 24 |
def run(self): |
| 25 |
# Only run if progress monitoring is enabled |
| 26 |
try: |
| 27 |
if ( |
| 28 |
"progress_monitoring" in self.env["commandBuffer"] |
| 29 |
and self.env["commandBuffer"]["progress_monitoring"] |
| 30 |
): |
| 31 |
# Check if current line is a prompt - if so, reset progress |
| 32 |
# state |
| 33 |
if self.is_current_line_prompt(): |
| 34 |
self.reset_progress_state() |
| 35 |
# Only check new incoming text (new_delta), but filter out |
| 36 |
# screen changes |
| 37 |
elif ( |
| 38 |
self.env["screen"]["new_delta"] |
| 39 |
and not self.env["screen"].get("new_delta_is_typing", False) |
| 40 |
and self.is_real_progress_update() |
| 41 |
): |
| 42 |
self.detect_progress(self.env["screen"]["new_delta"]) |
| 43 |
except Exception as e: |
| 44 |
# Log errors for debugging instead of silently ignoring |
| 45 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 46 |
"Progress detector error: " + str(e), |
| 47 |
debug.DebugLevel.ERROR, |
| 48 |
) |
| 49 |
|
| 50 |
def is_real_progress_update(self): |
| 51 |
"""Check if this is a real progress update vs screen change/window switch""" |
| 52 |
# If the screen/application changed, it's not a progress update |
| 53 |
if self.env["runtime"]["ScreenManager"].is_screen_change(): |
| 54 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 55 |
"Progress filter: screen change detected", debug.DebugLevel.INFO |
| 56 |
) |
| 57 |
return False |
| 58 |
|
| 59 |
# If there was a large cursor movement, it's likely navigation, not |
| 60 |
# progress |
| 61 |
if self.env["runtime"]["CursorManager"].is_cursor_vertical_move(): |
| 62 |
x_move = abs( |
| 63 |
self.env["screen"]["new_cursor"]["x"] |
| 64 |
- self.env["screen"]["old_cursor"]["x"] |
| 65 |
) |
| 66 |
y_move = abs( |
| 67 |
self.env["screen"]["new_cursor"]["y"] |
| 68 |
- self.env["screen"]["old_cursor"]["y"] |
| 69 |
) |
| 70 |
# Large movements suggest navigation, not progress output |
| 71 |
if y_move > 2 or x_move > 20: |
| 72 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 73 |
f"Progress filter: large cursor move y={y_move} x={x_move}", |
| 74 |
debug.DebugLevel.INFO, |
| 75 |
) |
| 76 |
return False |
| 77 |
|
| 78 |
# Check if delta is too large (screen change) vs small incremental |
| 79 |
# updates |
| 80 |
delta_text = self.env["screen"]["new_delta"] |
| 81 |
delta_length = len(delta_text) |
| 82 |
explicit_progress_lines = self.get_explicit_progress_lines(delta_text) |
| 83 |
if ( |
| 84 |
delta_length > 200 |
| 85 |
): # Allow longer progress lines such as terminal status output |
| 86 |
if not explicit_progress_lines: |
| 87 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 88 |
f"Progress filter: delta too long ({delta_length})", |
| 89 |
debug.DebugLevel.INFO, |
| 90 |
) |
| 91 |
return False |
| 92 |
|
| 93 |
# If delta contains newlines and is substantial, let incoming handler |
| 94 |
# deal with it to avoid interfering with multi-line text output |
| 95 |
if ( |
| 96 |
"\n" in delta_text |
| 97 |
and delta_length > 50 |
| 98 |
and not explicit_progress_lines |
| 99 |
): |
| 100 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 101 |
f"Progress filter: multiline delta ({delta_length} chars)", |
| 102 |
debug.DebugLevel.INFO, |
| 103 |
) |
| 104 |
return False |
| 105 |
|
| 106 |
# Check if current line looks like a prompt - progress unlikely during |
| 107 |
# prompts |
| 108 |
if self.is_current_line_prompt(): |
| 109 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 110 |
"Progress filter: prompt detected", debug.DebugLevel.INFO |
| 111 |
) |
| 112 |
return False |
| 113 |
|
| 114 |
return True |
| 115 |
|
| 116 |
def get_curl_classic_percentage(self, text): |
| 117 |
"""Return progress from a structurally valid curl classic row.""" |
| 118 |
import re |
| 119 |
|
| 120 |
size_field = r"\d+(?:\.\d+)?(?:[kKmMgGtT](?:i?[bB])?)?" |
| 121 |
time_field = r"(?:(?:\d+:)?\d{2}:\d{2}|--:--:--)" |
| 122 |
curl_classic_match = re.match( |
| 123 |
rf"^\s*(\d+)\s+{size_field}\s+" |
| 124 |
rf"(\d+)\s+{size_field}\s+" |
| 125 |
rf"(\d+)\s+{size_field}\s+" |
| 126 |
rf"{size_field}\s+{size_field}\s+" |
| 127 |
rf"{time_field}\s+{time_field}\s+{time_field}\s+" |
| 128 |
rf"{size_field}(?=\s|$)", |
| 129 |
text, |
| 130 |
) |
| 131 |
if not curl_classic_match: |
| 132 |
return None |
| 133 |
|
| 134 |
total_percentage = int(curl_classic_match.group(1)) |
| 135 |
received_percentage = int(curl_classic_match.group(2)) |
| 136 |
uploaded_percentage = int(curl_classic_match.group(3)) |
| 137 |
if not all( |
| 138 |
0 <= percentage <= 100 |
| 139 |
for percentage in ( |
| 140 |
total_percentage, |
| 141 |
received_percentage, |
| 142 |
uploaded_percentage, |
| 143 |
) |
| 144 |
): |
| 145 |
return None |
| 146 |
if total_percentage != received_percentage: |
| 147 |
return None |
| 148 |
return float(total_percentage) |
| 149 |
|
| 150 |
def is_explicit_progress_delta(self, text): |
| 151 |
"""Allow long single-line deltas that still look like progress output.""" |
| 152 |
import re |
| 153 |
|
| 154 |
if "\n" in text or self.contains_url(text): |
| 155 |
return False |
| 156 |
|
| 157 |
if self.get_curl_classic_percentage(text) is not None: |
| 158 |
return True |
| 159 |
|
| 160 |
has_percentage = re.search(r"(^|\s)\d+(?:\.\d+)?\s*%", text) |
| 161 |
if not has_percentage: |
| 162 |
return self.has_progress_status_fields(text) |
| 163 |
|
| 164 |
bracketed_bar = re.search( |
| 165 |
r"\[(?=[#=*>█▉▊▋▌▍▎▏▒▓░.\s-]{2,}\])" |
| 166 |
r"(?=[^\]]*[#=*>█▉▊▋▌▍▎▏▒▓░])" |
| 167 |
r"[#=*>█▉▊▋▌▍▎▏▒▓░.\s-]+\]", |
| 168 |
text, |
| 169 |
) |
| 170 |
pipe_bar = re.search( |
| 171 |
r"\|(?=[#=*>█▉▊▋▌▍▎▏▒▓░.\s-]{2,}\|)" |
| 172 |
r"(?=[^|]*[#=*>█▉▊▋▌▍▎▏▒▓░])" |
| 173 |
r"[#=*>█▉▊▋▌▍▎▏▒▓░.\s-]+\|", |
| 174 |
text, |
| 175 |
) |
| 176 |
transfer_rate = re.search( |
| 177 |
r"\b\d+(?:\.\d+)?\s*[kKmMgGtT](?:i?B)?/s\b", |
| 178 |
text, |
| 179 |
) |
| 180 |
return bool( |
| 181 |
bracketed_bar |
| 182 |
or pipe_bar |
| 183 |
or transfer_rate |
| 184 |
) |
| 185 |
|
| 186 |
def get_explicit_progress_lines(self, text): |
| 187 |
"""Return structurally explicit progress rows from a screen delta.""" |
| 188 |
return [ |
| 189 |
line |
| 190 |
for line in text.splitlines() |
| 191 |
if self.is_explicit_progress_delta(line) |
| 192 |
] |
| 193 |
|
| 194 |
def reset_progress_state(self): |
| 195 |
"""Reset progress state when a prompt is detected, allowing new progress operations to start fresh""" |
| 196 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 197 |
"Resetting progress state due to prompt detection", |
| 198 |
debug.DebugLevel.INFO, |
| 199 |
) |
| 200 |
self.env["commandBuffer"]["lastProgressValue"] = -1 |
| 201 |
self.env["commandBuffer"]["lastProgressTime"] = 0 |
| 202 |
|
| 203 |
def detect_progress(self, text): |
| 204 |
import re |
| 205 |
import time |
| 206 |
|
| 207 |
if "\n" in text: |
| 208 |
explicit_progress_lines = self.get_explicit_progress_lines(text) |
| 209 |
if not explicit_progress_lines: |
| 210 |
return |
| 211 |
# Parallel transfer tools commonly repaint several package rows, |
| 212 |
# followed by an aggregate row. One tone per update is enough, and |
| 213 |
# the final explicit row is normally that aggregate. |
| 214 |
text = explicit_progress_lines[-1] |
| 215 |
|
| 216 |
current_time = time.time() |
| 217 |
|
| 218 |
# Debug: Print what we're checking |
| 219 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 220 |
"Progress detector checking: '" + text + "'", debug.DebugLevel.INFO |
| 221 |
) |
| 222 |
|
| 223 |
# Filter out URLs to prevent false positives |
| 224 |
if self.contains_url(text): |
| 225 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 226 |
"Skipping progress detection - text contains URL", debug.DebugLevel.INFO |
| 227 |
) |
| 228 |
return |
| 229 |
|
| 230 |
# Note: Auto-disable on 100% completion removed to respect user |
| 231 |
# settings |
| 232 |
|
| 233 |
# Pattern 1: Percentage (50%, 25.5%, etc.) |
| 234 |
# Filter out common non-progress percentages (weather, system stats, |
| 235 |
# etc.) |
| 236 |
percent_match = re.search(r"(\d+(?:\.\d+)?)\s*%", text) |
| 237 |
if percent_match: |
| 238 |
percentage = float(percent_match.group(1)) |
| 239 |
# Only trigger on realistic progress percentages (0-100%) |
| 240 |
if 0 <= percentage <= 100: |
| 241 |
# Filter out weather/system stats that contain percentages |
| 242 |
if not re.search( |
| 243 |
r"\b(?:humidity|cpu|memory|disk|usage|temp|weather|forecast)\b", |
| 244 |
text, |
| 245 |
re.IGNORECASE, |
| 246 |
): |
| 247 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 248 |
"found percentage: " + str(percentage), |
| 249 |
debug.DebugLevel.INFO, |
| 250 |
) |
| 251 |
if ( |
| 252 |
percentage |
| 253 |
!= self.env["commandBuffer"]["lastProgressValue"] |
| 254 |
): |
| 255 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 256 |
"Playing tone for: " + str(percentage), |
| 257 |
debug.DebugLevel.INFO, |
| 258 |
) |
| 259 |
self.play_progress_tone(percentage) |
| 260 |
self.env["commandBuffer"][ |
| 261 |
"lastProgressValue" |
| 262 |
] = percentage |
| 263 |
self.env["commandBuffer"][ |
| 264 |
"lastProgressTime" |
| 265 |
] = current_time |
| 266 |
return |
| 267 |
|
| 268 |
# Pattern 1a2: Curl classic progress format (percentage without % symbol) |
| 269 |
percentage = self.get_curl_classic_percentage(text) |
| 270 |
if percentage is not None: |
| 271 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 272 |
"found curl classic percentage: " + str(percentage), |
| 273 |
debug.DebugLevel.INFO, |
| 274 |
) |
| 275 |
if percentage != self.env["commandBuffer"]["lastProgressValue"]: |
| 276 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 277 |
"Playing tone for curl: " + str(percentage), |
| 278 |
debug.DebugLevel.INFO, |
| 279 |
) |
| 280 |
self.play_progress_tone(percentage) |
| 281 |
self.env["commandBuffer"]["lastProgressValue"] = percentage |
| 282 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 283 |
return |
| 284 |
|
| 285 |
# Pattern 1b: Time/token activity (not percentage-based, so use single |
| 286 |
# beep) |
| 287 |
time_match = re.search(r"(?:(?:remaining|elapsed|left|ETA|eta)[:;\s]*(\d+)s|(\d+)s\s+(?:remaining|elapsed|left))", text, re.IGNORECASE) |
| 288 |
token_match = re.search(r"(?:processing|generating|used|consumed)\s+(\d+)\s+tokens", text, re.IGNORECASE) |
| 289 |
# Pattern 1c: dd command output (bytes copied with transfer rate) |
| 290 |
dd_match = re.search(r"\d+\s+bytes.*copied.*\d+\s+s.*[kMGT]?B/s", text) |
| 291 |
# Pattern 1d: Curl-style transfer data (bytes, speed indicators - legacy) |
| 292 |
curl_match = re.search( |
| 293 |
r"(\d+\s+\d+\s+\d+\s+\d+.*?(?:k|M|G)?.*?--:--:--|Speed)", text |
| 294 |
) |
| 295 |
# Pattern 1e: General transfer progress (size, rate, time patterns) |
| 296 |
transfer_match = re.search( |
| 297 |
r"\d+\s+\d+[kMGT]?\s+\d+\s+\d+[kMGT]?.*?\d+\.\d+[kMGT].*?\d+:\d+:\d+", text |
| 298 |
) |
| 299 |
# Pattern 1f: Pacman-style transfer progress (flexible size/speed/time) |
| 300 |
pacman_match = re.search( |
| 301 |
r"\d+(?:\.\d+)?\s+[kKmMgGtT]iB\s+\d+(?:\.\d+)?\s+[kKmMgGtT]iB/s\s+\d+:\d+", text |
| 302 |
) |
| 303 |
# Pattern 1g: Key/value status lines from ffmpeg-like terminal tools. |
| 304 |
# These do not expose a percentage in the status line, so treat them as |
| 305 |
# activity rather than guessing progress from elapsed time. |
| 306 |
status_fields_match = self.has_progress_status_fields(text) |
| 307 |
|
| 308 |
if ( |
| 309 |
time_match |
| 310 |
or token_match |
| 311 |
or dd_match |
| 312 |
or curl_match |
| 313 |
or transfer_match |
| 314 |
or pacman_match |
| 315 |
or status_fields_match |
| 316 |
): |
| 317 |
# For non-percentage progress, use a single activity beep every 2 |
| 318 |
# seconds |
| 319 |
if ( |
| 320 |
current_time - self.env["commandBuffer"]["lastProgressTime"] |
| 321 |
>= 2.0 |
| 322 |
): |
| 323 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 324 |
"Playing activity beep for transfer progress", |
| 325 |
debug.DebugLevel.INFO, |
| 326 |
) |
| 327 |
self.play_activity_beep() |
| 328 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 329 |
return |
| 330 |
|
| 331 |
# Pattern 2: Fraction (15/100, 3 of 10, etc.) |
| 332 |
fraction_match = re.search(r"(\d+)\s*(?:of|/)\s*(\d+)", text) |
| 333 |
if fraction_match: |
| 334 |
current = int(fraction_match.group(1)) |
| 335 |
total = int(fraction_match.group(2)) |
| 336 |
# Filter out dates, page numbers, and other non-progress fractions |
| 337 |
if (total > 0 and total <= 1000 and current <= total and |
| 338 |
not re.search(r"\b(?:page|chapter|section|line|row|column|year|month|day)\b", text, re.IGNORECASE) and |
| 339 |
not re.search(r"\d{1,2}/\d{1,2}/\d{2,4}", text)): # Date pattern |
| 340 |
percentage = (current / total) * 100 |
| 341 |
if ( |
| 342 |
percentage |
| 343 |
!= self.env["commandBuffer"]["lastProgressValue"] |
| 344 |
): |
| 345 |
self.play_progress_tone(percentage) |
| 346 |
self.env["commandBuffer"]["lastProgressValue"] = percentage |
| 347 |
self.env["commandBuffer"][ |
| 348 |
"lastProgressTime" |
| 349 |
] = current_time |
| 350 |
return |
| 351 |
|
| 352 |
# Pattern 3: Progress bars ([#### ], [====> ], etc.) |
| 353 |
# Improved pattern to avoid matching IRC channels like [#channel] |
| 354 |
bar_match = re.search(r"\[([#=\*]+)([\s\.\-]*)\]", text) |
| 355 |
if bar_match: |
| 356 |
filled = len(bar_match.group(1)) |
| 357 |
unfilled = len(bar_match.group(2)) |
| 358 |
total = filled + unfilled |
| 359 |
# Require at least 2 progress chars total and unfilled portion must |
| 360 |
# be spaces/dots |
| 361 |
if total >= 2 and ( |
| 362 |
not bar_match.group(2) |
| 363 |
or re.match(r"^[\s\.]*$", bar_match.group(2)) |
| 364 |
): |
| 365 |
percentage = (filled / total) * 100 |
| 366 |
if ( |
| 367 |
percentage |
| 368 |
!= self.env["commandBuffer"]["lastProgressValue"] |
| 369 |
): |
| 370 |
self.play_progress_tone(percentage) |
| 371 |
self.env["commandBuffer"]["lastProgressValue"] = percentage |
| 372 |
self.env["commandBuffer"][ |
| 373 |
"lastProgressTime" |
| 374 |
] = current_time |
| 375 |
return |
| 376 |
|
| 377 |
# Pattern 4: Generic activity indicators (Loading..., Working..., etc.) |
| 378 |
activity_pattern = re.search( |
| 379 |
r"(loading|processing|working|installing|downloading|compiling|building).*\.{2,}", |
| 380 |
text, |
| 381 |
re.IGNORECASE, |
| 382 |
) |
| 383 |
if activity_pattern: |
| 384 |
# Play a steady beep every 2 seconds for ongoing activity |
| 385 |
if ( |
| 386 |
current_time - self.env["commandBuffer"]["lastProgressTime"] |
| 387 |
>= 2.0 |
| 388 |
): |
| 389 |
self.play_activity_beep() |
| 390 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 391 |
return |
| 392 |
|
| 393 |
# Pattern 5: Braille spinner indicators |
| 394 |
braille_match = re.search(r'[⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⡿⣟⣯⣷⣾⣽⣻⢿]', text) |
| 395 |
if braille_match: |
| 396 |
if current_time - self.env["commandBuffer"]["lastProgressTime"] >= 1.0: |
| 397 |
self.play_activity_beep() |
| 398 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 399 |
return |
| 400 |
|
| 401 |
# Pattern 6: Interruptible terminal activity indicators |
| 402 |
# Matches any: [symbol] [Task description][…] (... to interrupt ...) |
| 403 |
# Symbols include: * ✢ ✽ ✶ ✻ · • ◦ ○ ● ◆ and similar decorative characters |
| 404 |
# Keep this structural rather than adding application-specific formats, |
| 405 |
# which change too frequently to support reliably. |
| 406 |
interruptible_activity_match = re.search( |
| 407 |
r'[*✢✽✶✻·•◦○●◆]\s+\w+.*?(?:…\s*)?\(.*(?:esc|ctrl\+c) to interrupt.*\)', |
| 408 |
text, |
| 409 |
re.IGNORECASE, |
| 410 |
) |
| 411 |
if not interruptible_activity_match: |
| 412 |
interruptible_activity_match = re.search( |
| 413 |
( |
| 414 |
r'^\s*(?:thinking|working|processing|analyzing|searching|' |
| 415 |
r'reading|writing|planning|running|executing|updating|' |
| 416 |
r'building|installing|compiling|downloading|reviewing|' |
| 417 |
r'generating|responding|applying|fixing|editing|creating|' |
| 418 |
r'preparing|checking|opening|loading|fetching|retrieving|' |
| 419 |
r'scanning|indexing|summarizing)\b.*' |
| 420 |
r'\([^)]*(?:esc|ctrl\+c) to interrupt[^)]*\)\s*$' |
| 421 |
), |
| 422 |
text, |
| 423 |
re.IGNORECASE, |
| 424 |
) |
| 425 |
if interruptible_activity_match: |
| 426 |
if current_time - self.env["commandBuffer"]["lastProgressTime"] >= 1.0: |
| 427 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 428 |
"Playing interruptible activity beep", |
| 429 |
debug.DebugLevel.INFO, |
| 430 |
) |
| 431 |
self.play_activity_beep() |
| 432 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 433 |
return |
| 434 |
|
| 435 |
# Pattern 6b: Bullet/white bullet activity lines (•/◦ ...) |
| 436 |
bullet_activity_match = re.search( |
| 437 |
( |
| 438 |
r'^\s*[•◦]\s+.*(?:…|\.{3,}|\b(?:thinking|working|processing|' |
| 439 |
r'analyzing|searching|reading|writing|planning|running|' |
| 440 |
r'executing|updating|building|installing|compiling|downloading|' |
| 441 |
r'reviewing|generating|responding|applying|fixing|editing|' |
| 442 |
r'creating|preparing|checking|opening|loading|fetching|' |
| 443 |
r'retrieving|scanning|indexing|summarizing)\b)' |
| 444 |
), |
| 445 |
text, |
| 446 |
re.IGNORECASE, |
| 447 |
) |
| 448 |
if bullet_activity_match: |
| 449 |
if current_time - self.env["commandBuffer"]["lastProgressTime"] >= 1.0: |
| 450 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 451 |
"Playing bullet activity beep", |
| 452 |
debug.DebugLevel.INFO, |
| 453 |
) |
| 454 |
self.play_activity_beep() |
| 455 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 456 |
return |
| 457 |
|
| 458 |
# Pattern 7: Moon phase spinner indicators |
| 459 |
moon_match = re.search(r'[🌑🌒🌓🌔🌕🌖🌗🌘]', text) |
| 460 |
if moon_match: |
| 461 |
if current_time - self.env["commandBuffer"]["lastProgressTime"] >= 1.0: |
| 462 |
self.play_activity_beep() |
| 463 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 464 |
return |
| 465 |
|
| 466 |
# Pattern 8: Thinking/processing with timing (🔄 Thinking... 23s) |
| 467 |
thinking_match = re.search(r'🔄[^\w]*(?:thinking|processing|working|analyzing)[^\d]*(\d+)s?\b', text, re.IGNORECASE) |
| 468 |
if thinking_match: |
| 469 |
# Extract timing value for activity beep frequency adjustment |
| 470 |
seconds = int(thinking_match.group(1)) |
| 471 |
# Use slightly longer interval for thinking patterns to avoid spam |
| 472 |
thinking_interval = 1.5 if seconds < 10 else 2.0 |
| 473 |
if ( |
| 474 |
current_time - self.env["commandBuffer"]["lastProgressTime"] |
| 475 |
>= thinking_interval |
| 476 |
): |
| 477 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 478 |
f"Playing thinking activity beep (timing: {seconds}s)", |
| 479 |
debug.DebugLevel.INFO, |
| 480 |
) |
| 481 |
self.play_activity_beep() |
| 482 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 483 |
return |
| 484 |
|
| 485 |
# Pattern 9: Half-circle/circle progress indicators (◐ ◓ ◒ ◑) |
| 486 |
circle_match = re.search(r'[◐◓◒◑]', text) |
| 487 |
if circle_match: |
| 488 |
if current_time - self.env["commandBuffer"]["lastProgressTime"] >= 1.0: |
| 489 |
self.play_activity_beep() |
| 490 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 491 |
return |
| 492 |
|
| 493 |
# Pattern 10: Task status indicators (● Task Output, ○ Task Running, etc.) |
| 494 |
# Matches bullet points with task-related status text |
| 495 |
task_status_match = re.search( |
| 496 |
r'[●○◉]\s+(?:Task\s+)?(?:Output|Running|Pending|Working|Processing)\s+[a-zA-Z0-9]+', |
| 497 |
text, |
| 498 |
re.IGNORECASE, |
| 499 |
) |
| 500 |
if task_status_match: |
| 501 |
if current_time - self.env["commandBuffer"]["lastProgressTime"] >= 1.5: |
| 502 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 503 |
"Playing task status activity beep", |
| 504 |
debug.DebugLevel.INFO, |
| 505 |
) |
| 506 |
self.play_activity_beep() |
| 507 |
self.env["commandBuffer"]["lastProgressTime"] = current_time |
| 508 |
return |
| 509 |
|
| 510 |
def play_progress_tone(self, percentage): |
| 511 |
# Map 0-100% to 400-1200Hz frequency range |
| 512 |
frequency = 400 + (percentage * 8) |
| 513 |
frequency = max(400, min(1200, frequency)) # Clamp to safe range |
| 514 |
|
| 515 |
# Use Sox directly for clean quiet tones like: play -qn synth .1 tri |
| 516 |
# 400 gain -8 |
| 517 |
self.play_quiet_tone(frequency, 0.1) |
| 518 |
|
| 519 |
def play_activity_beep(self): |
| 520 |
# Single tone for generic activity |
| 521 |
self.play_quiet_tone(800, 0.08) |
| 522 |
|
| 523 |
def play_quiet_tone(self, frequency, duration): |
| 524 |
"""Play a quiet tone using Sox directly with flood protection""" |
| 525 |
import shlex |
| 526 |
import subprocess |
| 527 |
import time |
| 528 |
|
| 529 |
# Flood protection: prevent beeps closer than 0.1 seconds apart |
| 530 |
current_time = time.time() |
| 531 |
if not hasattr(self, '_last_beep_time'): |
| 532 |
self._last_beep_time = 0 |
| 533 |
|
| 534 |
if current_time - self._last_beep_time < 0.1: |
| 535 |
# Skip this beep to prevent audio crackling on low-resource systems |
| 536 |
return |
| 537 |
|
| 538 |
self._last_beep_time = current_time |
| 539 |
|
| 540 |
# Build the Sox command: play -qn synth <duration> tri <frequency> gain |
| 541 |
# -8 |
| 542 |
command = f"play -qn synth {duration} tri {frequency} gain -8" |
| 543 |
|
| 544 |
try: |
| 545 |
# Only play if sound is enabled |
| 546 |
if self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 547 |
"sound", "enabled" |
| 548 |
): |
| 549 |
subprocess.Popen( |
| 550 |
shlex.split(command), |
| 551 |
stdin=None, |
| 552 |
stdout=None, |
| 553 |
stderr=None, |
| 554 |
shell=False, |
| 555 |
) |
| 556 |
except Exception as e: |
| 557 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 558 |
"Sox tone error: " + str(e), debug.DebugLevel.ERROR |
| 559 |
) |
| 560 |
|
| 561 |
def is_current_line_prompt(self): |
| 562 |
"""Check if the current line looks like a standalone prompt (not command with progress)""" |
| 563 |
import re |
| 564 |
|
| 565 |
try: |
| 566 |
# Get the current screen content |
| 567 |
if not self.env["screen"]["new_content_text"]: |
| 568 |
return False |
| 569 |
|
| 570 |
lines = self.env["screen"]["new_content_text"].split("\n") |
| 571 |
if not lines: |
| 572 |
return False |
| 573 |
|
| 574 |
# Check the last line (most common) and current cursor line for |
| 575 |
# prompt patterns |
| 576 |
lines_to_check = [] |
| 577 |
|
| 578 |
# Add last line (most common for prompts) |
| 579 |
if lines: |
| 580 |
lines_to_check.append(lines[-1]) |
| 581 |
|
| 582 |
# Add current cursor line if different from last line |
| 583 |
if ( |
| 584 |
self.env["screen"]["new_cursor"]["y"] < len(lines) |
| 585 |
and self.env["screen"]["new_cursor"]["y"] != len(lines) - 1 |
| 586 |
): |
| 587 |
lines_to_check.append( |
| 588 |
lines[self.env["screen"]["new_cursor"]["y"]] |
| 589 |
) |
| 590 |
|
| 591 |
# Standalone prompt patterns (no commands mixed in) |
| 592 |
standalone_prompt_patterns = [ |
| 593 |
r"^\s*\$\s*$", # Just $ (with whitespace) |
| 594 |
r"^\s*#\s*$", # Just # (with whitespace) |
| 595 |
r"^\s*>\s*$", # Just > (with whitespace) |
| 596 |
r"^\[.*\]\s*[\\\$#>]\s*$", # [path]$ without commands |
| 597 |
r"^[a-zA-Z0-9._-]+[\\\$#>]\s*$", # bash-5.1$ without commands |
| 598 |
# Interactive prompt patterns (these ARE standalone) |
| 599 |
r".*\?\s*\[[YyNn]/[YyNn]\]\s*$", # ? [Y/n] or ? [y/N] style |
| 600 |
r".*\?\s*\[[Yy]es/[Nn]o\]\s*$", # ? [Yes/No] style |
| 601 |
# "continue? [Y/n]" style |
| 602 |
r".*continue\?\s*\[[YyNn]/[YyNn]\].*$", |
| 603 |
r"^::.*\?\s*\[[YyNn]/[YyNn]\].*$", # pacman style prompts |
| 604 |
# Authentication prompts (these ARE standalone) |
| 605 |
r"^\[[Ss]udo\]\s*[Pp]assword\s*for\s+.*:\s*$", # [sudo] password |
| 606 |
r"^[Pp]assword\s*:\s*$", # Password: |
| 607 |
r".*[Pp]assword\s*:\s*$", # general password prompts |
| 608 |
# Continuation prompts (these ARE standalone) |
| 609 |
r"^[Pp]ress\s+any\s+key\s+to\s+continue.*$", # Press any key |
| 610 |
r"^[Aa]re\s+you\s+sure\?\s*.*$", # Are you sure? |
| 611 |
] |
| 612 |
|
| 613 |
for line in lines_to_check: |
| 614 |
line = line.strip() |
| 615 |
if not line: |
| 616 |
continue |
| 617 |
|
| 618 |
# Check if this line contains both a prompt AND other content (like commands) |
| 619 |
# If so, don't treat it as a standalone prompt |
| 620 |
has_prompt_marker = bool( |
| 621 |
re.search(r".*@.*[\\\$#>]", line) |
| 622 |
or re.search(r"^\[.*\]\s*[\\\$#>]", line) |
| 623 |
) |
| 624 |
if has_prompt_marker: |
| 625 |
# If line has prompt marker but also has significant content after it, |
| 626 |
# it's a command line, not a standalone prompt |
| 627 |
prompt_end = max( |
| 628 |
line.rfind("$"), |
| 629 |
line.rfind("#"), |
| 630 |
line.rfind(">"), |
| 631 |
line.rfind("\\"), |
| 632 |
) |
| 633 |
if ( |
| 634 |
prompt_end >= 0 and prompt_end < len(line) - 5 |
| 635 |
): # More than just whitespace after prompt |
| 636 |
continue # This is a command line, not a standalone prompt |
| 637 |
|
| 638 |
for pattern in standalone_prompt_patterns: |
| 639 |
try: |
| 640 |
if re.search(pattern, line): |
| 641 |
return True |
| 642 |
except re.error: |
| 643 |
continue |
| 644 |
|
| 645 |
return False |
| 646 |
|
| 647 |
except Exception: |
| 648 |
# If anything fails, assume it's not a prompt to be safe |
| 649 |
return False |
| 650 |
|
| 651 |
def contains_url(self, text): |
| 652 |
"""Check if text contains URLs that might cause false progress detection""" |
| 653 |
import re |
| 654 |
|
| 655 |
# Specific URL patterns - only match actual URLs, not filenames |
| 656 |
url_patterns = [ |
| 657 |
r"\S+://\S+\.\S{2,}", # Any protocol:// with domain.ext |
| 658 |
r"www\.[^\s]+\.[a-zA-Z]{2,}", # www.domain.ext patterns |
| 659 |
] |
| 660 |
|
| 661 |
for pattern in url_patterns: |
| 662 |
if re.search(pattern, text, re.IGNORECASE): |
| 663 |
return True |
| 664 |
return False |
| 665 |
|
| 666 |
def has_progress_status_fields(self, text): |
| 667 |
"""Recognize dense key=value status lines from terminal tools.""" |
| 668 |
import re |
| 669 |
|
| 670 |
if "\n" in text or self.contains_url(text): |
| 671 |
return False |
| 672 |
|
| 673 |
fields = re.findall(r"\b([a-zA-Z][a-zA-Z0-9_-]*)=\s*\S+", text) |
| 674 |
if len(fields) < 3: |
| 675 |
return False |
| 676 |
|
| 677 |
field_names = {field.lower() for field in fields} |
| 678 |
progress_fields = { |
| 679 |
"frame", |
| 680 |
"fps", |
| 681 |
"size", |
| 682 |
"time", |
| 683 |
"bitrate", |
| 684 |
"speed", |
| 685 |
"elapsed", |
| 686 |
"dup", |
| 687 |
"drop", |
| 688 |
} |
| 689 |
if len(field_names & progress_fields) < 3: |
| 690 |
return False |
| 691 |
|
| 692 |
has_time_field = bool( |
| 693 |
re.search( |
| 694 |
r"\b(?:time|elapsed|eta)=\s*\d+(?::\d{2}){1,2}(?:\.\d+)?\b", |
| 695 |
text, |
| 696 |
re.IGNORECASE, |
| 697 |
) |
| 698 |
) |
| 699 |
has_rate_field = bool( |
| 700 |
re.search( |
| 701 |
r"\b(?:speed|fps|bitrate)=\s*[\d.]+[a-zA-Z/]*\b", |
| 702 |
text, |
| 703 |
re.IGNORECASE, |
| 704 |
) |
| 705 |
) |
| 706 |
|
| 707 |
return has_time_field and has_rate_field |
| 708 |
|
| 709 |
def set_callback(self, callback): |
| 710 |
pass |