diff options
author | hut <hut@lavabit.com> | 2010-04-06 22:26:38 +0200 |
---|---|---|
committer | hut <hut@lavabit.com> | 2010-04-06 22:26:38 +0200 |
commit | 1fecf8935330acd417370c32eea4f17f133d4776 (patch) | |
tree | 9f3f8d0b897b973549e59ff6943db21043e96342 | |
parent | 31130838e094741e8adf51785aad2c0097efd1b0 (diff) | |
download | ranger-1fecf8935330acd417370c32eea4f17f133d4776.tar.gz |
ext.direction: simplification + docs
-rw-r--r-- | ranger/ext/direction.py | 150 |
1 files changed, 51 insertions, 99 deletions
diff --git a/ranger/ext/direction.py b/ranger/ext/direction.py index 30eb87ce..1cdaa97b 100644 --- a/ranger/ext/direction.py +++ b/ranger/ext/direction.py @@ -13,109 +13,61 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -class NoDefault(object): - pass - -class Direction(object): - """An object with a down and right method""" - def __init__(self, right=None, down=None, absolute=False, - percent=False, pages=False, **keywords): - self.has_explicit_direction = False - - if 'up' in keywords: - self.down = -keywords['up'] - else: - self.down = down - - if 'left' in keywords: - self.right = -keywords['left'] - else: - self.right = right - - if 'relative' in keywords: - self.absolute = not relative - else: - self.absolute = absolute - - if 'default' in keywords: - self.default = keywords['default'] - else: - self.default = NoDefault - - self.original_down = self.down - self.original_right = self.right - - self.percent = percent - self.pages = pages - - @property +""" +Directions provide convenience methods for movement operations. + +Direction objects are handled just like dicts but provide +methods like up() and down() which give you the correct value +for the vertical direction, even if only the "up" or "down" key +has been defined. + +Example application: +d = Direction(down=5) +print(d.up()) # prints -5 +print(bool(d.horizontal())) # False, since no horizontal direction is defined +""" + +class Direction(dict): + __doc__ = __doc__ # for nicer pydoc + + def __init__(self, **keywords): + dict.__init__(self, keywords) + + def copy(self): + return Direction(**self) + + def _get_bool(self, first, second, fallback=None): + try: return self[first] + except: + try: return not self[second] + except: return fallback + + def _get_direction(self, first, second, fallback=0): + try: return self[first] + except: + try: return -self[second] + except: return fallback + def up(self): - if self.down is None: - return None - return -self.down + return -Direction.down(self) - @property - def left(self): - if self.right is None: - return None - return -self.right + def down(self): + return Direction._get_direction(self, 'down', 'up') - @property - def relative(self): - return not self.absolute + def right(self): + return Direction._get_direction(self, 'right', 'left') - def down_or_default(self, default): - if self.has_been_modified: - return self.down - return default + def absolute(self): + return Direction._get_bool(self, 'absolute', 'relative') - def steps_down(self, page_length=10): - if self.pages: - return self.down * page_length - else: - return self.down + def left(self): + return -Direction.right(self) - def steps_right(self, page_length=10): - if self.pages: - return self.right * page_length - else: - return self.right + def relative(self): + return not Direction.absolute(self) - def copy(self): - new = type(self)() - new.__dict__.update(self.__dict__) - return new - - def __mul__(self, other): - copy = self.copy() - if self.absolute: - if self.down is not None: - copy.down = other - if self.right is not None: - copy.right = other - else: - if self.down is not None: - copy.down *= other - if self.right is not None: - copy.right *= other - copy.original_down = self.original_down - copy.original_right = self.original_right - return copy - __rmul__ = __mul__ - - def __str__(self): - s = ['<Direction'] - if self.down is not None: - s.append(" down=" + str(self.down)) - if self.right is not None: - s.append(" right=" + str(self.right)) - if self.absolute: - s.append(" absolute") - else: - s.append(" relative") - if self.pages: - s.append(" pages") - if self.percent: - s.append(" percent") - s.append('>') - return ''.join(s) + def vertical(self): + return set(self) & set(['up', 'down']) + + def horizontal(self): + return set(self) & set(['left', 'right']) |