nncli

NextCloud Notes Command Line Interface
git clone git://git.danielmoch.com/nncli.git
Log | Files | Refs | LICENSE

commit 3fc4140a0ea487e17c84dbb3b385ca96af46a207
parent b48e9048b940fc8a55ecb2af7ac4c6117f4595f3
Author: Eric Davis <edavis@insanum.com>
Date:   Thu, 26 Jun 2014 16:25:11 -0700

config changes and help view shows all config items

Diffstat:
Mconfig.py | 248++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mnotes_db.py | 21++++++++++-----------
Msncli.py | 212+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
3 files changed, 272 insertions(+), 209 deletions(-)

diff --git a/config.py b/config.py @@ -1,5 +1,5 @@ -import os, ConfigParser +import os, urwid, ConfigParser class Config: @@ -7,16 +7,16 @@ def __init__(self): self.home = os.path.abspath(os.path.expanduser('~')) defaults = \ { - 'sn_username' : '', - 'sn_password' : '', - 'db_path' : os.path.join(self.home, '.sncli'), - 'search_mode' : 'gstyle', - 'search_tags' : '1', - 'sort_mode' : '1', - 'pinned_ontop' : '1', - 'tabstop' : '4', - 'format_strftime' : '%Y/%m/%d', - 'format_note_title' : '[%D] %F %-N %T', + 'cfg_sn_username' : '', + 'cfg_sn_password' : '', + 'cfg_db_path' : os.path.join(self.home, '.sncli'), + 'cfg_search_mode' : 'gstyle', # gstyle/regex + 'cfg_search_tags' : 'yes', + 'cfg_sort_mode' : 'date', # alpha/date + 'cfg_pinned_ontop' : 'yes', + 'cfg_tabstop' : '4', + 'cfg_format_strftime' : '%Y/%m/%d', + 'cfg_format_note_title' : '[%D] %F %-N %T', 'kb_help' : 'h', 'kb_quit' : 'q', @@ -34,51 +34,40 @@ def __init__(self): 'kb_tabstop4' : '4', 'kb_tabstop8' : '8', - 'clr_default_fg' : 'default', - 'clr_default_bg' : 'default', - - 'clr_note_focus_fg' : 'white', - 'clr_note_focus_bg' : 'light red', - - 'clr_note_title_day_fg' : 'light red', - 'clr_note_title_day_bg' : 'default', - - 'clr_note_title_week_fg' : 'light green', - 'clr_note_title_week_bg' : 'default', - - 'clr_note_title_month_fg' : 'brown', - 'clr_note_title_month_bg' : 'default', - - 'clr_note_title_year_fg' : 'light blue', - 'clr_note_title_year_bg' : 'default', - + 'clr_default_fg' : 'default', + 'clr_default_bg' : 'default', + 'clr_note_focus_fg' : 'white', + 'clr_note_focus_bg' : 'light red', + 'clr_note_title_day_fg' : 'light red', + 'clr_note_title_day_bg' : 'default', + 'clr_note_title_week_fg' : 'light green', + 'clr_note_title_week_bg' : 'default', + 'clr_note_title_month_fg' : 'brown', + 'clr_note_title_month_bg' : 'default', + 'clr_note_title_year_fg' : 'light blue', + 'clr_note_title_year_bg' : 'default', 'clr_note_title_ancient_fg' : 'light blue', 'clr_note_title_ancient_bg' : 'default', - - 'clr_note_date_fg' : 'dark blue', - 'clr_note_date_bg' : 'default', - - 'clr_note_flags_fg' : 'dark magenta', - 'clr_note_flags_bg' : 'default', - - 'clr_note_tags_fg' : 'dark red', - 'clr_note_tags_bg' : 'default', - + 'clr_note_date_fg' : 'dark blue', + 'clr_note_date_bg' : 'default', + 'clr_note_flags_fg' : 'dark magenta', + 'clr_note_flags_bg' : 'default', + 'clr_note_tags_fg' : 'dark red', + 'clr_note_tags_bg' : 'default', 'clr_note_content_fg' : 'default', 'clr_note_content_bg' : 'default', 'clr_note_content_focus_fg' : 'white', 'clr_note_content_focus_bg' : 'light red', - - 'clr_help_focus_fg' : 'white', - 'clr_help_focus_bg' : 'dark gray', - 'clr_help_header_fg' : 'dark blue', - 'clr_help_header_bg' : 'default', - 'clr_help_key_fg' : 'default', - 'clr_help_key_bg' : 'default', - 'clr_help_config_fg' : 'dark green', - 'clr_help_config_bg' : 'default', - 'clr_help_descr_fg' : 'default', - 'clr_help_descr_bg' : 'default' + 'clr_help_focus_fg' : 'white', + 'clr_help_focus_bg' : 'dark gray', + 'clr_help_header_fg' : 'dark blue', + 'clr_help_header_bg' : 'default', + 'clr_help_config_fg' : 'dark green', + 'clr_help_config_bg' : 'default', + 'clr_help_value_fg' : 'dark red', + 'clr_help_value_bg' : 'default', + 'clr_help_descr_fg' : 'default', + 'clr_help_descr_bg' : 'default' } cp = ConfigParser.SafeConfigParser(defaults) @@ -92,79 +81,92 @@ def __init__(self): else: self.ok = True - self.sn_username = cp.get(cfg_sec, 'sn_username', raw=True) - self.sn_password = cp.get(cfg_sec, 'sn_password', raw=True) - self.db_path = cp.get(cfg_sec, 'db_path') - self.search_mode = cp.get(cfg_sec, 'search_mode') - self.search_tags = cp.getint(cfg_sec, 'search_tags') - self.sort_mode = cp.getint(cfg_sec, 'sort_mode') - self.pinned_ontop = cp.getint(cfg_sec, 'pinned_ontop') - self.tabstop = cp.getint(cfg_sec, 'tabstop') - self.format_strftime = cp.get(cfg_sec, 'format_strftime', raw=True) - self.format_note_title = cp.get(cfg_sec, 'format_note_title', raw=True) - - self.clr_default_fg = cp.get(cfg_sec, 'clr_default_fg') - self.clr_default_bg = cp.get(cfg_sec, 'clr_default_bg') - - self.clr_note_focus_fg = cp.get(cfg_sec, 'clr_note_focus_fg') - self.clr_note_focus_bg = cp.get(cfg_sec, 'clr_note_focus_bg') - - self.clr_note_title_day_fg = cp.get(cfg_sec, 'clr_note_title_day_fg') - self.clr_note_title_day_bg = cp.get(cfg_sec, 'clr_note_title_day_bg') - - self.clr_note_title_week_fg = cp.get(cfg_sec, 'clr_note_title_week_fg') - self.clr_note_title_week_bg = cp.get(cfg_sec, 'clr_note_title_week_bg') - - self.clr_note_title_month_fg = cp.get(cfg_sec, 'clr_note_title_month_fg') - self.clr_note_title_month_bg = cp.get(cfg_sec, 'clr_note_title_month_bg') - - self.clr_note_title_year_fg = cp.get(cfg_sec, 'clr_note_title_year_fg') - self.clr_note_title_year_bg = cp.get(cfg_sec, 'clr_note_title_year_bg') - - self.clr_note_title_ancient_fg = cp.get(cfg_sec, 'clr_note_title_ancient_fg') - self.clr_note_title_ancient_bg = cp.get(cfg_sec, 'clr_note_title_ancient_bg') - - self.clr_note_date_fg = cp.get(cfg_sec, 'clr_note_date_fg') - self.clr_note_date_bg = cp.get(cfg_sec, 'clr_note_date_bg') - - self.clr_note_flags_fg = cp.get(cfg_sec, 'clr_note_flags_fg') - self.clr_note_flags_bg = cp.get(cfg_sec, 'clr_note_flags_bg') - - self.clr_note_tags_fg = cp.get(cfg_sec, 'clr_note_tags_fg') - self.clr_note_tags_bg = cp.get(cfg_sec, 'clr_note_tags_bg') - - self.clr_note_content_fg = cp.get(cfg_sec, 'clr_note_content_fg') - self.clr_note_content_bg = cp.get(cfg_sec, 'clr_note_content_bg') - self.clr_note_content_focus_fg = cp.get(cfg_sec, 'clr_note_content_focus_fg') - self.clr_note_content_focus_bg = cp.get(cfg_sec, 'clr_note_content_focus_bg') - - self.clr_help_focus_fg = cp.get(cfg_sec, 'clr_help_focus_fg') - self.clr_help_focus_bg = cp.get(cfg_sec, 'clr_help_focus_bg') - self.clr_help_header_fg = cp.get(cfg_sec, 'clr_help_header_fg') - self.clr_help_header_bg = cp.get(cfg_sec, 'clr_help_header_bg') - self.clr_help_key_fg = cp.get(cfg_sec, 'clr_help_key_fg') - self.clr_help_key_bg = cp.get(cfg_sec, 'clr_help_key_bg') - self.clr_help_config_fg = cp.get(cfg_sec, 'clr_help_config_fg') - self.clr_help_config_bg = cp.get(cfg_sec, 'clr_help_config_bg') - self.clr_help_descr_fg = cp.get(cfg_sec, 'clr_help_descr_fg') - self.clr_help_descr_bg = cp.get(cfg_sec, 'clr_help_descr_bg') + self.configs = \ + { + 'sn_username' : [ cp.get(cfg_sec, 'cfg_sn_username', raw=True), 'Simplenote Username' ], + 'sn_password' : [ cp.get(cfg_sec, 'cfg_sn_password', raw=True), 'Simplenote Password' ], + 'db_path' : [ cp.get(cfg_sec, 'cfg_db_path'), 'Note storage path' ], + 'search_mode' : [ cp.get(cfg_sec, 'cfg_search_mode'), 'Search mode' ], + 'search_tags' : [ cp.get(cfg_sec, 'cfg_search_tags'), 'Search tags as well' ], + 'sort_mode' : [ cp.get(cfg_sec, 'cfg_sort_mode'), 'Sort mode' ], + 'pinned_ontop' : [ cp.get(cfg_sec, 'cfg_pinned_ontop'), 'Pinned at top of list' ], + 'tabstop' : [ cp.get(cfg_sec, 'cfg_tabstop'), 'Tabstop spaces' ], + 'format_strftime' : [ cp.get(cfg_sec, 'cfg_format_strftime', raw=True), 'Date strftime format' ], + 'format_note_title' : [ cp.get(cfg_sec, 'cfg_format_note_title', raw=True), 'Note title format' ] + } self.keybinds = \ - { - 'help' : [ cp.get(cfg_sec, 'kb_help'), 'Help' ], - 'quit' : [ cp.get(cfg_sec, 'kb_quit'), 'Quit' ], - 'down' : [ cp.get(cfg_sec, 'kb_down'), 'Scroll down one line' ], - 'up' : [ cp.get(cfg_sec, 'kb_up'), 'Scroll up one line' ], - 'page_down' : [ cp.get(cfg_sec, 'kb_page_down'), 'Page down' ], - 'page_up' : [ cp.get(cfg_sec, 'kb_page_up'), 'Page up' ], - 'half_page_down' : [ cp.get(cfg_sec, 'kb_half_page_down'), 'Half page down' ], - 'half_page_up' : [ cp.get(cfg_sec, 'kb_half_page_up'), 'Half page up' ], - 'bottom' : [ cp.get(cfg_sec, 'kb_bottom'), 'Goto bottom' ], - 'top' : [ cp.get(cfg_sec, 'kb_top'), 'Goto top' ], - 'view_note' : [ cp.get(cfg_sec, 'kb_view_note'), 'View note' ], - 'view_log' : [ cp.get(cfg_sec, 'kb_view_log'), 'View log' ], - 'tabstop2' : [ cp.get(cfg_sec, 'kb_tabstop2'), 'View with tabstop=2' ], - 'tabstop4' : [ cp.get(cfg_sec, 'kb_tabstop4'), 'View with tabstop=4' ], - 'tabstop8' : [ cp.get(cfg_sec, 'kb_tabstop8'), 'View with tabstop=8' ] - } + { + 'help' : [ cp.get(cfg_sec, 'kb_help'), 'Help' ], + 'quit' : [ cp.get(cfg_sec, 'kb_quit'), 'Quit' ], + 'down' : [ cp.get(cfg_sec, 'kb_down'), 'Scroll down one line' ], + 'up' : [ cp.get(cfg_sec, 'kb_up'), 'Scroll up one line' ], + 'page_down' : [ cp.get(cfg_sec, 'kb_page_down'), 'Page down' ], + 'page_up' : [ cp.get(cfg_sec, 'kb_page_up'), 'Page up' ], + 'half_page_down' : [ cp.get(cfg_sec, 'kb_half_page_down'), 'Half page down' ], + 'half_page_up' : [ cp.get(cfg_sec, 'kb_half_page_up'), 'Half page up' ], + 'bottom' : [ cp.get(cfg_sec, 'kb_bottom'), 'Goto bottom' ], + 'top' : [ cp.get(cfg_sec, 'kb_top'), 'Goto top' ], + 'view_note' : [ cp.get(cfg_sec, 'kb_view_note'), 'View note' ], + 'view_log' : [ cp.get(cfg_sec, 'kb_view_log'), 'View log' ], + 'tabstop2' : [ cp.get(cfg_sec, 'kb_tabstop2'), 'View with tabstop=2' ], + 'tabstop4' : [ cp.get(cfg_sec, 'kb_tabstop4'), 'View with tabstop=4' ], + 'tabstop8' : [ cp.get(cfg_sec, 'kb_tabstop8'), 'View with tabstop=8' ] + } + + self.colors = \ + { + 'default_fg' : [ cp.get(cfg_sec, 'clr_default_fg'), 'Default fg' ], + 'default_bg' : [ cp.get(cfg_sec, 'clr_default_bg'), 'Default bg' ], + 'note_focus_fg' : [ cp.get(cfg_sec, 'clr_note_focus_fg'), 'Note title focus fg' ], + 'note_focus_bg' : [ cp.get(cfg_sec, 'clr_note_focus_bg'), 'Note title focus bg' ], + 'note_title_day_fg' : [ cp.get(cfg_sec, 'clr_note_title_day_fg'), 'Day old note title fg' ], + 'note_title_day_bg' : [ cp.get(cfg_sec, 'clr_note_title_day_bg'), 'Day old note title bg' ], + 'note_title_week_fg' : [ cp.get(cfg_sec, 'clr_note_title_week_fg'), 'Week old note title fg' ], + 'note_title_week_bg' : [ cp.get(cfg_sec, 'clr_note_title_week_bg'), 'Week old note title bg' ], + 'note_title_month_fg' : [ cp.get(cfg_sec, 'clr_note_title_month_fg'), 'Month old note title fg' ], + 'note_title_month_bg' : [ cp.get(cfg_sec, 'clr_note_title_month_bg'), 'Month old note title bg' ], + 'note_title_year_fg' : [ cp.get(cfg_sec, 'clr_note_title_year_fg'), 'Year old note title fg' ], + 'note_title_year_bg' : [ cp.get(cfg_sec, 'clr_note_title_year_bg'), 'Year old note title bg' ], + 'note_title_ancient_fg' : [ cp.get(cfg_sec, 'clr_note_title_ancient_fg'), 'Ancient note title fg' ], + 'note_title_ancient_bg' : [ cp.get(cfg_sec, 'clr_note_title_ancient_bg'), 'Ancient note title bg' ], + 'note_date_fg' : [ cp.get(cfg_sec, 'clr_note_date_fg'), 'Note date fg' ], + 'note_date_bg' : [ cp.get(cfg_sec, 'clr_note_date_bg'), 'Note date bg' ], + 'note_flags_fg' : [ cp.get(cfg_sec, 'clr_note_flags_fg'), 'Note flags fg' ], + 'note_flags_bg' : [ cp.get(cfg_sec, 'clr_note_flags_bg'), 'Note flags bg' ], + 'note_tags_fg' : [ cp.get(cfg_sec, 'clr_note_tags_fg'), 'Note tags fg' ], + 'note_tags_bg' : [ cp.get(cfg_sec, 'clr_note_tags_bg'), 'Note tags bg' ], + 'note_content_fg' : [ cp.get(cfg_sec, 'clr_note_content_fg'), 'Note content fg' ], + 'note_content_bg' : [ cp.get(cfg_sec, 'clr_note_content_bg'), 'Note content bg' ], + 'note_content_focus_fg' : [ cp.get(cfg_sec, 'clr_note_content_focus_fg'), 'Note content focus fg' ], + 'note_content_focus_bg' : [ cp.get(cfg_sec, 'clr_note_content_focus_bg'), 'Note content focus bg' ], + 'help_focus_fg' : [ cp.get(cfg_sec, 'clr_help_focus_fg'), 'Help focus fg' ], + 'help_focus_bg' : [ cp.get(cfg_sec, 'clr_help_focus_bg'), 'Help focus bg' ], + 'help_header_fg' : [ cp.get(cfg_sec, 'clr_help_header_fg'), 'Help header fg' ], + 'help_header_bg' : [ cp.get(cfg_sec, 'clr_help_header_bg'), 'Help header bg' ], + 'help_config_fg' : [ cp.get(cfg_sec, 'clr_help_config_fg'), 'Help config fg' ], + 'help_config_bg' : [ cp.get(cfg_sec, 'clr_help_config_bg'), 'Help config bg' ], + 'help_value_fg' : [ cp.get(cfg_sec, 'clr_help_value_fg'), 'Help value fg' ], + 'help_value_bg' : [ cp.get(cfg_sec, 'clr_help_value_bg'), 'Help value bg' ], + 'help_descr_fg' : [ cp.get(cfg_sec, 'clr_help_descr_fg'), 'Help description fg' ], + 'help_descr_bg' : [ cp.get(cfg_sec, 'clr_help_descr_bg'), 'Help description bg' ] + } + + def get_config(self, name): + return self.configs[name][0] + + def get_config_descr(self, name): + return self.configs[name][1] + + def get_keybind(self, name): + return self.keybinds[name][0] + + def get_keybind_descr(self, name): + return self.keybinds[name][1] + + def get_color(self, name): + return self.colors[name][0] + + def get_color_descr(self, name): + return self.colors[name][1] diff --git a/notes_db.py b/notes_db.py @@ -41,10 +41,8 @@ def __init__(self, config): self.config = config # create db dir if it does not exist - if not os.path.exists(config.db_path): - os.mkdir(config.db_path) - - self.db_path = config.db_path + if not os.path.exists(self.config.get_config('db_path')): + os.mkdir(self.config.get_config('db_path')) now = time.time() # now read all .json files from disk @@ -83,7 +81,8 @@ def __init__(self, config): # initialise the simplenote instance we're going to use # this does not yet need network access - self.simplenote = Simplenote(config.sn_username, config.sn_password) + self.simplenote = Simplenote(config.get_config('sn_username'), + config.get_config('sn_password')) # we'll use this to store which notes are currently being synced by # the background thread, so we don't add them anew if they're still @@ -145,20 +144,20 @@ def filter_notes(self, search_string=None): total number of notes in memory. """ - if self.config.search_mode == 'regexp': + if self.config.get_config('search_mode') == 'regexp': filtered_notes, match_regexp, active_notes = self.filter_notes_regexp(search_string) else: filtered_notes, match_regexp, active_notes = self.filter_notes_gstyle(search_string) - if self.config.sort_mode == 0: - if self.config.pinned_ontop == 0: + if self.config.get_config('sort_mode') == 'alpha': + if self.config.get_config('pinned_ontop') == 'yes': # sort alphabetically on title filtered_notes.sort(key=lambda o: utils.get_note_title(o.note)) else: filtered_notes.sort(utils.sort_by_title_pinned) else: - if self.config.pinned_ontop == 0: + if self.config.get_config('pinned_ontop') == 'yes': # last modified on top filtered_notes.sort(key=lambda o: -float(o.note.get('modifydate', 0))) else: @@ -303,7 +302,7 @@ def filter_notes_regexp(self, search_string=None): active_notes += 1 c = n.get('content') - if self.config.search_tags == 1: + if self.config.search_tags == 'yes': t = n.get('tags') if sspat: # this used to use a filter(), but that would by definition @@ -364,7 +363,7 @@ def get_sync_queue_len(self): return self.q_sync.qsize() def helper_key_to_fname(self, k): - return os.path.join(self.db_path, k) + '.json' + return os.path.join(self.config.get_config('db_path'), k) + '.json' def helper_save_note(self, k, note): """Save a single note to disc. diff --git a/sncli.py b/sncli.py @@ -13,11 +13,12 @@ class sncli: def __init__(self): self.config = Config() - if not os.path.exists(self.config.db_path): - os.mkdir(self.config.db_path) + print self.config.get_config('db_path') + if not os.path.exists(self.config.get_config('db_path')): + os.mkdir(self.config.get_config('db_path')) # configure the logging module - self.logfile = os.path.join(self.config.db_path, 'sncli.log') + self.logfile = os.path.join(self.config.get_config('db_path'), 'sncli.log') self.loghandler = RotatingFileHandler(self.logfile, maxBytes=100000, backupCount=1) self.loghandler.setLevel(logging.DEBUG) self.loghandler.setFormatter(logging.Formatter(fmt='%(asctime)s [%(levelname)s] %(message)s')) @@ -119,7 +120,7 @@ def format_title(note): # format the note modification date t = time.localtime(float(note['modifydate'])) - mod_time = time.strftime(self.config.format_strftime, t) + mod_time = time.strftime(self.config.get_config('format_strftime'), t) # get the age of the note dt = datetime.datetime.fromtimestamp(time.mktime(t)) @@ -189,7 +190,7 @@ def recursive_format(title_format): return tmp # convert the format string into the actual note title line - title_line = recursive_format(self.config.format_note_title) + title_line = recursive_format(self.config.get_config('format_note_title')) return urwid.Columns(title_line) def list_get_note_titles(): @@ -235,20 +236,20 @@ def pop_last_view(): def handle_common_scroll_keybind(obj, size, key): - if key == self.config.keybinds['down'][0]: + if key == self.config.get_keybind('down'): last = len(obj.body.positions()) if obj.focus_position == (last - 1): return obj.focus_position += 1 obj.render(size) - elif key == self.config.keybinds['up'][0]: + elif key == self.config.get_keybind('up'): if obj.focus_position == 0: return obj.focus_position -= 1 obj.render(size) - elif key == self.config.keybinds['page_down'][0]: + elif key == self.config.get_keybind('page_down'): last = len(obj.body.positions()) next_focus = obj.focus_position + size[1] if next_focus >= last: @@ -257,7 +258,7 @@ def handle_common_scroll_keybind(obj, size, key): offset_inset=0, coming_from='above') - elif key == self.config.keybinds['page_up'][0]: + elif key == self.config.get_keybind('page_up'): if 'bottom' in obj.ends_visible(size): last = len(obj.body.positions()) next_focus = last - size[1] - size[1] @@ -269,7 +270,7 @@ def handle_common_scroll_keybind(obj, size, key): offset_inset=0, coming_from='below') - elif key == self.config.keybinds['half_page_down'][0]: + elif key == self.config.get_keybind('half_page_down'): last = len(obj.body.positions()) next_focus = obj.focus_position + (size[1] / 2) if next_focus >= last: @@ -278,7 +279,7 @@ def handle_common_scroll_keybind(obj, size, key): offset_inset=0, coming_from='above') - elif key == self.config.keybinds['half_page_up'][0]: + elif key == self.config.get_keybind('half_page_up'): if 'bottom' in obj.ends_visible(size): last = len(obj.body.positions()) next_focus = last - size[1] - (size[1] / 2) @@ -290,44 +291,45 @@ def handle_common_scroll_keybind(obj, size, key): offset_inset=0, coming_from='below') - elif key == self.config.keybinds['bottom'][0]: + elif key == self.config.get_keybind('bottom'): obj.change_focus(size, (len(obj.body.positions()) - 1), offset_inset=0, coming_from='above') - elif key == self.config.keybinds['top'][0]: + elif key == self.config.get_keybind('top'): obj.change_focus(size, 0, offset_inset=0, coming_from='below') class NoteTitles(urwid.ListBox): def __init__(self): - self.keybinds = get_config().keybinds + self.config = get_config() body = urwid.SimpleFocusListWalker(list_get_note_titles()) super(NoteTitles, self).__init__(body) def keypress(self, size, key): - if key == self.keybinds['quit'][0]: + if key == self.config.get_keybind('quit'): raise urwid.ExitMainLoop() - elif key == self.keybinds['help'][0]: + elif key == self.config.get_keybind('help'): push_last_view(self) sncli_loop.widget = Help() - elif key == self.keybinds['view_log'][0]: + elif key == self.config.get_keybind('view_log'): push_last_view(self) sncli_loop.widget = ViewLog() - elif key == self.keybinds['view_note'][0]: + elif key == self.config.get_keybind('view_note'): push_last_view(self) - sncli_loop.widget = NoteContent(self.focus_position, get_config().tabstop) + sncli_loop.widget = NoteContent(self.focus_position, + int(get_config().get_config('tabstop'))) else: handle_common_scroll_keybind(self, size, key) class NoteContent(urwid.ListBox): def __init__(self, nl_focus_index, tabstop): - self.keybinds = get_config().keybinds + self.config = get_config() self.nl_focus_index = nl_focus_index body = \ urwid.SimpleFocusListWalker( @@ -336,24 +338,24 @@ def __init__(self, nl_focus_index, tabstop): super(NoteContent, self).__init__(body) def keypress(self, size, key): - if key == self.keybinds['quit'][0]: + if key == self.config.get_keybind('quit'): sncli_loop.widget = pop_last_view() - elif key == self.keybinds['help'][0]: + elif key == self.config.get_keybind('help'): push_last_view(self) sncli_loop.widget = Help() - elif key == self.keybinds['view_log'][0]: + elif key == self.config.get_keybind('view_log'): push_last_view(self) sncli_loop.widget = ViewLog() - elif key == self.keybinds['tabstop2'][0]: + elif key == self.config.get_keybind('tabstop2'): sncli_loop.widget = NoteContent(self.nl_focus_index, 2) - elif key == self.keybinds['tabstop4'][0]: + elif key == self.config.get_keybind('tabstop4'): sncli_loop.widget = NoteContent(self.nl_focus_index, 4) - elif key == self.keybinds['tabstop8'][0]: + elif key == self.config.get_keybind('tabstop8'): sncli_loop.widget = NoteContent(self.nl_focus_index, 8) else: @@ -361,7 +363,7 @@ def keypress(self, size, key): class ViewLog(urwid.ListBox): def __init__(self): - self.keybinds = get_config().keybinds + self.config = get_config() f = open(get_logfile()) lines = [] for line in f: @@ -374,10 +376,10 @@ def __init__(self): super(ViewLog, self).__init__(body) def keypress(self, size, key): - if key == self.keybinds['quit'][0]: + if key == self.config.get_keybind('quit'): sncli_loop.widget = pop_last_view() - elif key == self.keybinds['help'][0]: + elif key == self.config.get_keybind('help'): push_last_view(self) sncli_loop.widget = Help() @@ -386,7 +388,7 @@ def keypress(self, size, key): class Help(urwid.ListBox): def __init__(self): - self.keybinds = get_config().keybinds + self.config = get_config() lines = [] @@ -402,51 +404,111 @@ def __init__(self): 'bottom', 'top', 'view_log' ] - lines.extend(self.create_help_lines(u"Common", keys)) + lines.extend(self.create_kb_help_lines(u"Keybinds Common", keys)) # NoteTitles keybinds keys = [ 'view_note' ] - lines.extend(self.create_help_lines(u"Note List", keys)) + lines.extend(self.create_kb_help_lines(u"Keybinds Note List", keys)) # NoteContent keybinds keys = [ 'tabstop2', 'tabstop4', 'tabstop8' ] - lines.extend(self.create_help_lines(u"Note Content", keys)) + lines.extend(self.create_kb_help_lines(u"Keybinds Note Content", keys)) + + lines.extend(self.create_config_help_lines()) + lines.extend(self.create_color_help_lines()) lines.append(urwid.Text(('help_header', u''))) body = urwid.SimpleFocusListWalker(lines) super(Help, self).__init__(body) - def create_help_lines(self, header, keys): + def create_kb_help_lines(self, header, keys): lines = [ urwid.AttrMap(urwid.Text(u''), 'help_header', 'help_focus') ] lines.append(urwid.AttrMap(urwid.Text(u' ' + header), 'help_header', 'help_focus')) - for k in keys: + for c in keys: lines.append( urwid.AttrMap( urwid.Text( [ - ('help_key', '{:>20} '.format(u"'" + self.keybinds[k][0] + u"'")), - ('help_config', '{:<20} '.format(u'kb_' + k)), - ('help_descr', self.keybinds[k][1]) + ('help_descr', '{:>24} '.format(self.config.get_keybind_descr(c))), + ('help_config', '{:>25} '.format(u'kb_' + c)), + ('help_value', u"'" + self.config.get_keybind(c) + u"'") ] ), attr_map = None, focus_map = { - 'help_key' : 'help_focus', + 'help_value' : 'help_focus', 'help_config' : 'help_focus', 'help_descr' : 'help_focus' } )) return lines + def create_config_help_lines(self): + lines = [ urwid.AttrMap(urwid.Text(u''), + 'help_header', + 'help_focus') ] + lines.append(urwid.AttrMap(urwid.Text(u' Configuration'), + 'help_header', + 'help_focus')) + for c in sorted(self.config.configs): + if c in [ 'sn_username', 'sn_password' ]: continue + lines.append( + urwid.AttrMap( + urwid.Text( + [ + ('help_descr', '{:>24} '.format(self.config.get_config_descr(c))), + ('help_config', '{:>25} '.format(u'cfg_' + c)), + ('help_value', u"'" + self.config.get_config(c) + u"'") + ] + ), + attr_map = None, + focus_map = { + 'help_value' : 'help_focus', + 'help_config' : 'help_focus', + 'help_descr' : 'help_focus' + } + )) + return lines + + def create_color_help_lines(self): + lines = [ urwid.AttrMap(urwid.Text(u''), + 'help_header', + 'help_focus') ] + lines.append(urwid.AttrMap(urwid.Text(u' Colors'), + 'help_header', + 'help_focus')) + fmap = {} + for c in sorted(self.config.colors): + fmap[re.search("^(.*)(_fg|_bg)$", c).group(1)] = 'help_focus' + for c in sorted(self.config.colors): + lines.append( + urwid.AttrMap( + urwid.Text( + [ + ('help_descr', '{:>24} '.format(self.config.get_color_descr(c))), + ('help_config', '{:>25} '.format(u'clr_' + c)), + (re.search("^(.*)(_fg|_bg)$", c).group(1), u"'" + self.config.get_color(c) + u"'") + ] + ), + attr_map = None, + focus_map = fmap + #focus_map = { + # 'help_value' : 'help_focus', + # 'help_config' : 'help_focus', + # 'help_descr' : 'help_focus' + # } + )) + return lines + def keypress(self, size, key): - if key == self.keybinds['quit'][0]: + if key == self.config.get_keybind('quit'): sncli_loop.widget = pop_last_view() else: @@ -455,56 +517,56 @@ def keypress(self, size, key): palette = \ [ ('default', - self.config.clr_default_fg, - self.config.clr_default_bg ), + self.config.get_color('default_fg'), + self.config.get_color('default_bg') ), ('note_focus', - self.config.clr_note_focus_fg, - self.config.clr_note_focus_bg ), + self.config.get_color('note_focus_fg'), + self.config.get_color('note_focus_bg') ), ('note_title_day', - self.config.clr_note_title_day_fg, - self.config.clr_note_title_day_bg ), + self.config.get_color('note_title_day_fg'), + self.config.get_color('note_title_day_bg') ), ('note_title_week', - self.config.clr_note_title_week_fg, - self.config.clr_note_title_week_bg ), + self.config.get_color('note_title_week_fg'), + self.config.get_color('note_title_week_bg') ), ('note_title_month', - self.config.clr_note_title_month_fg, - self.config.clr_note_title_month_bg ), + self.config.get_color('note_title_month_fg'), + self.config.get_color('note_title_month_bg') ), ('note_title_year', - self.config.clr_note_title_year_fg, - self.config.clr_note_title_year_bg ), + self.config.get_color('note_title_year_fg'), + self.config.get_color('note_title_year_bg') ), ('note_title_ancient', - self.config.clr_note_title_ancient_fg, - self.config.clr_note_title_ancient_bg ), + self.config.get_color('note_title_ancient_fg'), + self.config.get_color('note_title_ancient_bg') ), ('note_date', - self.config.clr_note_date_fg, - self.config.clr_note_date_bg ), + self.config.get_color('note_date_fg'), + self.config.get_color('note_date_bg') ), ('note_flags', - self.config.clr_note_flags_fg, - self.config.clr_note_flags_bg ), + self.config.get_color('note_flags_fg'), + self.config.get_color('note_flags_bg') ), ('note_tags', - self.config.clr_note_tags_fg, - self.config.clr_note_tags_bg ), + self.config.get_color('note_tags_fg'), + self.config.get_color('note_tags_bg') ), ('note_content', - self.config.clr_note_content_fg, - self.config.clr_note_content_bg ), + self.config.get_color('note_content_fg'), + self.config.get_color('note_content_bg') ), ('note_content_focus', - self.config.clr_note_content_focus_fg, - self.config.clr_note_content_focus_bg ), + self.config.get_color('note_content_focus_fg'), + self.config.get_color('note_content_focus_bg') ), ('help_focus', - self.config.clr_help_focus_fg, - self.config.clr_help_focus_bg ), + self.config.get_color('help_focus_fg'), + self.config.get_color('help_focus_bg') ), ('help_header', - self.config.clr_help_header_fg, - self.config.clr_help_header_bg ), - ('help_key', - self.config.clr_help_key_fg, - self.config.clr_help_key_bg ), + self.config.get_color('help_header_fg'), + self.config.get_color('help_header_bg') ), ('help_config', - self.config.clr_help_config_fg, - self.config.clr_help_config_bg ), + self.config.get_color('help_config_fg'), + self.config.get_color('help_config_bg') ), + ('help_value', + self.config.get_color('help_value_fg'), + self.config.get_color('help_value_bg') ), ('help_descr', - self.config.clr_help_descr_fg, - self.config.clr_help_descr_bg ) + self.config.get_color('help_descr_fg'), + self.config.get_color('help_descr_bg') ) ] sncli_loop = urwid.MainLoop(NoteTitles(),