nncli

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

commit b48e9048b940fc8a55ecb2af7ac4c6117f4595f3
parent 7218de0300d0fb98806a6d786ea6bd5969a83cba
Author: Eric Davis <edavis@insanum.com>
Date:   Thu, 26 Jun 2014 13:57:50 -0700

added support for formatted note title lines with coloring
removed various focus color attributes

Diffstat:
Mconfig.py | 132++++++++++++++++++++++++++++++++-----------------------------------------------
Msncli.py | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 170 insertions(+), 107 deletions(-)

diff --git a/config.py b/config.py @@ -16,7 +16,7 @@ def __init__(self): 'pinned_ontop' : '1', 'tabstop' : '4', 'format_strftime' : '%Y/%m/%d', - 'format_note_title' : '[%D] %F %N%>%T', + 'format_note_title' : '[%D] %F %-N %T', 'kb_help' : 'h', 'kb_quit' : 'q', @@ -37,45 +37,32 @@ def __init__(self): 'clr_default_fg' : 'default', 'clr_default_bg' : 'default', - 'clr_note_title_day_fg' : 'dark blue', - 'clr_note_title_day_bg' : 'default', - 'clr_note_title_day_focus_fg' : 'white', - 'clr_note_title_day_focus_bg' : 'default', - - 'clr_note_title_week_fg' : 'dark blue', - 'clr_note_title_week_bg' : 'default', - 'clr_note_title_week_focus_fg' : 'white', - 'clr_note_title_week_focus_bg' : 'default', - - 'clr_note_title_month_fg' : 'dark blue', - 'clr_note_title_month_bg' : 'default', - 'clr_note_title_month_focus_fg' : 'white', - 'clr_note_title_month_focus_bg' : 'default', - - 'clr_note_title_year_fg' : 'dark blue', - 'clr_note_title_year_bg' : 'default', - 'clr_note_title_year_focus_fg' : 'white', - 'clr_note_title_year_focus_bg' : 'default', - - 'clr_note_title_ancient_fg' : 'dark blue', - 'clr_note_title_ancient_bg' : 'default', - 'clr_note_title_ancient_focus_fg' : 'white', - 'clr_note_title_ancient_focus_bg' : 'default', - - 'clr_note_date_fg' : 'dark blue', - 'clr_note_date_bg' : 'default', - 'clr_note_date_focus_fg' : 'white', - 'clr_note_date_focus_bg' : 'default', - - 'clr_note_flags_fg' : 'dark blue', - 'clr_note_flags_bg' : 'default', - 'clr_note_flags_focus_fg' : 'white', - 'clr_note_flags_focus_bg' : 'default', - - 'clr_note_tags_fg' : 'dark blue', - 'clr_note_tags_bg' : 'default', - 'clr_note_tags_focus_fg' : 'white', - 'clr_note_tags_focus_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_content_fg' : 'default', 'clr_note_content_bg' : 'default', @@ -119,45 +106,32 @@ def __init__(self): 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_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_day_focus_fg = cp.get(cfg_sec, 'clr_note_title_day_focus_fg') - self.clr_note_title_day_focus_bg = cp.get(cfg_sec, 'clr_note_title_day_focus_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_week_focus_fg = cp.get(cfg_sec, 'clr_note_title_week_focus_fg') - self.clr_note_title_week_focus_bg = cp.get(cfg_sec, 'clr_note_title_week_focus_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_month_focus_fg = cp.get(cfg_sec, 'clr_note_title_month_focus_fg') - self.clr_note_title_month_focus_bg = cp.get(cfg_sec, 'clr_note_title_month_focus_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_year_focus_fg = cp.get(cfg_sec, 'clr_note_title_year_focus_fg') - self.clr_note_title_year_focus_bg = cp.get(cfg_sec, 'clr_note_title_year_focus_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_title_ancient_focus_fg = cp.get(cfg_sec, 'clr_note_title_ancient_focus_fg') - self.clr_note_title_ancient_focus_bg = cp.get(cfg_sec, 'clr_note_title_ancient_focus_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_date_focus_fg = cp.get(cfg_sec, 'clr_note_date_focus_fg') - self.clr_note_date_focus_bg = cp.get(cfg_sec, 'clr_note_date_focus_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_flags_focus_fg = cp.get(cfg_sec, 'clr_note_flags_focus_fg') - self.clr_note_flags_focus_bg = cp.get(cfg_sec, 'clr_note_flags_focus_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_tags_focus_fg = cp.get(cfg_sec, 'clr_note_tags_focus_fg') - self.clr_note_tags_focus_bg = cp.get(cfg_sec, 'clr_note_tags_focus_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') diff --git a/sncli.py b/sncli.py @@ -89,14 +89,124 @@ def observer_notes_db_synced_note(self, ndb, evt_type, evt): def ba_bam_what(self): + def format_title(note): + """ + Various formatting tags are supporting for dynamically building + the title string. Each of these formatting tags supports a width + specifier (decimal) and a left justification (-) like that + supported by printf. + + %F -- flags ('*' for pinned, 'm' for markdown) + %T -- tags + %D -- date + %N -- note title + """ + + title = utils.get_note_title(note) + + # get the note flags + if note.has_key("systemtags"): + flags = '' + if ('pinned' in note['systemtags']): flags = flags + u'*' + else: flags = flags + u' ' + if ('markdown' in note['systemtags']): flags = flags + u'm' + else: flags = flags + u' ' + else: + flags = ' ' + + # get the note tags + tags = '%s' % ','.join(note['tags']) + + # format the note modification date + t = time.localtime(float(note['modifydate'])) + mod_time = time.strftime(self.config.format_strftime, t) + + # get the age of the note + dt = datetime.datetime.fromtimestamp(time.mktime(t)) + if dt > datetime.datetime.now() - datetime.timedelta(days=1): + note_age = 'd' # less than a day old + elif dt > datetime.datetime.now() - datetime.timedelta(weeks=1): + note_age = 'w' # less than a week old + elif dt > datetime.datetime.now() - datetime.timedelta(weeks=4): + note_age = 'm' # less than a month old + elif dt > datetime.datetime.now() - datetime.timedelta(weeks=52): + note_age = 'y' # less than a year old + else: + note_age = 'a' # ancient + + def recursive_format(title_format): + if not title_format: + return None + fmt = re.search("^(.*)%([-]*)([0-9]*)([FDTN])(.*)$", title_format) + if not fmt: + m = ('pack', urwid.AttrMap(urwid.Text(title_format), + 'default')) + l_fmt = None + r_fmt = None + else: + l = fmt.group(1) if fmt.group(1) else None + m = None + r = fmt.group(5) if fmt.group(5) else None + align = 'left' if fmt.group(2) == '-' else 'right' + width = int(fmt.group(3)) if fmt.group(3) else 'pack' + if fmt.group(4) == 'F': + m = (width, urwid.AttrMap(urwid.Text(flags, + align=align, + wrap='clip'), + 'note_flags')) + elif fmt.group(4) == 'D': + m = (width, urwid.AttrMap(urwid.Text(mod_time, + align=align, + wrap='clip'), + 'note_date')) + elif fmt.group(4) == 'T': + m = (width, urwid.AttrMap(urwid.Text(tags, + align=align, + wrap='clip'), + 'note_tags')) + elif fmt.group(4) == 'N': + if note_age == 'd': attr = 'note_title_day' + elif note_age == 'w': attr = 'note_title_week' + elif note_age == 'm': attr = 'note_title_month' + elif note_age == 'y': attr = 'note_title_year' + elif note_age == 'a': attr = 'note_title_ancient' + if width != 'pack': + m = (width, urwid.AttrMap(urwid.Text(title, + align=align, + wrap='clip'), + attr)) + else: + m = urwid.AttrMap(urwid.Text(title, + align=align, + wrap='clip'), + attr) + l_fmt = recursive_format(l) + r_fmt = recursive_format(r) + tmp = [] + if l_fmt: tmp.extend(l_fmt) + tmp.append(m) + if r_fmt: tmp.extend(r_fmt) + return tmp + + # convert the format string into the actual note title line + title_line = recursive_format(self.config.format_note_title) + return urwid.Columns(title_line) + def list_get_note_titles(): lines = [] for n in self.all_notes: - #lines.append( - # urwid.Text(('note_title_day', utils.get_note_title(n.note)))) lines.append( - urwid.AttrMap(urwid.Text(utils.get_note_title(n.note)), - 'note_title_day', 'note_title_day_focus')) + urwid.AttrMap(format_title(n.note), + 'default', + { 'default' : 'note_focus', + 'note_title_day' : 'note_focus', + 'note_title_week' : 'note_focus', + 'note_title_month' : 'note_focus', + 'note_title_year' : 'note_focus', + 'note_title_ancient' : 'note_focus', + 'note_date' : 'note_focus', + 'note_flags' : 'note_focus', + 'note_tags' : 'note_focus' })) return lines def list_get_note_content(index, tabstop): @@ -347,54 +457,33 @@ def keypress(self, size, key): ('default', self.config.clr_default_fg, self.config.clr_default_bg ), + ('note_focus', + self.config.clr_note_focus_fg, + self.config.clr_note_focus_bg ), ('note_title_day', self.config.clr_note_title_day_fg, self.config.clr_note_title_day_bg ), - ('note_title_day_focus', - self.config.clr_note_title_day_focus_fg, - self.config.clr_note_title_day_focus_bg ), ('note_title_week', self.config.clr_note_title_week_fg, self.config.clr_note_title_week_bg ), - ('note_title_week_focus', - self.config.clr_note_title_week_focus_fg, - self.config.clr_note_title_week_focus_bg ), ('note_title_month', self.config.clr_note_title_month_fg, self.config.clr_note_title_month_bg ), - ('note_title_month_focus', - self.config.clr_note_title_month_focus_fg, - self.config.clr_note_title_month_focus_bg ), ('note_title_year', self.config.clr_note_title_year_fg, self.config.clr_note_title_year_bg ), - ('note_title_year_focus', - self.config.clr_note_title_year_focus_fg, - self.config.clr_note_title_year_focus_bg ), ('note_title_ancient', self.config.clr_note_title_ancient_fg, self.config.clr_note_title_ancient_bg ), - ('note_title_ancient_focus', - self.config.clr_note_title_ancient_focus_fg, - self.config.clr_note_title_ancient_focus_bg ), ('note_date', self.config.clr_note_date_fg, self.config.clr_note_date_bg ), - ('note_date_focus', - self.config.clr_note_date_focus_fg, - self.config.clr_note_date_focus_bg ), ('note_flags', self.config.clr_note_flags_fg, self.config.clr_note_flags_bg ), - ('note_flags_focus', - self.config.clr_note_flags_focus_fg, - self.config.clr_note_flags_focus_bg ), ('note_tags', self.config.clr_note_tags_fg, self.config.clr_note_tags_bg ), - ('note_tags_focus', - self.config.clr_note_tags_focus_fg, - self.config.clr_note_tags_focus_bg ), ('note_content', self.config.clr_note_content_fg, self.config.clr_note_content_bg ),