示例#1
0
    def _highlight(self, s):
        """Gathers captured styled text and intervening unstyled text into a
        :class:`prompt_toolkit.formatted_text.FormattedText` instance."""
        if not isinstance(s, str):
            msg = 'Cannot highlight type {}, only str.'
            raise TypeError(msg.format(type(s).__name__))

        default_style = Token.Text if self.uses_pygments_tokens else ''

        self.styler.clear()
        self._scan_string(s)
        locs = self.styler.locs()
        locs.append(len(s))

        i = 0
        loc = 0
        fragments = FormattedText()
        while loc < len(s):
            fragment = self.styler.get(loc)
            if fragment:
                fragments.append(fragment)
                loc += len(fragment[1])
                while locs[i] < loc:
                    i += 1
            else:
                fragments.append((default_style, s[loc:locs[i]]))
                loc = locs[i]

        return fragments
示例#2
0
def todo_string(todo: Dict[str, any]):
    if todo.get("status_code") == 404:
        return FormattedText([('orange', todo["message"])])

    result = FormattedText([])

    if "line" in todo:
        result.append((todo.get('color', ''), todo['line']))

    if "description" in todo:
        if result:
            result.append(('', ': '))
        result.append(('', todo.get('description')))

    if "url" in todo:
        if result:
            result.append(('', '\n'))
        result.append(('blue', todo.get('url', '')))

    if "message" in todo:
        if result:
            result.append(('', '\n'))
        result.append(('', todo.get('message', '')))

    return result