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

@@ -176,7 +176,7 @@ class SpellList extends BaseType
if ($mv < 0) // all stats
{
for ($iMod = ITEM_MOD_AGILITY; $iMod <= ITEM_MOD_STAMINA; $iMod++)
Util::arraySumByKey($stats, [iMod => $pts]);
Util::arraySumByKey($stats, [$iMod => $pts]);
}
else if ($mv == STAT_STRENGTH) // one stat
Util::arraySumByKey($stats, [ITEM_MOD_STRENGTH => $pts]);

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;
@@ -45,6 +46,7 @@ class CLISetup
public static function init()
{
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;