CLISetup/OSCompatibility

- generally check for readline handler
 - only ignore \r for WIN
This commit is contained in:
Sarjuuk
2015-09-07 20:31:13 +02:00
parent 58babe24fe
commit 00bd33abfb
2 changed files with 14 additions and 9 deletions

View File

@@ -23,6 +23,7 @@ class CLISetup
const LOG_INFO = 3;
private static $win = true;
private static $hasReadline = false;
private static $logFile = '';
private static $logHandle = null;
@@ -44,7 +45,8 @@ class CLISetup
public static function init()
{
self::$win = substr(PHP_OS, 0, 3) == 'WIN';
self::$win = substr(PHP_OS, 0, 3) == 'WIN';
self::$hasReadline = function_exists('readline_callback_handler_install');
if ($_ = getopt('d', ['log::', 'locales::', 'mpqDataDir::', 'delete']))
{
@@ -342,8 +344,8 @@ class CLISetup
public static function readInput(&$fields, $singleChar = false)
{
// prevent default output on *nix (readline doen't exist for WIN)
if (!self::$win)
// prevent default output if able
if (self::$hasReadline)
readline_callback_handler_install('', function() { });
foreach ($fields as $name => $data)
@@ -366,13 +368,16 @@ class CLISetup
$char = stream_get_contents(STDIN, 1);
$keyId = ord($char);
if ($keyId == self::CHR_TAB) // ignore this one
// ignore this one
if ($keyId == self::CHR_TAB)
continue;
if ($keyId == self::CHR_CR) // also ignore this bastard!
// WIN sends \r\n as sequence, ignore one
if ($keyId == self::CHR_CR && self::$win)
continue;
if ($keyId == self::CHR_ESC) // will not be send on WIN .. other ways of returning from setup? (besides ctrl + c)
// will not be send on WIN .. other ways of returning from setup? (besides ctrl + c)
if ($keyId == self::CHR_ESC)
{
echo chr(self::CHR_BELL);
return false;
@@ -393,10 +398,10 @@ class CLISetup
else if (!$validPattern || preg_match($validPattern, $char))
{
$charBuff .= $char;
if (!$isHidden && !self::$win) // see note above
if (!$isHidden && self::$hasReadline)
echo $char;
if ($singleChar && !self::$win) // see note above
if ($singleChar && self::$hasReadline)
{
$fields[$name] = $charBuff;
break;