Skip to content

Instantly share code, notes, and snippets.

@pdt256
Created August 9, 2012 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdt256/3307839 to your computer and use it in GitHub Desktop.
Save pdt256/3307839 to your computer and use it in GitHub Desktop.
mack
#!/usr/bin/env php
<?php
/**
* Converts grep and ack output (file:line:matching text) into TextMate links so
* you can click them to open the file.
*
* @author Anthony Bush
* @copyright Anthony Bush (http://anthonybush.com/), 12 February, 2010
* @package default
**/
/**
* Define DocBlock
**/
$title = 'mack on ' . date('m/d @ H:i');
$cmd = 'unknown command | mack';
$cmd .= "\n" . 'Consider using "ackm search_terms"; just put this in your .bash_profile: function ackm() { tmpAckm="$@"; ack "$@" | mack --cmd="ack $tmpAckm"; }';
$scriptName = array_shift($argv);
foreach ($argv as $arg)
{
if (preg_match('/--cmd=(.*)/', $arg, $match))
{
$cmd = $match[1];
$title = $cmd;
}
}
// Get header and styles
ob_start();
?>
<html>
<head>
<?php if (!empty($title)) { echo '<title>' . htmlentities($title) . '</title>'; } ?>
<style type="text/css" media="screen">
/* <![CDATA[ */
body, table {
margin: 0;
padding: 0;
color #000;
}
body, th, td, pre {
font-family: Monaco,monospace;
font-size: 9pt;
}
th {
text-align: right;
}
a {
text-decoration: none;
}
a:link,
a:hover,
a:active {
color: blue;
}
a > span.code {
color: black;
}
#cmd {
padding: .5em 1em;
background: #333;
color: #fff;
}
#cmd .path {
color: #3c0;
}
#notes {
background: #ccc;
padding: .5em 1em;
}
#notes textarea {
width: 100%;
}
#results {
padding: .5em 1em;
}
#results > div {
white-space: pre;
}
#results > div:hover {
background: #FF9;
}
/* ]]> */
</style>
</head>
<body>
<div id="cmd"><span class="path"><?php echo htmlentities($_SERVER['PWD']); ?>$</span> <?php echo nl2br(htmlentities($cmd)) ?></div>
<div id="notes">Notes:<br /><textarea rows="3" cols="100"></textarea></div>
<div id="results"><?php
$header = ob_get_clean();
$footer = '</body></html>';
// TextMate link format: txmt://open/?url=file://~/.bash_profile&line=11&column=2
// See: http://manual.macromates.com/en/using_textmate_from_terminal#url_scheme_html
$tmpFileName = tempnam('/tmp', 'mack') . '.html';
$handle = fopen($tmpFileName, "w");
fwrite($handle, $header);
$i = 0;
$baseDir = getcwd() . '/';
while ($line = fgets(STDIN))
{
if (!preg_match('/^([^:]+):(\d+):(.*)/', $line, $match))
{
continue;
}
++$i;
if ($match[1][0] == '/') {
$file = 'file://' . $match[1];
} else {
$file = 'file://' . $baseDir . $match[1];
}
$url = 'txmt://open/?url=' . $file . '&line=' . $match[2];
fwrite($handle, '<div><input type="checkbox" /><input type="checkbox" id="c' . $i . '" /><a href="' . $url . '" onclick="document.getElementById(\'c' . $i . '\').checked=true;">' . htmlentities($match[1] . ':' . $match[2] . ':') . '<span class="code">' . htmlentities($match[3]) . "</span></a></div>\n");
}
fwrite($handle, "\n" . $i . ' matches.</div>');
fwrite($handle, $footer);
fclose($handle);
passthru('open ' . escapeshellarg($tmpFileName));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment