Python pygments.formatters 模块,TerminalFormatter() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pygments.formatters.TerminalFormatter()

项目:pdb    作者:antocuni    | 项目源码 | 文件源码
def _init_pygments(self):
        if not self.config.use_pygments:
            return False
        try:
            from pygments.lexers import PythonLexer
            from pygments.formatters import TerminalFormatter, Terminal256Formatter
        except ImportError:
            return False

        if hasattr(self, '_fmt'):
            return True
        if hasattr(self.config, 'formatter'):
            self._fmt = self.config.formatter
        else:
            Formatter = (Terminal256Formatter
                         if self.config.use_terminal256formatter
                            and '256color' in os.environ.get('TERM', '')
                         else TerminalFormatter)
            self._fmt = Formatter(bg=self.config.bg,
                                  colorscheme=self.config.colorscheme)
        self._lexer = PythonLexer()
        return True
项目:mindpark    作者:danijar    | 项目源码 | 文件源码
def color_stack_trace():

    def excepthook(type_, value, trace):
        text = ''.join(traceback.format_exception(type_, value, trace))
        try:
            from pygments import highlight
            from pygments.lexers import get_lexer_by_name
            from pygments.formatters import TerminalFormatter
            lexer = get_lexer_by_name('pytb', stripall=True)
            formatter = TerminalFormatter()
            sys.stderr.write(highlight(text, lexer, formatter))
        except Exception:
            sys.stderr.write(text)
            sys.stderr.write('Failed to colorize the traceback.')

    sys.excepthook = excepthook
    setup_thread_excepthook()
项目:mssqlcli    作者:rtrox    | 项目源码 | 文件源码
def jsonify(obj):
    """
    Convert serializable object to string.

    If Pygments is present, it will be used to colorify the json blob.
    :param list[dict] obj: object to be serialized.
    :returns: serialized json string.
    :rtype: string
    """
    formatted_json = json.dumps(stringify({"results": obj}), indent=4)

    try:
        from pygments import highlight, lexers, formatters
        colorful_json = highlight(
            formatted_json,
            lexers.JsonLexer(),
            formatters.TerminalFormatter()
        )
    except ImportError:
        return formatted_json

    return colorful_json
项目:girlfriend    作者:chihongze    | 项目源码 | 文件源码
def do_show(self, line):
        """Show me the code!
        """
        code = autopep8.fix_code("".join(self._generate_workflow_code()))
        if self.options.no_highlight:
            print code
        else:
            print highlight(code, PythonLexer(), TerminalFormatter())
项目:structlog-pretty    作者:underyx    | 项目源码 | 文件源码
def __call__(self, _, __, event_dict):
        for field, lexer in self.lexers.items():
            try:
                code = event_dict[field]
            except KeyError:
                continue
            event_dict[field] = highlight(code, lexer, TerminalFormatter())

        return event_dict
项目:git-reviewers    作者:johnnadratowski    | 项目源码 | 文件源码
def print_contributer_lines(contributer, diff_infos):
    output = []
    for diff_info in diff_infos:
        lines = diff_info["reviewers"].get(contributer)
        if not lines:
            continue

        shl.print_section(shl.BOLD, diff_info["from_hash"], diff_info["file"], file=output)

        prev_line = None
        for line in lines:
            try:
                from pygments import highlight
                from pygments.lexers import PythonLexer
                from pygments.formatters import TerminalFormatter

                code = highlight(line["code_line"], PythonLexer(), TerminalFormatter())
            except ImportError:
                code = line["code_line"]

            cur_line = int(line["line_num"])

            if prev_line and prev_line + 1 < cur_line:
                output.append("    .")
                output.append("    .")
                output.append("    .")
            output.append("{line_num: >5}|\t{code_line}".format(line_num=line["line_num"], code_line=code.rstrip()))

            prev_line = cur_line

        output.append("\n\n")

    pydoc.pager("\n".join(output))
项目:whaaaaat    作者:finklabs    | 项目源码 | 文件源码
def colorize_json(data):
    colorful_json = highlight(unicode(data, 'UTF-8'),
                              lexers.JsonLexer(),
                              formatters.TerminalFormatter())
    return colorful_json
项目:whaaaaat    作者:finklabs    | 项目源码 | 文件源码
def print_json(data):
    #colorful_json = highlight(unicode(format_json(data), 'UTF-8'),
    #                          lexers.JsonLexer(),
    #                          formatters.TerminalFormatter())
    print(colorize_json(format_json(data)))
项目:exchangelib    作者:ecederstrand    | 项目源码 | 文件源码
def highlight_xml(xml_str):
        # Highlights a string containing XML, using terminal color codes
        return highlight(xml_str, XmlLexer(), TerminalFormatter())
项目:nittygriddy    作者:cbourjau    | 项目源码 | 文件源码
def _pprint_json(dics):
    json_str = json.dumps(dics, sort_keys=True, indent=4)
    print(highlight(str(json_str, 'UTF-8'),
                    JsonLexer(), TerminalFormatter()))
