nncli

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

commit 980fdc1ad8b6bf15073aebac247f681d3fb16c3b
parent bfea904e4e56a6f372c6de1fe23fda9456afbc1a
Author: Eric Davis <edavis@insanum.com>
Date:   Wed, 16 Jul 2014 10:37:32 -0700

new keybind command for viewing the diff of an older version of a note

Diffstat:
Mconfig.py | 8++++++--
Msncli.py | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtemp.py | 4+++-
3 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/config.py b/config.py @@ -19,6 +19,7 @@ def __init__(self): 'cfg_status_bar' : 'yes', 'cfg_editor' : 'vim', 'cfg_pager' : 'less -c', + 'cfg_diff' : 'diff -b -U10', 'cfg_max_logs' : '5', 'cfg_log_timeout' : '5', 'cfg_log_reversed' : 'yes', @@ -49,6 +50,7 @@ def __init__(self): 'kb_tabstop8' : '8', 'kb_prev_version' : '<', 'kb_next_version' : '>', + 'kb_diff_version' : 'D', 'kb_restore_version' : 'R', 'kb_latest_version' : 'L', 'kb_select_version' : '#', @@ -129,8 +131,9 @@ def __init__(self): self.configs['format_strftime'] = [ cp.get(cfg_sec, 'cfg_format_strftime', raw=True), 'Date strftime format' ] self.configs['format_note_title'] = [ cp.get(cfg_sec, 'cfg_format_note_title', raw=True), 'Note title format' ] self.configs['status_bar'] = [ cp.get(cfg_sec, 'cfg_status_bar'), 'Show the status bar' ] - self.configs['editor'] = [ cp.get(cfg_sec, 'cfg_editor'), 'Editor' ] - self.configs['pager'] = [ cp.get(cfg_sec, 'cfg_pager'), 'External pager' ] + self.configs['editor'] = [ cp.get(cfg_sec, 'cfg_editor'), 'Editor command' ] + self.configs['pager'] = [ cp.get(cfg_sec, 'cfg_pager'), 'External pager command' ] + self.configs['diff'] = [ cp.get(cfg_sec, 'cfg_diff'), 'External diff command' ] self.configs['max_logs'] = [ cp.get(cfg_sec, 'cfg_max_logs'), 'Max logs in footer' ] self.configs['log_timeout'] = [ cp.get(cfg_sec, 'cfg_log_timeout'), 'Log timeout' ] self.configs['log_reversed'] = [ cp.get(cfg_sec, 'cfg_log_reversed'), 'Log file reversed' ] @@ -162,6 +165,7 @@ def __init__(self): self.keybinds['tabstop8'] = [ cp.get(cfg_sec, 'kb_tabstop8'), [ 'notes' ], 'View with tabstop=8' ] self.keybinds['prev_version'] = [ cp.get(cfg_sec, 'kb_prev_version'), [ 'notes' ], 'View previous version' ] self.keybinds['next_version'] = [ cp.get(cfg_sec, 'kb_next_version'), [ 'notes' ], 'View next version' ] + self.keybinds['diff_version'] = [ cp.get(cfg_sec, 'kb_diff_version'), [ 'notes' ], 'Diff version of note' ] self.keybinds['restore_version'] = [ cp.get(cfg_sec, 'kb_restore_version'), [ 'notes' ], 'Restore version of note' ] self.keybinds['latest_version'] = [ cp.get(cfg_sec, 'kb_latest_version'), [ 'notes' ], 'View latest version' ] self.keybinds['select_version'] = [ cp.get(cfg_sec, 'kb_select_version'), [ 'notes' ], 'Select version' ] diff --git a/sncli.py b/sncli.py @@ -61,6 +61,13 @@ def get_pager(self): return None return pager + def get_diff(self): + diff = self.config.get_config('diff') + if not diff: + self.log(u'No diff command configured!') + return None + return diff + def exec_cmd_on_note(self, note, cmd=None, raw=False): if not cmd: @@ -86,6 +93,41 @@ def exec_cmd_on_note(self, note, cmd=None, raw=False): temp.tempfile_delete(tf) return content + def exec_diff_on_note(self, note, old_note): + + diff = self.get_diff() + if not diff: + return None + + pager = self.get_pager() + if not pager: + return None + + ltf = temp.tempfile_create(note) + otf = temp.tempfile_create(old_note) + out = temp.tempfile_create(None) + + try: + subprocess.call(diff + u' ' + + temp.tempfile_name(ltf) + u' ' + + temp.tempfile_name(otf) + u' > ' + + temp.tempfile_name(out), + shell=True) + subprocess.check_call(pager + u' ' + + temp.tempfile_name(out), + shell=True) + except Exception, e: + self.log(u'Command error: ' + str(e)) + temp.tempfile_delete(ltf) + temp.tempfile_delete(otf) + temp.tempfile_delete(out) + return None + + temp.tempfile_delete(ltf) + temp.tempfile_delete(otf) + temp.tempfile_delete(out) + return None + def gui_header_clear(self): self.master_frame.contents['header'] = ( None, None ) self.sncli_loop.draw_screen() @@ -459,6 +501,20 @@ def gui_frame_keypress(self, size, key): lb.update_note_view(version=version) + elif key == self.config.get_keybind('diff_version'): + if self.gui_body_get().__class__ != view_note.ViewNote: + return key + + if not self.view_note.old_note: + self.log(u'Already at latest version (key={0})'. + format(self.view_note.key)) + return None + + self.gui_clear() + self.exec_diff_on_note(self.view_note.note, + self.view_note.old_note) + self.gui_reset() + elif key == self.config.get_keybind('restore_version'): if self.gui_body_get().__class__ != view_note.ViewNote: return key diff --git a/temp.py b/temp.py @@ -9,7 +9,9 @@ def tempfile_create(note, raw=False): tf.flush() else: ext = '.txt' - if note and 'markdown' in note['systemtags']: + if note and \ + 'systemtags' in note and \ + 'markdown' in note['systemtags']: ext = '.mkd' tf = tempfile.NamedTemporaryFile(suffix=ext, delete=False) if note: