File: strings.py

package info (click to toggle)
python-asciimatics 1.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,488 kB
  • sloc: python: 15,713; sh: 8; makefile: 2
file content (175 lines) | stat: -rw-r--r-- 6,015 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
This module provides classes to handle embedded control strings for widgets.
"""
from asciimatics.parsers import Parser


class ColouredText():
    """
    Unicode string-like object to store text and colour maps, using a parser to convert the raw text
    passed in into visible text and an associated colour map.  This only handles simple colour
    change commands and will ignore more complex commands).
    """

    def __init__(self, raw_text, parser, colour=None, colour_map=None, offsets=None, text=None):
        """
        :param raw_text: The raw unicode string to be processed
        :param parser: The parser to process the text
        :param colour: Optional starting colour tuple to use for this text.
        :param colour_map: Optional ready parsed colour map for this text.
        :param offsets: Optional ready parsed offsets for this text.
        :param text: Optional ready parsed text for this text.

        The colour_map, offsets and text options are to optimize creation of substrings from an
        existing ColouredText object and should not be used in general.
        """
        super().__init__()
        self._raw_text = raw_text
        self._raw_offsets = []
        self._parser = parser
        self._colour_map = None
        self._last_colour = colour if colour else (None, None, None)
        self._init_colour = colour
        self._colour_map = []
        self._text = ""
        if colour_map:
            self._colour_map = colour_map
            self._last_colour = colour_map[-1]
            self._raw_offsets = offsets
            self._text = text
        else:
            self._parser.reset(self._raw_text, self._init_colour)
            for offset, command, params in self._parser.parse():
                if command == Parser.DISPLAY_TEXT:
                    self._colour_map.append(self._last_colour)
                    self._raw_offsets.append(offset)
                    self._text += params
                elif command == Parser.CHANGE_COLOURS:
                    self._last_colour = params

    def __repr__(self):
        """
        Return the processed (displayable) text.
        """
        return self._text

    def __len__(self):
        """
        Returns the length of the processed (displayable) text.
        """
        return len(self._text)

    def __getitem__(self, item):
        """
        Slice magic method.
        """
        if isinstance(item, int):
            start = self._raw_offsets[item]
            stop = None if item == len(self._raw_offsets) - 1 else self._raw_offsets[item + 1]
            step = 1
            colour_index = max(0, item - 1)
            base = self._raw_offsets[item]
            offsets = [0]
        else:
            try:
                start = None if item.start is None else self._raw_offsets[slice(item.start, None, None)][0]
            except IndexError:
                start = len(self._raw_text)
            try:
                stop = None if item.stop is None else self._raw_offsets[slice(item.stop, None, None)][0]
            except IndexError:
                stop = None
            step = item.step
            colour_index = max(0, item.start - 1 if item.start else 0)
            try:
                base = self._raw_offsets[item][0]
                offsets = [x - base for x in self._raw_offsets[item]]
            except IndexError:
                base = 0
                offsets = []
        try:
            colour = self._colour_map[colour_index]
        except IndexError:
            colour = self._init_colour
        return ColouredText(self._raw_text[slice(start, stop, step)],
                            parser=self._parser,
                            colour=colour,
                            text=self._text[item],
                            colour_map=self._colour_map[item],
                            offsets=offsets)

    def __add__(self, other):
        """
        Addition magic method.
        """
        if hasattr(other, "raw_text"):
            new_text = self._raw_text + other.raw_text
        else:
            new_text = self._raw_text + str(other)
        return ColouredText(new_text, parser=self._parser, colour=self._init_colour)

    def __eq__(self, other):
        """
        Equals magic method.
        """
        if isinstance(other, ColouredText):
            return self.raw_text == other.raw_text
        return NotImplemented

    def __ne__(self, other):
        """
        Not equals magic method.
        """
        x = self.__eq__(other)
        if x is not NotImplemented:
            return not x
        return NotImplemented

    def startswith(self, text):
        """
        Check whether parsed (i.e. displayed) text starts woth specified string.
        """
        return self._text.startswith(text)

    def join(self, others):
        """
        Join the list of ColouredObjects using this ColouredObject.

        :param others: the list of other objects to join.
        """
        try:
            return ColouredText(self._raw_text.join([x.raw_text for x in others]),
                                parser=self._parser,
                                colour=self._init_colour)
        except AttributeError:
            return ColouredText(self._raw_text.join(others),
                                parser=self._parser,
                                colour=self._init_colour)

    @property
    def colour_map(self):
        """
        Colour map for the processed text (for use with `paint` method).
        """
        return self._colour_map

    @property
    def raw_text(self):
        """
        Raw (unprocessed) text for this object.
        """
        return self._raw_text

    @property
    def first_colour(self):
        """
        First colour triplet used for this text.
        """
        return self._init_colour

    @property
    def last_colour(self):
        """
        Last colour triplet used for this text.
        """
        return self._last_colour