-
-
Save marvinrabe/87b52be29567d38326eac571277efa78 to your computer and use it in GitHub Desktop.
Copies SequelPro results as a Markdown table to use it in markdown-supported systems.
This file contains hidden or 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/php | |
<?php | |
function parseInput() { | |
$in = fopen('php://stdin', 'r'); | |
$result = array(); | |
while($line = fgetcsv($in, 0, "\t")) { | |
$result[] = $line; | |
} | |
fclose($in); | |
return $result; | |
} | |
function buildRow($data, &$array) { | |
$array[] = '|' . implode('|', $data) . '|'; | |
} | |
function buildHeaderRow($data, &$array) { | |
buildRow($data, $array); | |
$columns = count($data); | |
buildRow(array_fill(0, $columns, '---'), $array); | |
} | |
function buildTable($data) { | |
$result = []; | |
$header = array_shift($data); | |
buildHeaderRow($header, $result); | |
foreach($data as $row) { | |
buildRow($row, $result); | |
} | |
return implode("\n", $result); | |
} | |
function copyString($string) { | |
$cmd = 'echo ' . escapeshellarg($string) . ' | __CF_USER_TEXT_ENCODING=' . posix_getuid() . ':0x8000100:0x8000100 pbcopy'; | |
shell_exec($cmd); | |
} | |
$data = parseInput(); | |
$table = buildTable($data); | |
copyString($table); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment