Created
November 25, 2014 06:49
-
-
Save sumeet/25f5a7a55353d45470ed to your computer and use it in GitHub Desktop.
syntax highlighted ruby stack traces. before: http://s.goobtown.net/aglnb29ic2hvdHNyDQsSBFNob3QY0YaAAQw. after: http://s.goobtown.net/aglnb29ic2hvdHNyDQsSBFNob3QY4dSAAQw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pygments | |
import re | |
import sys | |
from pygments.lexers import RubyLexer | |
from pygments.formatters import TerminalFormatter | |
filename_line_no_re = re.compile(r'\b([^: ]+)[:](\d+)[:]in\b') | |
def get_source_code_annotation(line): | |
match = filename_line_no_re.search(line) | |
if not match: | |
return "" | |
filename, line_no_str = match.groups() | |
return get_source_code(filename, int(line_no_str)) | |
def get_source_code(filename, lineno): | |
lines = open(filename, 'r').read().splitlines() | |
return lines[lineno - 1] | |
def indent(line): | |
return " " * 7 + line.lstrip() | |
def highlight(code): | |
return pygments.highlight(code, formatter=TerminalFormatter(bg='dark'), | |
lexer=RubyLexer()) | |
if __name__ == '__main__': | |
for line in sys.stdin: | |
sys.stdout.write(line) | |
source_code_annotation = get_source_code_annotation(line) | |
if source_code_annotation: | |
sys.stdout.write(indent(highlight(source_code_annotation))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment