Skip to content

Commit

Permalink
Fix filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiko Nickerl committed Jun 25, 2018
1 parent aba1953 commit c2dbe7a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## v0.14.0
## v0.14.1, June 25, 2018
##### Bugfixes
- Fix crash when escaping input with '\' while filtering
- Update display correctly in case filter does not match anything

## v0.14.0: June 25, 2018
##### Features
- Filtering in file preview is now possible
##### Bugfixes
Expand Down
2 changes: 1 addition & 1 deletion sodalite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from core.entry import Entry
from util import environment

VERSION = 'sodalite v0.14.0'
VERSION = 'sodalite v0.14.1'


logger = logging.getLogger(__name__)
Expand Down
7 changes: 4 additions & 3 deletions sodalite/ui/filepreview.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
class FilePreview(List):
def __init__(self, model):
super().__init__()
self.content = None
self.content = []
self.model: ViewModel = model
self.model.register(self)

def on_update(self):
if self.model.filtered_file_content and self.model.filtered_file_content != self.content:
if self.model.filtered_file_content != self.content:
self.content = self.model.filtered_file_content
with graphics.DRAW_LOCK:
self.body.clear()
self.body.extend([Text(line.numbered_content) for line in self.content])
self.focus_position = 0
if len(self.content) > 0:
self.focus_position = 0
4 changes: 2 additions & 2 deletions sodalite/ui/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def keypress(self, size, key):
super().keypress(size, key)

def update_filter(self, *args, **keywords):
self.model.filter_string = self.edit_text
self.model.filter_pattern = self.edit_text

def clear_filter(self):
self.model.filter_string = ''
self.model.filter_pattern = ''
self.parent.footer = None

def render(self, size, focus=False):
Expand Down
7 changes: 2 additions & 5 deletions sodalite/ui/highlighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class HighlightedLine:
def __init__(self, content, linenumber):
self.content = self.bold_headings(content)
self.raw_content = ''.join([word for attr, word in self.content])
self.linenumber = linenumber
formatted_linenumber = [(theme.line_number, u'{:>2} '.format(linenumber))]
self.numbered_content = [formatted_linenumber] + content
Expand All @@ -30,11 +31,7 @@ def bold_headings(self, content):
return new_content

def matches(self, pattern: Pattern):
if not pattern.pattern:
return True
for attr, word in self.content:
if pattern.search(word):
return True
return not pattern.pattern or pattern.search(self.raw_content)

def __str__(self):
return f"[{self.linenumber}: {self.content}]"
Expand Down
37 changes: 19 additions & 18 deletions sodalite/ui/viewmodel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import re
import sre_constants
from enum import Enum
from sre_parse import Pattern
from typing import List

from core.entry import Entry
Expand All @@ -24,8 +26,8 @@ def __init__(self, navigator: Navigator):
self.mode = Mode.NORMAL
self.current_entry: Entry = None
self.file_content: List[HighlightedLine] = None
self.filtered_file_content: List[HighlightedLine] = None
self._filter_string = ""
self.filtered_file_content: List[HighlightedLine] = []
self._filter_pattern: Pattern = re.compile('')
self.navigator = navigator
self._show_hidden_files = True
self.entries = []
Expand All @@ -38,7 +40,7 @@ def on_update(self):
else:
self.file_content = None

self._filter_string = ""
self._filter_pattern = re.compile('')
self.process()

def process(self):
Expand All @@ -47,22 +49,17 @@ def process(self):
self.notify_all()
else:
entries = list(self.current_entry.children)
entries = self.filter_regexp(entries)
entries = self.filter_entry(entries)
entries = self.filter_hidden_files(entries)
entries = sort(entries)
self.entries = entries
self.notify_all()

def filter_regexp(self, entries: List[Entry]) -> List[Entry]:
p = self.get_filter_pattern()
return [entry for entry in entries if p.search(entry.name)]
def filter_entry(self, entries: List[Entry]) -> List[Entry]:
return [entry for entry in entries if self.filter_pattern.search(entry.name)]

def filter_file_content(self):
pattern = self.get_filter_pattern()
return [line for line in self.file_content if line.matches(pattern)]

def get_filter_pattern(self):
return re.compile(self.filter_string, re.IGNORECASE)
return [line for line in self.file_content if line.matches(self.filter_pattern)]

def filter_hidden_files(self, entries: List[Entry]) -> List[Entry]:
if self.show_hidden_files:
Expand All @@ -81,13 +78,17 @@ def show_hidden_files(self, show: bool):
self.process()

@property
def filter_string(self) -> str:
return self._filter_string
def filter_pattern(self) -> Pattern:
return self._filter_pattern

@filter_string.setter
def filter_string(self, string: str):
self._filter_string = string
self.process()
@filter_pattern.setter
def filter_pattern(self, pattern: str):
try:
self._filter_pattern = re.compile(pattern, re.IGNORECASE)
self.process()
except sre_constants.error:
# .e.g gets thrown when string ends with '\' (user is about to escape a char)
pass


def sort(entries: List[Entry]):
Expand Down

0 comments on commit c2dbe7a

Please sign in to comment.