Skip to content

Instantly share code, notes, and snippets.

@sumeet
Created November 25, 2014 06:49
Show Gist options
  • Save sumeet/25f5a7a55353d45470ed to your computer and use it in GitHub Desktop.
Save sumeet/25f5a7a55353d45470ed to your computer and use it in GitHub Desktop.
#!/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