我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pygments.formatters.TerminalFormatter()。
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
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()
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
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())
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
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))
def colorize_json(data): colorful_json = highlight(unicode(data, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter()) return colorful_json
def print_json(data): #colorful_json = highlight(unicode(format_json(data), 'UTF-8'), # lexers.JsonLexer(), # formatters.TerminalFormatter()) print(colorize_json(format_json(data)))
def highlight_xml(xml_str): # Highlights a string containing XML, using terminal color codes return highlight(xml_str, XmlLexer(), TerminalFormatter())
def _pprint_json(dics): json_str = json.dumps(dics, sort_keys=True, indent=4) print(highlight(str(json_str, 'UTF-8'), JsonLexer(), TerminalFormatter()))
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)
def json(formatted_json): return pygments.highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter())
def javascript(code): return pygments.highlight(code, lexers.JavascriptLexer(), formatters.TerminalFormatter())
def python(code): return pygments.highlight(code, lexers.PythonLexer(), formatters.TerminalFormatter())
def erlang(code): return pygments.highlight(code, lexers.ErlangLexer(), formatters.TerminalFormatter())
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]
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]
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
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
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
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
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 #??????
def format_json_color(obj): from pygments import highlight, lexers, formatters return highlight(format_json(obj), lexers.JsonLexer(), formatters.TerminalFormatter()) # pylint: disable=no-member
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)
def xml(text): tree = minidom.parseString(text) print(highlight(tree.toprettyxml(), XmlLexer(), TerminalFormatter())) return text