about summary refs log tree commit diff stats
path: root/README
Commit message (Expand)AuthorAgeFilesLines
* small changes to dwm.1, rearranged order within main event loopAnselm R.Garbe2006-08-211-1/+1
* applied Sanders doc changes, added a PHONY line and changed the output of con...arg@10ksloc.org2006-08-031-1/+2
* implemented the idea presented by Sander for dwm targetarg@10ksloc.org2006-08-021-2/+1
* fixed a type in README, and patched config.mkarg@10ksloc.org2006-08-021-1/+1
* removed the CONFIG variable from config.mk, renamed config.h into config.defa...arg@10ksloc.org2006-08-021-5/+1
* simplified READMEarg@10ksloc.org2006-08-011-4/+1
* centralized/externalized configuration to config.harg@10ksloc.org2006-08-011-2/+6
* applied Sanders patchesarg@10ksloc.org2006-08-011-8/+9
* s/sleep 5/sleep 2/arg@10ksloc.org2006-07-211-1/+1
* changed the status info README hint (more simple now, no extra script necessary)arg@10ksloc.org2006-07-211-7/+1
* added a note how to achieve status info in the bararg@10ksloc.org2006-07-211-1/+17
* updated READMEAnselm R. Garbe2006-07-171-2/+2
* added dev.c instead of kb.cAnselm R. Garbe2006-07-131-1/+1
* small changes to READMEAnselm R. Garbe2006-07-131-3/+3
* added logo+descriptionAnselm R. Garbe2006-07-131-13/+12
* removed unnecessary crapAnselm R. Garbe2006-07-131-4/+2
* added mouse-based resizalsAnselm R. Garbe2006-07-111-9/+1
* updated READMEAnselm R. Garbe2006-07-111-5/+15
* fixed several stuff (gridwm gets better and better)Anselm R. Garbe2006-07-111-0/+1
* initial importAnselm R. Garbe2006-07-101-0/+40
ce */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
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, dictionary=None, **keywords):
		if dictionary is not None:
			dict.__init__(self, dictionary)
		else:
			dict.__init__(self, keywords)
		if 'to' in self:
			self['down'] = self['to']
			self['absolute'] = True

	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):
		return -Direction.down(self)

	def down(self):
		return Direction._get_direction(self, 'down', 'up')

	def right(self):
		return Direction._get_direction(self, 'right', 'left')

	def absolute(self):
		return Direction._get_bool(self, 'absolute', 'relative')

	def left(self):
		return -Direction.right(self)

	def relative(self):
		return not Direction.absolute(self)

	def vertical_direction(self):
		down = Direction.down(self)
		return (down > 0) - (down < 0)

	def horizontal_direction(self):
		right = Direction.right(self)
		return (right > 0) - (right < 0)

	def vertical(self):
		return set(self) & set(['up', 'down'])

	def horizontal(self):
		return set(self) & set(['left', 'right'])

	def pages(self):
		return 'pages' in self and self['pages']

	def percentage(self):
		return 'percentage' in self and self['percentage']

	def multiply(self, n):
		for key in ('up', 'right', 'down', 'left'):
			try:
				self[key] *= n
			except:
				pass

	def set(self, n):
		for key in ('up', 'right', 'down', 'left'):
			if key in self:
				self[key] = n

	def move(self, direction, override=None, minimum=0, maximum=9999,
			current=0, pagesize=1, offset=0):
		"""
		Calculates the new position in a given boundary.

		Example:
		d = Direction(pages=True)
		d.move(direction=3) # = 3
		d.move(direction=3, current=2) # = 5
		d.move(direction=3, pagesize=5) # = 15
		d.move(direction=3, pagesize=5, maximum=10) # = 10
		d.move(direction=9, override=2) # = 18
		"""
		pos = direction
		if override is not None:
			if self.absolute():
				pos = override
			else:
				pos *= override
		if self.pages():
			pos *= pagesize
		elif self.percentage():
			pos *= maximum / 100.0
		if self.absolute():
			if pos < minimum:
				pos += maximum
		else:
			pos += current
		return int(max(min(pos, maximum + offset - 1), minimum))

	def select(self, lst, override, current, pagesize, offset=1):
		dest = self.move(direction=self.down(), override=override,
			current=current, pagesize=pagesize, minimum=0, maximum=len(lst))
		selection = lst[min(current, dest):max(current, dest) + offset]
		return dest + offset - 1, selection