项目:paste.se    作者:wanders    | 项目源码 | 文件源码
def get(self):
        try:
            paste, lang = self._get_paste(["paste", "lang"])
        except KeyError:
            self.clear()
            self.set_status(404)
            self.finish("<html><body>Not found</body></html>")
            return

        lexer = pygments.lexers.get_lexer_by_name(lang)
        formatter = TerminalFormatter()
        paste = highlight(paste, lexer, formatter)

        self.set_header("Content-Type", 'text/plain; charset="utf-8"')
        self.finish(paste)
项目:cdbcli    作者:kevinjqiu    | 项目源码 | 文件源码
def json(formatted_json):
    return pygments.highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter())
项目:cdbcli    作者:kevinjqiu    | 项目源码 | 文件源码
def javascript(code):
    return pygments.highlight(code, lexers.JavascriptLexer(), formatters.TerminalFormatter())
项目:cdbcli    作者:kevinjqiu    | 项目源码 | 文件源码
def python(code):
    return pygments.highlight(code, lexers.PythonLexer(), formatters.TerminalFormatter())
项目:cdbcli    作者:kevinjqiu    | 项目源码 | 文件源码
def erlang(code):
    return pygments.highlight(code, lexers.ErlangLexer(), formatters.TerminalFormatter())
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result
        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result
        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        lexer = get_lexer_by_name("java", stripall=True)
        lexer.add_filter(MethodFilter(method_name=method_name))
        formatter = TerminalFormatter()
        result = highlight(self.classes[class_name], lexer, formatter)
        return result
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result
        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def display_source(self, m):
        result = self.get_source_method(m)

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(result, lexer, formatter)
        print result
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def display_all(self, _class):
        result = self.get_source_class(_class)

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(result, lexer, formatter)
        print result
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result
        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        lexer = get_lexer_by_name("java", stripall=True)
        lexer.add_filter(MethodFilter(method_name=method_name))
        formatter = TerminalFormatter()
        result = highlight(self.classes[class_name], lexer, formatter)
        return result
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        lexer = get_lexer_by_name("java", stripall=True)
        formatter = TerminalFormatter()
        result = highlight(self.classes[class_name], lexer, formatter)
        return result
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def display_source(self, m):
        result = self.get_source_method(m)

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(result, lexer, formatter)
        print result
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def display_all(self, _class):
        result = self.get_source_class(_class)

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(result, lexer, formatter)
        print result
项目:yige-python    作者:wwj718    | 项目源码 | 文件源码
def output_format(obj):
    '''
    python2/3
    ???????
    obj is dict
    http://stackoverflow.com/questions/35950573/python-unicode-string-to-javascript
    '''
    formatted_json = json.dumps(obj, sort_keys=True, indent=4,ensure_ascii=False).encode('utf8')
    if (sys.version_info > (3, 0)):
            # Python 3 code in this block
            colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter())
            return colorful_json
    else:
            colorful_json = highlight(unicode(formatted_json, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter())
            return colorful_json #??????
项目:azure-cli    作者:Azure    | 项目源码 | 文件源码
def format_json_color(obj):
    from pygments import highlight, lexers, formatters
    return highlight(format_json(obj), lexers.JsonLexer(), formatters.TerminalFormatter())  # pylint: disable=no-member
项目:jsoncut    作者:json-transformations    | 项目源码 | 文件源码
def highlight_json(d, style=STYLE):
    """JSON Syntax highlighter."""
    try:
        formatter = TerminalFormatter(colorscheme=get_style(style))
    except (NameError, AttributeError):
        return d
    return pygments.highlight(d, JsonLexer(), formatter)
项目:extdbg    作者:duboviy    | 项目源码 | 文件源码
def xml(text):
    tree = minidom.parseString(text)
    print(highlight(tree.toprettyxml(), XmlLexer(), TerminalFormatter()))
    return text
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result
        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result
        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        lexer = get_lexer_by_name("java", stripall=True)
        lexer.add_filter(MethodFilter(method_name=method_name))
        formatter = TerminalFormatter()
        result = highlight(self.classes[class_name], lexer, formatter)
        return result
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result
        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def display_source(self, m):
        result = self.get_source_method(m)

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(result, lexer, formatter)
        print result
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def display_all(self, _class):
        result = self.get_source_class(_class)

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(result, lexer, formatter)
        print result
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            lexer.add_filter(MethodFilter(method_name=method_name))
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result

        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        if PYGMENTS:
            lexer = get_lexer_by_name("java", stripall=True)
            formatter = TerminalFormatter()
            result = highlight(self.classes[class_name], lexer, formatter)
            return result
        return self.classes[class_name]
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_source_method(self, method):
        class_name = method.get_class_name()
        method_name = method.get_name()

        if class_name not in self.classes:
            return ""

        lexer = get_lexer_by_name("java", stripall=True)
        lexer.add_filter(MethodFilter(method_name=method_name))
        formatter = TerminalFormatter()
        result = highlight(self.classes[class_name], lexer, formatter)
        return result
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def get_all(self, class_name):
        if class_name not in self.classes:
            return ""

        lexer = get_lexer_by_name("java", stripall=True)
        formatter = TerminalFormatter()
        result = highlight(self.classes[class_name], lexer, formatter)
        return result