Rich multi-line text. Line-by-line text output, with font attributes (e.g., color) and annotations (e.g., indices in a multi-dimensional tensor). Used as the text output of CLI commands. Can be rendered on terminal environments such as curses. This is not to be confused with Rich Text Form
| 152 | |
| 153 | |
| 154 | class RichTextLines(object): |
| 155 | """Rich multi-line text. |
| 156 | |
| 157 | Line-by-line text output, with font attributes (e.g., color) and annotations |
| 158 | (e.g., indices in a multi-dimensional tensor). Used as the text output of CLI |
| 159 | commands. Can be rendered on terminal environments such as curses. |
| 160 | |
| 161 | This is not to be confused with Rich Text Format (RTF). This class is for text |
| 162 | lines only. |
| 163 | """ |
| 164 | |
| 165 | def __init__(self, lines, font_attr_segs=None, annotations=None): |
| 166 | """Constructor of RichTextLines. |
| 167 | |
| 168 | Args: |
| 169 | lines: A list of str or a single str, representing text output to |
| 170 | screen. The latter case is for convenience when the text output is |
| 171 | single-line. |
| 172 | font_attr_segs: A map from 0-based row index to a list of 3-tuples. |
| 173 | It lists segments in each row that have special font attributes, such |
| 174 | as colors, that are not the default attribute. For example: |
| 175 | {1: [(0, 3, "red"), (4, 7, "green")], 2: [(10, 20, "yellow")]} |
| 176 | |
| 177 | In each tuple, the 1st element is the start index of the segment. The |
| 178 | 2nd element is the end index, in an "open interval" fashion. The 3rd |
| 179 | element is an object or a list of objects that represents the font |
| 180 | attribute. Colors are represented as strings as in the examples above. |
| 181 | annotations: A map from 0-based row index to any object for annotating |
| 182 | the row. A typical use example is annotating rows of the output as |
| 183 | indices in a multi-dimensional tensor. For example, consider the |
| 184 | following text representation of a 3x2x2 tensor: |
| 185 | [[[0, 0], [0, 0]], |
| 186 | [[0, 0], [0, 0]], |
| 187 | [[0, 0], [0, 0]]] |
| 188 | The annotation can indicate the indices of the first element shown in |
| 189 | each row, i.e., |
| 190 | {0: [0, 0, 0], 1: [1, 0, 0], 2: [2, 0, 0]} |
| 191 | This information can make display of tensors on screen clearer and can |
| 192 | help the user navigate (scroll) to the desired location in a large |
| 193 | tensor. |
| 194 | |
| 195 | Raises: |
| 196 | ValueError: If lines is of invalid type. |
| 197 | """ |
| 198 | if isinstance(lines, list): |
| 199 | self._lines = lines |
| 200 | elif isinstance(lines, six.string_types): |
| 201 | self._lines = [lines] |
| 202 | else: |
| 203 | raise ValueError("Unexpected type in lines: %s" % type(lines)) |
| 204 | |
| 205 | self._font_attr_segs = font_attr_segs |
| 206 | if not self._font_attr_segs: |
| 207 | self._font_attr_segs = {} |
| 208 | # TODO(cais): Refactor to collections.defaultdict(list) to simplify code. |
| 209 | |
| 210 | self._annotations = annotations |
| 211 | if not self._annotations: |
no outgoing calls
no test coverage detected