nncli

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

commit 6190ff331c0500d7e255d99f97d5d0aefebdd519
parent 9037c8f5e68459fb4bdbc48bc6970ba9cbfa68e5
Author: Shawn Axsom <shawn.axsom@rooksecurity.com>
Date:   Sun,  8 Mar 2015 14:00:52 -0400

Searching notes is working fordwards and backwards for normal searches, need to add regex

Diffstat:
Msimplenote_cli/config.py | 12++++++++++--
Msimplenote_cli/sncli.py | 34++++++++++++++++++++++++++++++----
Msimplenote_cli/view_note.py | 42+++++++++++++++++++++++++++++++++---------
3 files changed, 73 insertions(+), 15 deletions(-)

diff --git a/simplenote_cli/config.py b/simplenote_cli/config.py @@ -61,6 +61,10 @@ def __init__(self): 'kb_select_version' : '#', 'kb_search_gstyle' : '/', 'kb_search_regex' : 'meta /', + 'kb_search_prev_gstyle' : '?', + 'kb_search_prev_regex' : 'meta ?', + 'kb_search_next' : 'n', + 'kb_search_prev' : 'N', 'kb_clear_search' : 'A', 'kb_sort_date' : 'd', 'kb_sort_alpha' : 'a', @@ -174,8 +178,12 @@ def __init__(self): 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' ] - self.keybinds['search_gstyle'] = [ cp.get(cfg_sec, 'kb_search_gstyle'), [ 'titles' ], 'Search using gstyle' ] - self.keybinds['search_regex'] = [ cp.get(cfg_sec, 'kb_search_regex'), [ 'titles' ], 'Search using regex' ] + self.keybinds['search_gstyle'] = [ cp.get(cfg_sec, 'kb_search_gstyle'), [ 'titles', 'notes' ], 'Search using gstyle' ] + self.keybinds['search_prev_gstyle'] = [ cp.get(cfg_sec, 'kb_search_prev_gstyle'), [ 'notes' ], 'Search backwards using gstyle' ] + self.keybinds['search_regex'] = [ cp.get(cfg_sec, 'kb_search_regex'), [ 'titles', 'notes' ], 'Search using regex' ] + self.keybinds['search_prev_regex'] = [ cp.get(cfg_sec, 'kb_search_regex'), [ 'titles', 'notes' ], 'Search using regex' ] + self.keybinds['search_next'] = [ cp.get(cfg_sec, 'kb_search_next'), [ 'notes' ], 'Go to next search result' ] + self.keybinds['search_prev'] = [ cp.get(cfg_sec, 'kb_search_prev'), [ 'notes' ], 'Go to previous search result' ] 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' ] diff --git a/simplenote_cli/sncli.py b/simplenote_cli/sncli.py @@ -320,7 +320,8 @@ def gui_search_input(self, args, search_string): self.master_frame.keypress = self.gui_frame_keypress if search_string: if (self.gui_body_get() == self.view_note): - self.view_note.update_note_view(search_string=search_string, search_mode=args[0]) + self.search_direction = args[1] + self.view_note.search_note_view_next(search_string=search_string, search_mode=args[0]) else: self.view_titles.update_note_list(search_string, args[0]) self.gui_body_set(self.view_titles) @@ -772,11 +773,20 @@ def gui_frame_keypress(self, size, key): self.master_frame.keypress = self.gui_footer_input_get().keypress elif key == self.config.get_keybind('search_gstyle') or \ - key == self.config.get_keybind('search_regex'): + key == self.config.get_keybind('search_regex') or \ + key == self.config.get_keybind('search_prev_gstyle') or \ + key == self.config.get_keybind('search_prev_regex'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ - self.gui_body_get().__class__ != view_note.ViewNote: + self.gui_body_get().__class__ != view_note.ViewNote: return key + if self.gui_body_get().__class__ == view_note.ViewNote: + if key == self.config.get_keybind('search_prev_gstyle') or \ + key == self.config.get_keybind('search_prev_regex'): + self.view_note.search_direction = 'backward' + else: + self.view_note.search_direction = 'forward' + self.gui_footer_input_set( urwid.AttrMap( user_input.UserInput( @@ -785,11 +795,27 @@ def gui_frame_keypress(self, size, key): '', self.gui_search_input, [ 'gstyle' if key == self.config.get_keybind('search_gstyle') - else 'regex' ]), + or key == self.config.get_keybind('search_prev_gstyle') + else 'regex', + 'backward' if key == self.config.get_keybind('search_prev_gstyle') + or key == self.config.get_keybind('search_prev_regex') + else 'forward' ]), 'user_input_bar')) self.gui_footer_focus_input() self.master_frame.keypress = self.gui_footer_input_get().keypress + elif key == self.config.get_keybind('search_next'): + if self.gui_body_get().__class__ != view_note.ViewNote: + return key + + self.view_note.search_note_view_next() + + elif key == self.config.get_keybind('search_prev'): + if self.gui_body_get().__class__ != view_note.ViewNote: + return key + + self.view_note.search_note_view_prev() + elif key == self.config.get_keybind('clear_search'): if self.gui_body_get().__class__ != view_titles.ViewTitles: return key diff --git a/simplenote_cli/view_note.py b/simplenote_cli/view_note.py @@ -13,6 +13,7 @@ def __init__(self, config, args): self.key = args['key'] self.log = args['log'] self.search_string = '' + self.search_direction = '' self.note = self.ndb.get_note(self.key) if self.key else None self.old_note = None self.tabstop = int(self.config.get_config('tabstop')) @@ -38,8 +39,7 @@ def get_note_content_as_list(self): lines.append(urwid.AttrMap(urwid.Divider(u'-'), 'default')) return lines - def update_note_view(self, key=None, version=None, search_string=None, search_mode='gstyle'): - self.search_string = search_string + def update_note_view(self, key=None, version=None): if key: # setting a new note self.key = key self.note = self.ndb.get_note(self.key) @@ -71,15 +71,39 @@ def update_note_view(self, key=None, version=None, search_string=None, search_mo self.body[:] = \ urwid.SimpleFocusListWalker(self.get_note_content_as_list()) - if self.search_string: - for line in range(self.focus_position, len(self.body.positions())): - line_content = self.note['content'].split('\n')[line] - if (search_string in line_content): - self.focus_position = line - break - else: + if not self.search_string: self.focus_position = 0 + def lines_after_current_position(self): + lines_after_current_position = range(self.focus_position + 1, len(self.body.positions()) - 1) + return lines_after_current_position + + def lines_before_current_position(self): + lines_before_current_position = range(0, self.focus_position) + lines_before_current_position.reverse() + return lines_before_current_position + + def search_note_view_next(self, search_string=None, search_mode='gstyle'): + if search_string: + self.search_string = search_string + note_range = self.lines_after_current_position() if self.search_direction == 'forward' else self.lines_before_current_position() + self.search_note_range(note_range, search_mode) + + def search_note_view_prev(self, search_string=None, search_mode='gstyle'): + if search_string: + self.search_string = search_string + note_range = self.lines_after_current_position() if self.search_direction == 'backward' else self.lines_before_current_position() + self.search_note_range(note_range, search_mode) + + def search_note_range(self, note_range, search_mode='gstyle'): + for line in note_range: + line_content = self.note['content'].split('\n')[line] + if (self.search_string in line_content): + self.focus_position = line + break + self.update_note_view() + + def get_status_bar(self): if not self.key: return \