Skip to content

Instantly share code, notes, and snippets.

@dmouse
Last active November 20, 2015 18:19
Show Gist options
  • Save dmouse/e7cf4ccda23c4a33ddc8 to your computer and use it in GitHub Desktop.
Save dmouse/e7cf4ccda23c4a33ddc8 to your computer and use it in GitHub Desktop.
Move cursos in PHP Command Line from Symfony Console check https://github.com/dmouse/select-helper
<?php
$sttyMode = shell_exec('stty -g');
shell_exec('stty -icanon -echo');
$stdout = fopen('php://stdout', 'w');
fwrite($stdout, " 1.- option 1" . PHP_EOL );
fwrite($stdout, " 2.- option 2" . PHP_EOL );
fwrite($stdout, " 3.- option 3" . PHP_EOL );
fwrite($stdout, " 4.- option 4");
$character = mb_convert_encoding("\x21\xfe", 'UTF-8', 'UTF-16BE');
fwrite($stdout, "\x0D");
fwrite($stdout, "\033[1m\033[1;32m");
fwrite($stdout, $character);
fwrite($stdout, "\x0D");
fwrite($stdout, "\033[?25l");
$down = $count = 3;
$up = 0;
while (!feof(STDIN)) {
$c = fread(STDIN, 1);
#fwrite($stdout, ord($c) . "-");
#fwrite($stdout, $c );
if ("\x1b" === $c) {
$c = fread(STDIN, 1);
if ("\x5b" === $c) {
$c = fread(STDIN, 1);
if ("\x41" === $c) {
if ($up < $count) {
fwrite($stdout, " ");
fwrite($stdout, "\x0D");
fwrite($stdout, "\033[1A");
fwrite($stdout, "\033[1;32m");
fwrite($stdout, $character);
fwrite($stdout, "\x0D");
$up++;
$down--;
}
}
else if ("\x42" === $c) {
if ($down < $count) {
fwrite($stdout, " ");
fwrite($stdout, "\x0D");
fwrite($stdout, "\033[1B");
fwrite($stdout, $character);
fwrite($stdout, "\x0D");
$down++;
$up--;
}
}
}
}
else if("\n" === $c) {
$final = $up + $down + 1;
fwrite($stdout, "\033[{$final}B");
break;
}
}
shell_exec(sprintf('stty %s', $sttyMode));
fwrite($stdout, "\033[?25h");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment