nncli

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

commit 9dcdbe1401b5533918eac1a59c21263ceb23d4a5
parent 35ac6ec26ecacc8eabbf61b038dde9e62bebc4ae
Author: Samuel Walladge <samuel@swalladge.id.au>
Date:   Fri, 10 Feb 2017 20:57:37 +1030

add initial sort notes by tags implementation

- ref #34
- don't like the the default keybinding - looking for suggestions

Diffstat:
Msimplenote_cli/config.py | 2++
Msimplenote_cli/notes_db.py | 5++++-
Msimplenote_cli/sncli.py | 6++++++
Msimplenote_cli/utils.py | 11++++++++---
4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/simplenote_cli/config.py b/simplenote_cli/config.py @@ -67,6 +67,7 @@ def __init__(self, custom_file=None): 'kb_clear_search' : 'A', 'kb_sort_date' : 'd', 'kb_sort_alpha' : 'a', + 'kb_sort_tags' : 'ctrl t', 'kb_note_trash' : 'T', 'kb_note_pin' : 'p', 'kb_note_markdown' : 'm', @@ -191,6 +192,7 @@ def __init__(self, custom_file=None): self.keybinds['clear_search'] = [ cp.get(cfg_sec, 'kb_clear_search'), [ 'titles' ], 'Show all notes' ] self.keybinds['sort_date'] = [ cp.get(cfg_sec, 'kb_sort_date'), [ 'titles' ], 'Sort notes by date' ] self.keybinds['sort_alpha'] = [ cp.get(cfg_sec, 'kb_sort_alpha'), [ 'titles' ], 'Sort notes by alpha' ] + self.keybinds['sort_tags'] = [ cp.get(cfg_sec, 'kb_sort_tags'), [ 'titles' ], 'Sort notes by tags' ] self.keybinds['note_trash'] = [ cp.get(cfg_sec, 'kb_note_trash'), [ 'titles', 'notes' ], 'Trash a note' ] self.keybinds['note_pin'] = [ cp.get(cfg_sec, 'kb_note_pin'), [ 'titles', 'notes' ], 'Pin note' ] self.keybinds['note_markdown'] = [ cp.get(cfg_sec, 'kb_note_markdown'), [ 'titles', 'notes' ], 'Flag note as markdown' ] diff --git a/simplenote_cli/notes_db.py b/simplenote_cli/notes_db.py @@ -80,11 +80,14 @@ def filtered_notes_sort(self, filtered_notes, sort_mode='date'): filtered_notes.sort(key=utils.sort_by_modify_date_pinned, reverse=True) else: filtered_notes.sort(key=lambda o: -float(o.note.get('modifydate', 0))) - else: + elif sort_mode == 'alpha': if self.config.get_config('pinned_ontop') == 'yes': filtered_notes.sort(key=utils.sort_by_title_pinned) else: filtered_notes.sort(key=lambda o: utils.get_note_title(o.note)) + elif sort_mode == 'tags': + pinned = self.config.get_config('pinned_ontop') + utils.sort_notes_by_tags(filtered_notes, pinned_ontop=pinned) def filter_notes(self, search_string=None, search_mode='gstyle'): """Return list of notes filtered with search string. diff --git a/simplenote_cli/sncli.py b/simplenote_cli/sncli.py @@ -860,6 +860,12 @@ def gui_frame_keypress(self, size, key): self.view_titles.sort_note_list('alpha') + elif key == self.config.get_keybind('sort_tags'): + if self.gui_body_get().__class__ != view_titles.ViewTitles: + return key + + self.view_titles.sort_note_list('tags') + elif key == self.config.get_keybind('copy_note_text'): if self.gui_body_get().__class__ != view_note.ViewNote: return key diff --git a/simplenote_cli/utils.py b/simplenote_cli/utils.py @@ -10,10 +10,10 @@ # first line with non-whitespace should be the title note_title_re = re.compile('\s*(.*)\n?') - + def generate_random_key(): """Generate random 30 digit (15 byte) hex string. - + stackoverflow question 2782229 """ return '%030x' % (random.randrange(256**15),) @@ -148,6 +148,11 @@ def sanitise_tags(tags): def sort_by_title_pinned(a): return (not note_pinned(a.note), get_note_title(a.note)) +def sort_notes_by_tags(notes, pinned_ontop=False): + notes.sort(key=lambda i: (pinned_ontop and not note_pinned(i.note), + len(i.note.get('tags')) == 0, + i.note.get('tags'))) + def sort_by_modify_date_pinned(a): if note_pinned(a.note): return 100.0 * float(a.note.get('modifydate', 0)) @@ -156,7 +161,7 @@ def sort_by_modify_date_pinned(a): class KeyValueObject: """Store key=value pairs in this object and retrieve with o.key. - + You should also be able to do MiscObject(**your_dict) for the same effect. """