[hackers] [wmii] Add resize mode to python wmiirc. || Kris Maglione

From: <hg_AT_suckless.org>
Date: Fri, 22 May 2009 20:56:03 +0000 (UTC)

changeset: 2464:a4b6e2f30117
tag: tip
user: Kris Maglione <jg_AT_suckless.org>
date: Fri May 22 16:56:01 2009 -0400
files: alternative_wmiircs/python/pygmi/events.py alternative_wmiircs/python/pygmi/fs.py alternative_wmiircs/python/wmiirc.py
description:
Add resize mode to python wmiirc.

diff -r 3a88002303f4 -r a4b6e2f30117 alternative_wmiircs/python/pygmi/events.py
--- a/alternative_wmiircs/python/pygmi/events.py Fri May 22 15:32:32 2009 -0400
+++ b/alternative_wmiircs/python/pygmi/events.py Fri May 22 16:56:01 2009 -0400
@@ -6,8 +6,7 @@
 import pygmi
 from pygmi import monitor, client, curry, call, program_list, _
 
-__all__ = ('bind_keys', 'bind_events', 'toggle_keys', 'event_loop',
- 'event', 'Match')
+__all__ = ('keys', 'bind_events', 'event_loop', 'event', 'Match')
 
 keydefs = {}
 keys = {}
@@ -39,18 +38,19 @@
             return ary
 
 def flatten(items):
- for k, v in items.iteritems():
+ for k, v in items:
         if not isinstance(k, (list, tuple)):
             k = k,
         for key in k:
             yield key, v
 
-def bind_keys(items):
- for k, v in flatten(items):
+def bind_keys(mode='main', keys={}, import_={}):
+ for k, v in flatten(keys.iteritems()):
         keys[k % keydefs] = v
 
-def bind_events(items):
- for k, v in flatten(items):
+def bind_events(items={}, **kwargs):
+ kwargs.update(items)
+ for k, v in flatten(kwargs.iteritems()):
         if isinstance(k, Match):
             eventmatchers[k] = v
         else:
@@ -59,21 +59,42 @@
 def event(fn):
     bind_events({fn.__name__: fn})
 
-_AT_event
-def Key(args):
- if args in keys:
- keys[args](args)
+class Keys(object):
+ def __init__(self):
+ self.modes = {}
+ self.mode = 'main'
+ bind_events(Key=self.dispatch)
 
-keys_enabled = False
-keys_restore = None
-def toggle_keys(on=None, restore=None):
- if on is None:
- on = not keys_enabled
- keys_restore = restore
- if on:
- client.write('/keys', '\n'.join(keys.keys()))
- else:
- client.write('/keys', restore or ' ')
+ def _add_mode(self, mode):
+ if mode not in self.modes:
+ self.modes[mode] = { 'name': mode, 'keys': {}, 'import': {} }
+
+ def _set_mode(self, mode):
+ self._add_mode(mode)
+ self._mode = mode
+ client.write('/keys', '\n'.join(self.modes[mode]['keys'].keys() +
+ self.modes[mode]['import'].keys() +
+ ['']))
+ mode = property(lambda self: self._mode, _set_mode)
+
+ def bind(self, mode='main', keys={}, import_={}):
+ self._add_mode(mode)
+ mode = self.modes[mode]
+ for k, v in flatten(keys.iteritems()):
+ mode['keys'][k % keydefs] = v
+ for k, v in flatten((v, k) for k, v in import_.iteritems()):
+ mode['import'][k % keydefs] = v
+
+ def dispatch(self, key):
+ mode = self.modes[self.mode]
+ seen = set()
+ while mode and mode['name'] not in seen:
+ seen.add(mode['name'])
+ if key in mode['keys']:
+ return mode['keys'][key](key)
+ elif key in mode['import']:
+ mode = modes.get(mode['import'][key], None)
+keys = Keys()
 
 def dispatch(event, args=''):
     try:
@@ -88,7 +109,7 @@
 
 def event_loop():
     from pygmi import events
