menu.py (1584B)
1 from threading import Thread 2 from pygmi.util import call 3 4 __all__ = 'Menu', 'ClickMenu' 5 6 def inthread(name, args, action, **kwargs): 7 fn = lambda: call(*args, **kwargs) 8 if not action: 9 return fn() 10 t = Thread(target=lambda: action(fn())) 11 t.name += '-%s' % name 12 t.daemon = True 13 t.start() 14 15 class Menu(object): 16 def __init__(self, choices=(), action=None, 17 histfile=None, nhist=None): 18 self.choices = choices 19 self.action = action 20 self.histfile = histfile 21 self.nhist = nhist 22 23 def __call__(self, choices=None): 24 if choices is None: 25 choices = self.choices 26 if callable(choices): 27 choices = choices() 28 args = ['wimenu'] 29 if self.histfile: 30 args += ['-h', self.histfile] 31 if self.nhist: 32 args += ['-n', self.nhist] 33 return inthread('Menu', map(str, args), self.action, input='\n'.join(choices)) 34 call = __call__ 35 36 class ClickMenu(object): 37 def __init__(self, choices=(), action=None, 38 histfile=None, nhist=None): 39 self.choices = choices 40 self.action = action 41 self.prev = None 42 43 def __call__(self, choices=None): 44 if choices is None: 45 choices = self.choices 46 if callable(choices): 47 choices = choices() 48 args = ['wmii9menu'] 49 if self.prev: 50 args += ['-i', self.prev] 51 args += ['--'] + list(choices) 52 return inthread('ClickMenu', map(str, args), self.action) 53 call = __call__ 54 55 # vim:se sts=4 sw=4 et: