#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader # By Chrys, Storm Dragon, and contributors. import ast import copy from fenrirscreenreader.core import debug from fenrirscreenreader.core.i18n import _ class HelpManager: ACTIVE_SECTION = "active" UNBOUND_SECTION = "unbound" PLUGINS_SECTION = "plugins" HELP_SECTIONS = ( ACTIVE_SECTION, UNBOUND_SECTION, PLUGINS_SECTION, ) def __init__(self): self.env = None self.help_lists = { self.ACTIVE_SECTION: [], self.UNBOUND_SECTION: [], self.PLUGINS_SECTION: [], } self.help_section = self.ACTIVE_SECTION self.tutorial_list_index = None self.bindings_backup = None self.raw_bindings_backup = None self.capture_degraded = False def initialize(self, environment): self.env = environment def shutdown(self): if self.is_tutorial_mode(): self.set_tutorial_mode(False) def toggle_tutorial_mode(self): self.set_tutorial_mode(not self.is_tutorial_mode()) def set_tutorial_mode(self, tutorial_mode): if tutorial_mode == self.is_tutorial_mode(): return True if tutorial_mode and self.env["runtime"]["VmenuManager"].get_active(): return False if tutorial_mode: self.bindings_backup = self.env["bindings"].copy() self.raw_bindings_backup = copy.deepcopy( self.env["rawBindings"] ) self.create_help_dict() self.env["general"]["tutorialMode"] = True self._install_help_bindings() self._refresh_input_bindings() self.capture_degraded = not self.env["runtime"][ "InputManager" ].set_help_capture(True) return True if not self.env["runtime"]["InputManager"].set_help_capture(False): self.env["runtime"]["DebugManager"].write_debug_out( "HelpManager could not release exclusive input capture", debug.DebugLevel.ERROR, ) return False self._restore_bindings() self.env["general"]["tutorialMode"] = False self.capture_degraded = False self.env["runtime"]["InputManager"].reset_input_state() self._refresh_input_bindings() return True def _install_help_bindings(self): help_bindings = { str([1, ["KEY_ESC"]]): "TOGGLE_TUTORIAL_MODE", str([1, ["KEY_UP"]]): "PREV_HELP", str([1, ["KEY_DOWN"]]): "NEXT_HELP", str([1, ["KEY_LEFT"]]): "PREV_HELP_SECTION", str([1, ["KEY_RIGHT"]]): "NEXT_HELP_SECTION", str([1, ["KEY_SPACE"]]): "CURR_HELP", } self.env["bindings"].update(help_bindings) for shortcut in help_bindings: self.env["rawBindings"][shortcut] = ast.literal_eval(shortcut) def _restore_bindings(self): if self.bindings_backup is not None: self.env["bindings"] = self.bindings_backup if self.raw_bindings_backup is not None: self.env["rawBindings"] = self.raw_bindings_backup self.bindings_backup = None self.raw_bindings_backup = None def _refresh_input_bindings(self): try: refresh_grabs = getattr( self.env["runtime"]["InputDriver"], "refresh_grabs", None ) if refresh_grabs: refresh_grabs(force=True) except Exception as error: self.env["runtime"]["DebugManager"].write_debug_out( "HelpManager could not refresh input bindings: " + str(error), debug.DebugLevel.ERROR, ) def is_tutorial_mode(self): if self.env is None: return False return self.env["general"]["tutorialMode"] def is_capture_degraded(self): return self.capture_degraded def _get_shortcuts_by_command(self): bindings = ( self.bindings_backup if self.bindings_backup is not None else self.env["bindings"] ) raw_bindings = ( self.raw_bindings_backup if self.raw_bindings_backup is not None else self.env["rawBindings"] ) shortcuts_by_command = {} for shortcut_key, command in bindings.items(): raw_shortcut = raw_bindings.get(shortcut_key) if raw_shortcut is None: try: raw_shortcut = ast.literal_eval(shortcut_key) except (SyntaxError, ValueError): continue shortcuts_by_command.setdefault(command, []).append( copy.deepcopy(raw_shortcut) ) return shortcuts_by_command def _format_shortcut(self, raw_shortcut): shortcut_repeat, raw_keys = raw_shortcut keys = list(raw_keys) formatted_keys = [] repeat_prefix = "" if shortcut_repeat in range(2, 9): repeat_prefix = _("{} times ").format(shortcut_repeat) for key_name in [ "KEY_FENRIR", "KEY_SCRIPT", "KEY_CTRL", "KEY_SHIFT", "KEY_ALT", "KEY_META", ]: if key_name in keys: formatted_keys.append(self._format_key_name(key_name)) keys.remove(key_name) formatted_keys.extend(self._format_key_name(key) for key in keys) return repeat_prefix + ", ".join(formatted_keys) def _format_key_name(self, key_name): formatted_key = key_name.lower() formatted_key = formatted_key.replace("key_kp", "keypad ") formatted_key = formatted_key.replace("key_", "") return _(formatted_key.strip()) def get_formatted_shortcut_for_command(self, command): shortcuts = self._get_shortcuts_by_command().get(command, []) return "; ".join( self._format_shortcut(shortcut) for shortcut in shortcuts ) def get_command_help_text(self, command, shortcuts=None): command_name = command.lower().split("__-__")[0].replace("_", " ") if command == "TOGGLE_TUTORIAL_MODE": command_description = _("toggles the tutorial mode") else: command_description = self.env["runtime"][ "CommandManager" ].get_command_description(command, section="commands") if command_description == "": command_description = _("no description available") if shortcuts is None: shortcuts = self._get_shortcuts_by_command().get(command, []) command_shortcuts = "; ".join( self._format_shortcut(shortcut) for shortcut in shortcuts ) if command_shortcuts == "": command_shortcuts = _("unbound") return _( "{command_name}, Shortcuts {command_shortcuts}, Description " "{command_description}" ).format( command_name=command_name, command_shortcuts=command_shortcuts, command_description=command_description, ) def create_help_dict(self, section="commands"): shortcuts_by_command = self._get_shortcuts_by_command() self.help_lists = { self.ACTIVE_SECTION: [], self.UNBOUND_SECTION: [], self.PLUGINS_SECTION: [], } for command in sorted(self.env["commands"][section]): command_instance = self.env["commands"][section][command] if not getattr(command_instance, "help_visible", True): continue shortcuts = shortcuts_by_command.get(command, []) help_text = self.get_command_help_text(command, shortcuts) if any("KEY_SCRIPT" in shortcut[1] for shortcut in shortcuts): target_section = self.PLUGINS_SECTION elif shortcuts: target_section = self.ACTIVE_SECTION else: target_section = self.UNBOUND_SECTION self.help_lists[target_section].append(help_text) self.help_section = self.ACTIVE_SECTION self.tutorial_list_index = None def get_help_section_name(self): if self.help_section == self.PLUGINS_SECTION: return _("Plugins") if self.help_section == self.UNBOUND_SECTION: return _("Unbound actions") return _("Active actions") def select_help_section(self, section): if section not in self.help_lists: return self.get_help_section_name() self.help_section = section self.tutorial_list_index = None return self.get_help_section_name() def next_help_section(self): section_index = self.HELP_SECTIONS.index(self.help_section) section_index = (section_index + 1) % len(self.HELP_SECTIONS) return self.select_help_section(self.HELP_SECTIONS[section_index]) def prev_help_section(self): section_index = self.HELP_SECTIONS.index(self.help_section) section_index = (section_index - 1) % len(self.HELP_SECTIONS) return self.select_help_section(self.HELP_SECTIONS[section_index]) def get_help_for_current_index(self): entries = self.help_lists[self.help_section] if self.tutorial_list_index is None or not entries: return self.get_help_section_name() return entries[self.tutorial_list_index] def next_index(self): entries = self.help_lists[self.help_section] if not entries: self.tutorial_list_index = None return if self.tutorial_list_index is None: self.tutorial_list_index = 0 return self.tutorial_list_index = (self.tutorial_list_index + 1) % len( entries ) def prev_index(self): entries = self.help_lists[self.help_section] if not entries: self.tutorial_list_index = None return if self.tutorial_list_index is None: self.tutorial_list_index = len(entries) - 1 return self.tutorial_list_index = (self.tutorial_list_index - 1) % len( entries )