From 91e5b9437e96b067860523c93b7c47998f184161 Mon Sep 17 00:00:00 2001 From: hut Date: Thu, 18 Feb 2010 17:39:06 +0100 Subject: working on the implementation of the new key parser --- ranger/api/keys.py | 1 + ranger/container/__init__.py | 3 +-- ranger/container/environment.py | 4 ++-- ranger/container/keymap.py | 10 ++++++---- ranger/defaults/keys.py | 28 ++++++++++++++++++++++++++++ ranger/gui/ui.py | 34 +++++++++++++++++----------------- ranger/gui/widgets/console.py | 5 ++--- ranger/gui/widgets/pager.py | 11 +++-------- ranger/gui/widgets/taskview.py | 4 +--- 9 files changed, 61 insertions(+), 39 deletions(-) diff --git a/ranger/api/keys.py b/ranger/api/keys.py index 4ac2e18e..a08c57b3 100644 --- a/ranger/api/keys.py +++ b/ranger/api/keys.py @@ -21,6 +21,7 @@ from inspect import getargspec, ismethod from ranger import RANGERDIR from ranger.gui.widgets import console_mode as cmode from ranger.container.bookmarks import ALLOWED_KEYS as ALLOWED_BOOKMARK_KEYS +from ranger.container.keymap import KeyMap, Direction def make_abbreviations(command_list): def bind(*args, **keywords): diff --git a/ranger/container/__init__.py b/ranger/container/__init__.py index b3fe9aff..4c8f08ba 100644 --- a/ranger/container/__init__.py +++ b/ranger/container/__init__.py @@ -17,6 +17,5 @@ used to manage stored data """ from ranger.container.history import History -from ranger.container.keybuffer import KeyBuffer -from .keymap import KeyMap +from .keymap import KeyMap, KeyBuffer from .bookmarks import Bookmarks diff --git a/ranger/container/environment.py b/ranger/container/environment.py index be1d96c5..b08b357f 100644 --- a/ranger/container/environment.py +++ b/ranger/container/environment.py @@ -42,7 +42,7 @@ class Environment(SettingsAware): self.path = abspath(expanduser(path)) self.pathway = () self.directories = {} - self.keybuffer = KeyBuffer() + self.keybuffer = KeyBuffer(None, None) self.copy = set() self.history = History(self.settings.max_history_size) @@ -56,7 +56,7 @@ class Environment(SettingsAware): if key == curses.KEY_RESIZE: self.keybuffer.clear() - self.keybuffer.append(key) + self.keybuffer.add(key) def key_clear(self): """Clear the keybuffer""" diff --git a/ranger/container/keymap.py b/ranger/container/keymap.py index c2aa344f..e1ff06da 100644 --- a/ranger/container/keymap.py +++ b/ranger/container/keymap.py @@ -67,9 +67,9 @@ class CommandArgs(object): self.binding = keybuffer.command @staticmethod - def from_widget(self, widget): - return CommandArgs(displayable.fm, \ - displayable, displayable.env.keybuffer) + def from_widget(widget): + return CommandArgs(widget.fm, \ + widget, widget.env.keybuffer) class KeyMap(Tree): """Contains a tree with all the keybindings""" @@ -126,9 +126,11 @@ class Binding(object): class KeyBuffer(object): """The evaluator and storage for pressed keys""" def __init__(self, keymap, direction_keys): + self.assign(keymap, direction_keys) + + def assign(self, keymap, direction_keys): self.keymap = keymap self.direction_keys = direction_keys - self.clear() def add(self, key): if self.failure: diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py index 260ba568..6c24d6d7 100644 --- a/ranger/defaults/keys.py +++ b/ranger/defaults/keys.py @@ -333,3 +333,31 @@ def _basic_movement(command_list): bind(KEY_UP, wdg.move(relative=-1)) bind(KEY_HOME, wdg.move(absolute=0)) bind(KEY_END, wdg.move(absolute=-1)) + +def get_directions(): + k = KeyMap() + map = k.add + + map('j', dir=Direction(down=1)) + map('k', dir=Direction(down=-1)) + map('h', dir=Direction(right=-1)) + map('l', dir=Direction(right=1)) + return k + +def move(arg): + arg.fm.move_pointer(relative=arg.direction.down) + +def get_ui_keys(): + k = KeyMap() + map = k.add + + map('', func=move) + map('', func=fm.exit()) + return k + +ui_keys = get_ui_keys() +taskview_keys = ui_keys +pager_keys = ui_keys +embedded_pager_keys = ui_keys +console_keys = ui_keys +directions = get_directions() diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py index 69acda4b..79552bf2 100644 --- a/ranger/gui/ui.py +++ b/ranger/gui/ui.py @@ -19,14 +19,14 @@ import curses import _curses from .displayable import DisplayableContainer +from ranger.container.keymap import CommandArgs from .mouse_event import MouseEvent -from ranger.container import CommandList class UI(DisplayableContainer): is_set_up = False mousemask = curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION load_mode = False - def __init__(self, commandlist=None, env=None, fm=None): + def __init__(self, keymap=None, env=None, fm=None): import os os.environ['ESCDELAY'] = '25' # don't know a cleaner way @@ -35,12 +35,13 @@ class UI(DisplayableContainer): if fm is not None: self.fm = fm - if commandlist is None: - self.commandlist = CommandList() - self.settings.keys.initialize_commands(self.commandlist) + if keymap is None: + self.keymap = self.settings.keys.ui_keys else: - self.commandlist = commandlist + self.keymap = keymap self.win = curses.initscr() + self.env.keybuffer.assign(self.keymap, self.settings.keys.directions) + self.env.keybuffer.clear() DisplayableContainer.__init__(self, None) @@ -132,15 +133,14 @@ class UI(DisplayableContainer): if DisplayableContainer.press(self, key): return - try: - tup = self.env.keybuffer.tuple_without_numbers() + kbuf = self.env.keybuffer - if tup: - cmd = self.commandlist[tup] - else: - return - except KeyError: - self.env.key_clear() + if kbuf.done: + cmd = kbuf.command + elif kbuf.failure: + kbuf.clear() + return + else: return self.env.cmd = cmd @@ -148,12 +148,12 @@ class UI(DisplayableContainer): if hasattr(cmd, 'show_obj') and hasattr(cmd.show_obj, 'hint'): if hasattr(self, 'hint'): self.hint(cmd.show_obj.hint) - elif hasattr(cmd, 'execute'): + elif cmd.function: try: - cmd.execute_wrap(self) + cmd.function(CommandArgs.from_widget(self)) except Exception as error: self.fm.notify(error) - self.env.key_clear() + kbuf.clear() def get_next_key(self): """Waits for key input and returns the pressed key""" diff --git a/ranger/gui/widgets/console.py b/ranger/gui/widgets/console.py index 87e5a7b5..7ed00a7e 100644 --- a/ranger/gui/widgets/console.py +++ b/ranger/gui/widgets/console.py @@ -54,10 +54,9 @@ class Console(Widget): allow_close = False def __init__(self, win): - from ranger.container import CommandList, History + from ranger.container import History Widget.__init__(self, win) - self.commandlist = CommandList() - self.settings.keys.initialize_console_commands(self.commandlist) + self.keymap = self.settings.keys.console_keys self.clear() self.histories = [None] * 4 self.histories[DEFAULT_HISTORY] = History() diff --git a/ranger/gui/widgets/pager.py b/ranger/gui/widgets/pager.py index b3e826e0..03a421cf 100644 --- a/ranger/gui/widgets/pager.py +++ b/ranger/gui/widgets/pager.py @@ -18,7 +18,6 @@ The pager displays text and allows you to scroll inside it. """ import re from . import Widget -from ranger.container.commandlist import CommandList from ranger.ext.move import move_between from ranger import log @@ -42,14 +41,10 @@ class Pager(Widget): self.markup = None self.lines = [] - self.commandlist = CommandList() - if embedded: - keyfnc = self.settings.keys.initialize_embedded_pager_commands + self.keymap = self.settings.keys.embedded_pager_keys else: - keyfnc = self.settings.keys.initialize_pager_commands - - keyfnc(self.commandlist) + self.keymap = self.settings.keys.pager_keys def open(self): self.scroll_begin = 0 @@ -166,7 +161,7 @@ class Pager(Widget): try: tup = self.env.keybuffer.tuple_without_numbers() if tup: - cmd = self.commandlist[tup] + cmd = self.keymap[tup] else: return diff --git a/ranger/gui/widgets/taskview.py b/ranger/gui/widgets/taskview.py index 6e86465c..f7937e11 100644 --- a/ranger/gui/widgets/taskview.py +++ b/ranger/gui/widgets/taskview.py @@ -22,7 +22,6 @@ from collections import deque from . import Widget from ranger.ext.accumulator import Accumulator -from ranger.container import CommandList class TaskView(Widget, Accumulator): old_lst = None @@ -31,8 +30,7 @@ class TaskView(Widget, Accumulator): Widget.__init__(self, win) Accumulator.__init__(self) self.scroll_begin = 0 - self.commandlist = CommandList() - self.settings.keys.initialize_taskview_commands(self.commandlist) + self.keymap = self.settings.keys.taskview_keys def draw(self): base_clr = deque() -- cgit 1.4.1-2-gfad0