nncli

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

commit f8f7770a8906ec847942aa87dcd30a60ee74b524
parent e86031cce3024d79fd38e225f5754e3da99cd258
Author: Eric Davis <edavis@insanum.com>
Date:   Sun,  6 Jul 2014 16:14:14 -0700

new keybind command for editing a note
update the note if it has changed after viewing with an external pager

Diffstat:
Mconfig.py | 2++
Msncli.py | 66+++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Mtemp.py | 1+
3 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/config.py b/config.py @@ -34,6 +34,7 @@ def __init__(self): 'kb_top' : 'g', 'kb_status' : 's', 'kb_create_note' : 'C', + 'kb_edit_note' : 'e', 'kb_view_note' : 'enter', 'kb_view_note_ext' : 'meta enter', 'kb_view_next_note' : 'J', @@ -133,6 +134,7 @@ def __init__(self): 'status' : [ cp.get(cfg_sec, 'kb_status'), [ 'common' ], 'Toggle status bar' ], 'view_log' : [ cp.get(cfg_sec, 'kb_view_log'), [ 'common' ], 'View log' ], 'create_note' : [ cp.get(cfg_sec, 'kb_create_note'), [ 'titles' ], 'Create a new note' ], + 'edit_note' : [ cp.get(cfg_sec, 'kb_edit_note'), [ 'titles' ], 'Edit note' ], 'view_note' : [ cp.get(cfg_sec, 'kb_view_note'), [ 'titles' ], 'View note' ], 'view_note_ext' : [ cp.get(cfg_sec, 'kb_view_note_ext'), [ 'titles' ], 'View note with pager' ], 'view_next_note' : [ cp.get(cfg_sec, 'kb_view_next_note'), [ 'notes' ], 'View next note' ], diff --git a/sncli.py b/sncli.py @@ -1,6 +1,6 @@ #!/usr/bin/env python2 -import os, sys, re, signal, time, datetime, logging +import os, sys, re, signal, time, datetime, md5, logging import subprocess, thread, threading import copy, json, urwid, datetime import view_titles, view_note, view_help, view_log, user_input @@ -95,6 +95,24 @@ def observer_notes_db_synced_note(self, ndb, evt_type, evt): # XXX # update view if note synced back is the visible one + def get_editor(self): + editor = self.config.get_config('editor') + if not editor and os.environ['EDITOR']: + editor = os.environ['EDITOR'] + if not editor: + self.status_message_set(u'No editor configured!') + return None + return editor + + def get_pager(self): + pager = self.config.get_config('pager') + if not pager and os.environ['PAGER']: + pager = os.environ['PAGER'] + if not pager: + self.status_message_set(u'No pager configured!') + return None + return pager + def header_clear(self): self.master_frame.contents['header'] = ( None, None ) self.sncli_loop.draw_screen() @@ -323,12 +341,8 @@ def frame_keypress(self, size, key): elif key == self.config.get_keybind('create_note'): if self.body_get().__class__ == view_titles.ViewTitles: - editor = self.config.get_config('editor') - if not editor and os.environ['EDITOR']: - editor = os.environ['EDITOR'] - if not editor: - self.status_message_set(u'No editor configured!') - return None + editor = self.get_editor() + if not editor: return None tf = temp.tempfile_create(None) try: @@ -342,6 +356,26 @@ def frame_keypress(self, size, key): self.ndb.create_note(content) temp.tempfile_delete(tf) + elif key == self.config.get_keybind('edit_note'): + if self.body_get().__class__ == view_titles.ViewTitles: + editor = self.get_editor() + if not editor: return None + + note = lb.note_list[lb.focus_position].note + md5_old = md5.new(note['content']).digest() + tf = temp.tempfile_create(note) + try: + subprocess.check_call(editor + u' ' + temp.tempfile_name(tf), shell=True) + except Exception, e: + self.status_message_set(u'Editor error: ' + str(e)) + + new_content = ''.join(temp.tempfile_content(tf)) + md5_new = md5.new(new_content).digest() + if md5_old != md5_new: + self.status_message_set(u'Note updated') + self.ndb.set_note_content(note['key'], new_content) + temp.tempfile_delete(tf) + elif key == self.config.get_keybind('view_note'): # only when viewing the note list if self.body_get().__class__ == view_titles.ViewTitles: @@ -352,20 +386,22 @@ def frame_keypress(self, size, key): elif key == self.config.get_keybind('view_note_ext'): # only when viewing the note list if self.body_get().__class__ == view_titles.ViewTitles: - pager = self.config.get_config('pager') - if not pager and os.environ['PAGER']: - pager = os.environ['PAGER'] - if not pager: - self.status_message_set(u'No pager configured!') - return None + pager = self.get_pager() + if not pager: return None - tf = temp.tempfile_create(lb.note_list[lb.focus_position].note) + note = lb.note_list[lb.focus_position].note + md5_old = md5.new(note['content']).digest() + tf = temp.tempfile_create(note) try: subprocess.check_call(pager + u' ' + temp.tempfile_name(tf), shell=True) except Exception, e: self.status_message_set(u'Pager error: ' + str(e)) - # XXX check if modified, if so update it + new_content = ''.join(temp.tempfile_content(tf)) + md5_new = md5.new(new_content).digest() + if md5_old != md5_new: + self.status_message_set(u'Note updated') + self.ndb.set_note_content(note['key'], new_content) temp.tempfile_delete(tf) elif key == self.config.get_keybind('view_next_note'): diff --git a/temp.py b/temp.py @@ -21,6 +21,7 @@ def tempfile_name(tf): return '' def tempfile_content(tf): + tf.seek(0) lines = [] for line in tf: lines.append(line)