Skip to content

Instantly share code, notes, and snippets.

@marvinrabe
Forked from jakob-stoeck/Copy as textile
Last active June 1, 2016 11:16
Show Gist options
  • Save marvinrabe/87b52be29567d38326eac571277efa78 to your computer and use it in GitHub Desktop.
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.
#!/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