- toggle_keys(on=True)
+ keys.mode = 'main'
     for line in client.readlines('/event'):
         if not events.alive:
             break
diff -r 3a88002303f4 -r a4b6e2f30117 alternative_wmiircs/python/pygmi/fs.py
--- a/alternative_wmiircs/python/pygmi/fs.py Fri May 22 15:32:32 2009 -0400
+++ b/alternative_wmiircs/python/pygmi/fs.py Fri May 22 16:56:01 2009 -0400
@@ -15,8 +15,8 @@
     def __init__(self):
         pass
 
- def ctl(self, msg):
- client.awrite(self.ctl_path, msg)
+ def ctl(self, *args):
+ client.awrite(self.ctl_path, ' '.join(args))
 
     def __getitem__(self, key):
         for line in self.ctl_lines():
@@ -32,7 +32,7 @@
         assert '\n' not in key
         if key in self.ctl_types:
             val = self.ctl_types[key][1](val)
- self.ctl('%s %s\n' % (key, val))
+ self.ctl(key, val)
 
     def get(self, key, default=sentinel):
         try:
diff -r 3a88002303f4 -r a4b6e2f30117 alternative_wmiircs/python/wmiirc.py
--- a/alternative_wmiircs/python/wmiirc.py Fri May 22 15:32:32 2009 -0400
+++ b/alternative_wmiircs/python/wmiirc.py Fri May 22 16:56:01 2009 -0400
@@ -151,9 +151,7 @@
         self.timer.start()
 notice = Notice()
 
-bind_keys({
- '%(mod)s-Control-t': lambda k: events.toggle_keys(restore='%(mod)s-Control-t'),
-
+keys.bind('main', {
     '%(mod)s-%(left)s': lambda k: Tag('sel').select('left'),
     '%(mod)s-%(right)s': lambda k: Tag('sel').select('right'),
     '%(mod)s-%(up)s': lambda k: Tag('sel').select('up'),
@@ -192,11 +190,36 @@
     '%(mod)s-o': lambda k: tags.select(tags.PREV),
 })
 def bind_num(i):
- bind_keys({
+ keys.bind('main', {
         '%%(mod)s-%d' % i: lambda k: tags.select(str(i)),
         '%%(mod)s-Shift-%d' % i: lambda k: setattr(Client('sel'), 'tags', i),
     })
 map(bind_num, range(0, 10))
+
+keys.bind('main', {
+ '%(mod)s-Control-r': lambda k: setattr(keys, 'mode', 'resize'),
+ '%(mod)s-Control-t': lambda k: setattr(keys, 'mode', 'passthrough'),
+});
+keys.bind('passthrough', {
+ '%(mod)s-Control-t': lambda k: setattr(keys, 'mode', 'main'),
+});
+
+keys.bind('resize', {
+ 'Escape': lambda k: setattr(keys, 'mode', 'main'),
+}, import_={'main': ('%(mod)s-%(left)s', '%(mod)s-%(right)s',
+ '%(mod)s-%(up)s', '%(mod)s-%(down)s',
+ '%(mod)s-Space')})
+
+def addresize(mod, cmd, *args):
+ keys.bind('resize', {
+ mod + '%(left)s': lambda k: Tag('sel').ctl(cmd, 'sel sel', 'left', *args),
+ mod + '%(right)s': lambda k: Tag('sel').ctl(cmd, 'sel sel', 'right', *args),
+ mod + '%(up)s': lambda k: Tag('sel').ctl(cmd, 'sel sel', 'up', *args),
+ mod + '%(down)s': lambda k: Tag('sel').ctl(cmd, 'sel sel', 'down', *args),
+ });
+addresize('', 'grow')
+addresize('Control-', 'grow', '-1')
+addresize('Shift-', 'nudge')
 
 Actions.rehash()
 
Received on Fri May 22 2009 - 20:56:03 UTC

This archive was generated by hypermail 2.2.0 : Fri May 22 2009 - 21:00:06 UTC