File: highlightable.py

package info (click to toggle)
python-panwid 0.3.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 408 kB
  • sloc: python: 4,904; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import logging
logger = logging.getLogger(__name__)

class HighlightableTextMixin(object):

    @property
    def highlight_state(self):
        if not getattr(self, "_highlight_state", False):
            self._highlight_state = False
            self._highlight_case_sensitive = False
            self._highlight_string = None
        return self._highlight_state

    @property
    def highlight_content(self):
        if self.highlight_state:
            return self.get_highlight_text()
        else:
            return self.highlight_source


    def highlight(self, start, end):
        self._highlight_state = True
        self._highlight_location = (start, end)
        self.on_highlight()

    def unhighlight(self):
        self._highlight_state = False
        self._highlight_location = None
        self.on_unhighlight()

    def get_highlight_text(self):

        if not self._highlight_location:
            return None

        return [
            (self.highlightable_attr_normal, self.highlight_source[:self._highlight_location[0]]),
            (self.highlightable_attr_highlight, self.highlight_source[self._highlight_location[0]:self._highlight_location[1]]),
            (self.highlightable_attr_normal, self.highlight_source[self._highlight_location[1]:]),
        ]

    @property
    def highlight_source(self):
        raise NotImplementedError

    @property
    def highlightable_attr_normal(self):
        raise NotImplementedError

    @property
    def highlightable_attr_highlight(self):
        raise NotImplementedError

    def on_highlight(self):
        pass

    def on_unhighlight(self):
        pass

__all__ = ["HighlightableTextMixin"]