Skip to content

Instantly share code, notes, and snippets.

@nsfmc
Created November 26, 2012 04:37
Show Gist options
  • Save nsfmc/4146635 to your computer and use it in GitHub Desktop.
Save nsfmc/4146635 to your computer and use it in GitHub Desktop.
Blame current line in textmate (html mode dialog)
#!/usr/bin/env python
#
# beforeRunningCommand: nop
# input: selection
# inputFormat: text
# name: Blame current line
# outputCaret: afterOutput
# outputFormat: html
# outputLocation: toolTip
# scope: attr.scm.hg
# semanticClass: action.scm.blame
# uuid: 9FE176E2-9D45-43B9-8411-909B19D77920
# version: 2
#
import os
from sys import stdout, stdin
import subprocess
import re
import plistlib
hg = os.getenv('TM_HG', 'hg')
os.chdir(os.getenv('TM_DIRECTORY'))
def popen_results(args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
return proc.communicate()[0]
def tool_tip(content, format='text'):
"""docstring for tool_tip"""
dialog = os.getenv('DIALOG')
popen_results([dialog, 'tooltip', '--%s' % format, content])
file_path = os.getenv('TM_FILEPATH')
line_no = '%sp' % os.getenv('TM_LINE_NUMBER')
blame_out = subprocess.Popen([hg, 'blame', '-cv', file_path], stdout=subprocess.PIPE)
sed_out = subprocess.Popen(['sed', '-n', line_no], stdin=blame_out.stdout, stdout=subprocess.PIPE)
blame_out.stdout.close()
final_output = sed_out.communicate()[0]
revset = final_output.split(":")[0]
fontName = popen_results([os.getenv('TM_QUERY'), '--setting', 'fontName']) or "monospace"
fontSize = popen_results([os.getenv('TM_QUERY'), '--setting', 'fontSize']) or "12"
theme = plistlib.readPlist(os.getenv('TM_CURRENT_THEME_PATH'))
def settings_for_scope(theme_plist, scope):
"""docstring for get_key_from_plist"""
settings = [x for x in theme_plist.settings
if x.has_key('scope')
and x.has_key('settings')
and x['scope'] == scope]
return settings[0] if settings else { 'settings':{} }
deleted_color = (settings_for_scope(theme, "markup.deleted").
get('settings', {}).
get('foreground', 'red'))
inserted_color = (settings_for_scope(theme, "markup.deleted").
get('settings', {}).
get('foreground', 'green'))
template = """<html><head>
<style type="text/css" media="screen">
body {
font-family: "%s";
font-size: %spx;
background-color: #002b36;
color: #839496;
border: 1px solid #000;
padding: 5px;
white-space: pre;
}
.p { color: #859900; }
.n { color: #dc322f; }
strong { color: #b58900; font-weight: normal; }
</style></head><body>%s</body></html>
"""
desc = popen_results([hg, 'log', '--stat', '-vr', revset])
info = (template % (fontName, fontSize, desc)).strip()
info = re.sub(r"\|(\s+)(\d+)(\s+)(\+*)(-*)\n", r"|\1\2\3<span class='p'>\4</span><span class='n'>\5</span>\n", info)
info = re.sub(r"changeset:(\s+)(\d+):([0-9a-z]+)\n", r"<strong>changeset:\1\2:\3</strong>\n", info)
tool_tip(info, format="html")
@nsfmc
Copy link
Author

nsfmc commented Nov 26, 2012

obviously this should be resaved as blame.tmCommand but then it wouldn't have the python syntax highlighting. the current implementation is a bit borked since it doesn't use any of the colors it asks for (and it even gets the wrong ones), but it's perfectly usable if you don't mind seeing the blame info in the solarized dark theme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment