commit 7666e841b0ff55ab051a72ba457517fe52db402e
parent bc54fa197aca91cd92577b7223b4db8cbf3bba7b
Author: Samuel Walladge <samuel@swalladge.id.au>
Date: Mon, 23 Jan 2017 14:21:59 +1030
implement regex flags for note view search
- also refactor regex search builder out into a function in utils.py
Diffstat:
3 files changed, 32 insertions(+), 19 deletions(-)
diff --git a/simplenote_cli/notes_db.py b/simplenote_cli/notes_db.py
@@ -239,23 +239,7 @@ def filter_notes_regex(self, search_string=None):
Return a list of notes filtered using the regex search_string.
Each element in the list is a tuple (local_key, note).
"""
- sspat = None
- valid_flags = {
- 'i': re.IGNORECASE
- }
- if search_string:
- try:
- search_string, flag_letters = re.match(r'^(.+?)(?:/([a-z]+))?$', search_string).groups()
- flags = 0
- # if flags are given, OR together all the valid flags
- # see https://docs.python.org/3/library/re.html#re.compile
- if flag_letters:
- for letter in flag_letters:
- if letter in valid_flags:
- flags = flags | valid_flags[letter]
- sspat = re.compile(search_string, flags)
- except re.error:
- sspat = None
+ sspat = utils.build_regex_search(search_string)
filtered_notes = []
active_notes = 0 # total number of notes, including deleted ones
diff --git a/simplenote_cli/utils.py b/simplenote_cli/utils.py
@@ -163,3 +163,31 @@ class KeyValueObject:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
+
+def build_regex_search(search_string):
+ """
+ Build up a compiled regular expression from the search string.
+
+ Supports the use of flags - ie. search for `nothing/i` will perform a
+ case-insensitive regex for `nothing`
+ """
+
+ sspat = None
+ valid_flags = {
+ 'i': re.IGNORECASE
+ }
+ if search_string:
+ try:
+ search_string, flag_letters = re.match(r'^(.+?)(?:/([a-z]+))?$', search_string).groups()
+ flags = 0
+ # if flags are given, OR together all the valid flags
+ # see https://docs.python.org/3/library/re.html#re.compile
+ if flag_letters:
+ for letter in flag_letters:
+ if letter in valid_flags:
+ flags = flags | valid_flags[letter]
+ sspat = re.compile(search_string, flags)
+ except re.error:
+ sspat = None
+
+ return sspat
diff --git a/simplenote_cli/view_note.py b/simplenote_cli/view_note.py
@@ -6,6 +6,7 @@
from . import utils
import re
from .clipboard import Clipboard
+import logging
class ViewNote(urwid.ListBox):
@@ -115,8 +116,8 @@ def is_match(self, term, full_text):
if self.search_mode == 'gstyle':
return term in full_text
else:
- results = re.search(term, full_text)
- return ( results is not None )
+ sspat = utils.build_regex_search(term)
+ return sspat and sspat.search(full_text)
def get_status_bar(self):
if not self.key: