| 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.i18n import _ |
| 8 |
from fenrirscreenreader.utils import line_utils |
| 9 |
from fenrirscreenreader.utils import word_utils |
| 10 |
|
| 11 |
|
| 12 |
class command: |
| 13 |
def __init__(self): |
| 14 |
self.lastIdent = -1 |
| 15 |
|
| 16 |
def initialize(self, environment): |
| 17 |
self.env = environment |
| 18 |
|
| 19 |
def shutdown(self): |
| 20 |
pass |
| 21 |
|
| 22 |
def get_description(self): |
| 23 |
return "" |
| 24 |
|
| 25 |
def run(self): |
| 26 |
if not self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 27 |
"focus", "cursor" |
| 28 |
): |
| 29 |
return |
| 30 |
if self.env["runtime"]["ScreenManager"].is_screen_change(): |
| 31 |
self.lastIdent = 0 |
| 32 |
return |
| 33 |
|
| 34 |
# Skip if no cursor movement at all |
| 35 |
if (not self.env["runtime"]["CursorManager"].is_cursor_horizontal_move() and |
| 36 |
not self.env["runtime"]["CursorManager"].is_cursor_vertical_move()): |
| 37 |
return |
| 38 |
x, y, curr_line = line_utils.get_current_line( |
| 39 |
self.env["screen"]["new_cursor"]["x"], |
| 40 |
self.env["screen"]["new_cursor"]["y"], |
| 41 |
self.env["screen"]["new_content_text"], |
| 42 |
) |
| 43 |
curr_ident = self.env["screen"]["new_cursor"]["x"] |
| 44 |
|
| 45 |
if curr_line.isspace(): |
| 46 |
# Don't beep for lines with only spaces - no meaningful indentation |
| 47 |
return |
| 48 |
|
| 49 |
# Lines with actual content - calculate proper indentation |
| 50 |
lastIdent, lastY, last_line = line_utils.get_current_line( |
| 51 |
self.env["screen"]["new_cursor"]["x"], |
| 52 |
self.env["screen"]["new_cursor"]["y"], |
| 53 |
self.env["screen"]["old_content_text"], |
| 54 |
) |
| 55 |
if curr_line.strip() != last_line.strip(): |
| 56 |
return |
| 57 |
if len(curr_line.lstrip()) == len(last_line.lstrip()): |
| 58 |
return |
| 59 |
|
| 60 |
curr_ident = len(curr_line) - len(curr_line.lstrip()) |
| 61 |
|
| 62 |
if curr_ident <= 0: |
| 63 |
return |
| 64 |
|
| 65 |
# Initialize lastIdent if needed |
| 66 |
if self.lastIdent == -1: |
| 67 |
self.lastIdent = curr_ident |
| 68 |
|
| 69 |
# Only beep/announce if indentation level has changed |
| 70 |
if self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 71 |
"general", "auto_present_indent" |
| 72 |
) and self.lastIdent != curr_ident: |
| 73 |
if self.env["runtime"]["SettingsManager"].get_setting_as_int( |
| 74 |
"general", "auto_present_indent_mode" |
| 75 |
) in [0, 1]: |
| 76 |
self.env["runtime"]["OutputManager"].play_frequence( |
| 77 |
curr_ident * 50, 0.1, interrupt=False |
| 78 |
) |
| 79 |
if self.env["runtime"]["SettingsManager"].get_setting_as_int( |
| 80 |
"general", "autePresentIndentMode" |
| 81 |
) in [0, 2]: |
| 82 |
self.env["runtime"]["OutputManager"].present_text( |
| 83 |
_("indented ") + str(curr_ident) + " ", |
| 84 |
interrupt=False, |
| 85 |
flush=False, |
| 86 |
) |
| 87 |
|
| 88 |
# Always update lastIdent for next comparison |
| 89 |
self.lastIdent = curr_ident |
| 90 |
|
| 91 |
def set_callback(self, callback): |
| 92 |
pass |