Skip to content

Instantly share code, notes, and snippets.

@nickpeirson
Created November 18, 2014 16:21
Show Gist options
  • Save nickpeirson/d6b5b38b8b39e9896e9b to your computer and use it in GitHub Desktop.
Save nickpeirson/d6b5b38b8b39e9896e9b to your computer and use it in GitHub Desktop.
Working POC scrolling filterable gearman top replacement
<?php
function _log($msg) {
file_put_contents('/tmp/log', $msg.PHP_EOL, FILE_APPEND);
}
function getStatus(&$widths, $filter = '') {
$filter = strtolower($filter);
_log('Getting status');
$gearmanStatus = explode("\n",`gearadmin --status`);
$statusLines = [];
foreach ($gearmanStatus as $job) {
if (strpos($job, "\t") === false) {
continue;
}
$splitJob = [];
list($splitJob['name'], $splitJob['queued'], $splitJob['active'], $splitJob['available']) = explode("\t", trim($job));
if (strpos(strtolower($splitJob['name']), $filter) === false) {
continue;
}
$statusLines[] = $splitJob;
foreach ($widths as $columnName => $width) {
$newWidth = strlen($splitJob[$columnName]);
if ($newWidth > $width) {
$widths[$columnName] = $newWidth;
}
}
}
return $statusLines;
}
termbox_init();
termbox_set_input_mode(TB_INPUT_ESC);
$position = 0;
$ev = null;
$height = termbox_height();
while (true) {
$widths = [
'name' => 0,
'queued' => 5,
'active' => 5,
'available' => 5,
];
$filter = '';
if (isset($argv[1])) {
$filter = $argv[1];
}
$gearmanStatus = getStatus($widths, $filter);
$totalWidth = array_sum($widths) + 3;
if ($totalWidth > termbox_width()) {
unset($widths['name']);
$widths['name'] = termbox_width() - 3 - array_sum($widths);
}
if (isset($ev)) {
_log('Got event:');
ob_start();
var_dump($ev);
_log(ob_get_clean());
if ($ev['type'] == TB_EVENT_KEY) {
$key = termbox_utf8_unicode_to_char($ev['ch']);
$scrolledToBottom = count($gearmanStatus) <= ($position + termbox_height() + 1);
$scrolledToTop = $position > 0;
if ($ev['key'] == TB_KEY_ARROW_UP && $scrolledToTop) {
$position--;
_log('Moving up, position is '.$position);
}elseif ($ev['key'] == TB_KEY_ARROW_DOWN && !$scrolledToBottom) {
$position++;
_log('Moving down, position is '.$position);
}elseif (strtolower($key) == 'q') {
_log('Exiting');
break;
}else{
_log('Keypressed: '.termbox_utf8_unicode_to_char($ev['ch']));
}
} elseif ($ev['type'] == TB_EVENT_RESIZE) {
$hDiff = $ev['h'] - $height;
$height = $ev['h'];
if ($scrolledToBottom) {
$position -= $hDiff;
}
_log('Height diff: '.str_pad($hDiff, 4));
}
}
termbox_clear();
$y = 0;
foreach ($gearmanStatus as $job) {
$printY = $y - $position;
if ($printY > termbox_height()) {
break;
}
if ($printY < 0) {
$y++;
continue;
}
$x = 0;
termbox_print(str_pad($job['name'], $widths['name']), $x, $printY, TB_DEFAULT, TB_DEFAULT);
$x += $widths['name'];
termbox_print(' '.str_pad($job['queued'], $widths['queued']), $x, $printY, TB_DEFAULT, TB_DEFAULT);
$x += $widths['queued'];
termbox_print(' '.str_pad($job['active'], $widths['active']), $x, $printY, TB_DEFAULT, TB_DEFAULT);
$x += $widths['active'];
termbox_print(' '.str_pad($job['available'], $widths['available']), $x, $printY, TB_DEFAULT, TB_DEFAULT);
$y++;
}
termbox_present();
$ev = termbox_peek_event(1000);
}
termbox_shutdown();
@nickpeirson
Copy link
Author

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