Skip to content

Commit

Permalink
Introducing a rudimental, configurable keymapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian authored and Julian committed Feb 13, 2013
1 parent 07d0b02 commit 05f0b11
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
68 changes: 35 additions & 33 deletions wmal/ui/curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class wMAL_urwid(object):
mainloop = None
cur_sort = 'title'
sorts_iter = cycle(('id', 'title', 'my_progress', 'total', 'my_score'))
keymapping = dict()

"""Widgets"""
header = None
Expand All @@ -68,6 +69,10 @@ def __init__(self):
('item_notaired', 'yellow', ''),
]

# create file
keymap = utils.parse_config(utils.get_root_filename('keymap.json'), utils.keymap_defaults)
self.keymapping = self.map_key_to_func(keymap)

sys.stdout.write("\x1b]0;wMAL-curses "+VERSION+"\x07");
self.header_title = urwid.Text('wMAL-curses ' + VERSION)
self.header_api = urwid.Text('API:')
Expand All @@ -79,8 +84,13 @@ def __init__(self):
('fixed', 17, self.header_sort),
('fixed', 16, self.header_api)]), 'status')


top_text = keymap['help'] + ':Help ' + keymap['sort'] +':Sort ' + \
keymap['update'] + ':Update ' + keymap['play'] + ':Play ' + \
keymap['status'] + ':Status ' + keymap['score'] + ':Score ' + \
keymap['quit'] + ':Quit'
self.top_pile = urwid.Pile([self.header,
urwid.AttrMap(urwid.Text('F1:Help F3:Sort F4:Update F5:Play F6:Status F7:Score F12:Quit'), 'status')
urwid.AttrMap(urwid.Text(top_text), 'status')
])

self.statusbar = urwid.AttrMap(urwid.Text('wMAL-curses '+VERSION), 'status')
Expand All @@ -102,6 +112,29 @@ def __init__(self):

self.mainloop.set_alarm_in(0, self.do_switch_account)
self.mainloop.run()

def map_key_to_func(self, keymap):
keymapping = dict()
funcmap = { 'help': self.do_help,
'prev_filter': self.do_prev_filter,
'next_filter': self.do_next_filter,
'sort': self.do_sort,
'update': self.do_update,
'play': self.do_play,
'status': self.do_status,
'score': self.do_score,
'send': self.do_send,
'retrieve': self.do_retrieve,
'addsearch': self.do_addsearch,
'reload': self.do_reload,
'switch_account': self.do_switch_account,
'delete': self.do_delete,
'quit': self.do_quit,
'search': self.do_search }

for key, value in keymap.items():
keymapping.update({value: funcmap[key]})
return keymapping

def _rebuild(self):
self.header_api.set_text('API:%s' % self.engine.api_info['name'])
Expand Down Expand Up @@ -155,38 +188,7 @@ def message_handler(self, classname, msgtype, msg):
self.mainloop.draw_screen()

def keystroke(self, input):
if input == 'f1':
self.do_help()
elif input == 'left':
self.do_prev_filter()
elif input == 'right':
self.do_next_filter()
elif input == 'f3':
self.do_sort()
elif input == 'f4':
self.do_update()
elif input == 'f5':
self.do_play()
elif input == 'f6':
self.do_status()
elif input == 'f7':
self.do_score()
elif input == 's':
self.do_send()
elif input == 'R':
self.do_retrieve()
elif input == 'a':
self.do_addsearch()
elif input == 'c':
self.do_reload()
elif input == 'f9':
self.do_switch_account()
elif input == 'd':
self.do_delete()
elif input == 'f12':
self.do_quit()
elif input == '/':
self.do_search('')
self.keymapping[input]()

def do_switch_account(self, loop=None, data=None):
manager = AccountManager()
Expand Down
18 changes: 18 additions & 0 deletions wmal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,21 @@ class APIFatal(wmalFatal):
userconfig_defaults = {
'mediatype': '',
}
keymap_defaults = {
'help': 'f1',
'prev_filter': 'left',
'next_filter': 'right',
'sort': 'f3',
'update': 'f4',
'play': 'f5',
'status': 'f6',
'score': 'f7',
'send': 's',
'retrieve': 'R',
'addsearch': 'a',
'reload': 'c',
'switch_account': 'f9',
'delete': 'd',
'quit': 'f12',
'search': '/',
}

0 comments on commit 05f0b11

Please sign in to comment.