Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
command_map.py140 linesDownload Raw Back to urwid
1# Urwid CommandMap class2#    Copyright (C) 2004-2011  Ian Ward3#4#    This library is free software; you can redistribute it and/or5#    modify it under the terms of the GNU Lesser General Public6#    License as published by the Free Software Foundation; either7#    version 2.1 of the License, or (at your option) any later version.8#9#    This library is distributed in the hope that it will be useful,10#    but WITHOUT ANY WARRANTY; without even the implied warranty of11#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU12#    Lesser General Public License for more details.13#14#    You should have received a copy of the GNU Lesser General Public15#    License along with this library; if not, write to the Free Software16#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA17#18# Urwid web site: https://urwid.org/19 20from __future__ import annotations21 22import enum23import typing24from collections.abc import MutableMapping25 26if typing.TYPE_CHECKING:27    from collections.abc import Iterator28 29    from typing_extensions import Self30 31 32class Command(str, enum.Enum):33    REDRAW_SCREEN = "redraw screen"34    UP = "cursor up"35    DOWN = "cursor down"36    LEFT = "cursor left"37    RIGHT = "cursor right"38    PAGE_UP = "cursor page up"39    PAGE_DOWN = "cursor page down"40    MAX_LEFT = "cursor max left"41    MAX_RIGHT = "cursor max right"42    ACTIVATE = "activate"43    MENU = "menu"44    SELECT_NEXT = "next selectable"45    SELECT_PREVIOUS = "prev selectable"46 47 48REDRAW_SCREEN = Command.REDRAW_SCREEN49CURSOR_UP = Command.UP50CURSOR_DOWN = Command.DOWN51CURSOR_LEFT = Command.LEFT52CURSOR_RIGHT = Command.RIGHT53CURSOR_PAGE_UP = Command.PAGE_UP54CURSOR_PAGE_DOWN = Command.PAGE_DOWN55CURSOR_MAX_LEFT = Command.MAX_LEFT56CURSOR_MAX_RIGHT = Command.MAX_RIGHT57ACTIVATE = Command.ACTIVATE58 59 60class CommandMap(MutableMapping[str, typing.Union[str, Command, None]]):61    """62    dict-like object for looking up commands from keystrokes63 64    Default values (key: command)::65 66        'tab':       'next selectable',67        'ctrl n':    'next selectable',68        'shift tab': 'prev selectable',69        'ctrl p':    'prev selectable',70        'ctrl l':    'redraw screen',71        'esc':       'menu',72        'up':        'cursor up',73        'down':      'cursor down',74        'left':      'cursor left',75        'right':     'cursor right',76        'page up':   'cursor page up',77        'page down': 'cursor page down',78        'home':      'cursor max left',79        'end':       'cursor max right',80        ' ':         'activate',81        'enter':     'activate',82    """83 84    def __iter__(self) -> Iterator[str]:85        return iter(self._command)86 87    def __len__(self) -> int:88        return len(self._command)89 90    _command_defaults: typing.ClassVar[dict[str, str | Command]] = {91        "tab": Command.SELECT_NEXT,92        "ctrl n": Command.SELECT_NEXT,93        "shift tab": Command.SELECT_PREVIOUS,94        "ctrl p": Command.SELECT_PREVIOUS,95        "ctrl l": Command.REDRAW_SCREEN,96        "esc": Command.MENU,97        "up": Command.UP,98        "down": Command.DOWN,99        "left": Command.LEFT,100        "right": Command.RIGHT,101        "page up": Command.PAGE_UP,102        "page down": Command.PAGE_DOWN,103        "home": Command.MAX_LEFT,104        "end": Command.MAX_RIGHT,105        " ": Command.ACTIVATE,106        "enter": Command.ACTIVATE,107    }108 109    def __init__(self) -> None:110        self._command = self._command_defaults.copy()111 112    def restore_defaults(self) -> None:113        self._command = self._command_defaults.copy()114 115    def __getitem__(self, key: str) -> str | Command | None:116        return self._command.get(key, None)117 118    def __setitem__(self, key, command: str | Command) -> None:119        self._command[key] = command120 121    def __delitem__(self, key: str) -> None:122        del self._command[key]123 124    def clear_command(self, command: str | Command) -> None:125        dk = [k for k, v in self._command.items() if v == command]126        for k in dk:127            del self._command[k]128 129    def copy(self) -> Self:130        """131        Return a new copy of this CommandMap, likely so we can modify132        it separate from a shared one.133        """134        c = self.__class__()135        c._command = dict(self._command)  # pylint: disable=protected-access136        return c137 138 139command_map = CommandMap()  # shared command mappings140 
codekingpro/portable-devtools · Team Ai