Package libxyz :: Package core :: Module inputwrapper

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.core.inputwrapper

      1  #-*- coding: utf8 -*
      2  #
      3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008
      4  #
      5  # This file is part of XYZCommander.
      6  # XYZCommander is free software: you can redistribute it and/or modify
      7  # it under the terms of the GNU Lesser Public License as published by
      8  # the Free Software Foundation, either version 3 of the License, or
      9  # (at your option) any later version.
     10  # XYZCommander is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  # GNU Lesser Public License for more details.
     14  # You should have received a copy of the GNU Lesser Public License
     15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
     16
     17  from libxyz.ui.display import is_lowui_ge_0_9_9
     18
     19 -class InputWrapper(object):
     20      """
     21      Wrap get_input and seek in user-defined keycodes before return keys
     22      """
     23
     24      WIN_RESIZE = 'window resize'
     25
     26 -    def __init__(self, xyz):
     27          self.xyz = xyz
     28          self.plugin = xyz.pm.load(u":core:keycodes")
     29          self.keycodes = {}
     30          self._resized = False
     31
     32          self.update()
     33
     34      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     35
     36 -    def update(self, data=None):
     37          """
     38          Set/load keycodes data
     39          @param data: Keycodes data. If not provided load via get_keys()
     40          @type data: dict
     41          """
     42
     43          if data is not None:
     44              self.keycodes = data
     45          else:
     46              self.keycodes = self.plugin.get_keys()
     47
     48      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     49
     50 -    def get(self, allow_empty=False):
     51          """
     52          Get input from screen and search if it matches any user-defined
     53          keycodes
     54
     55          @param allow_empty: If set return empty list if nothing was typed
     56          """
     57
     58          _input = None
     59
     60          if allow_empty:
     61              if is_lowui_ge_0_9_9():
     62                  self.xyz.screen.set_input_timeouts(0)
     63
     64          while True:
     65              _in = self.xyz.screen.get_input()
     66
     67              if not _in:
     68                  if allow_empty:
     69                      _input = _in
     70                      break
     71                  else:
     72                      continue
     73
     74              if self.WIN_RESIZE in _in:
     75                  self._resized = True
     76
     77              try:
     78                  _input = [self.keycodes[tuple(_in)]]
     79              except KeyError:
     80                  _input = _in
     81
     82              break
     83
     84          if allow_empty:
     85              if is_lowui_ge_0_9_9():
     86                  self.xyz.screen.set_input_timeouts(None)
     87
     88          return _input
     89
     90      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     91
     92 -    def _resized_get(self):
     93          rval = self._resized
     94
     95          if rval:
     96              self._resized = False
     97
     98          return rval
     99
    100      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    101
    102 -    def _resized_set(self, value):
    103          if value:
    104              self._resized = True
    105          else:
    106              self._resized = False
    107
    108      resized = property(_resized_get, _resized_set)
    109