Skip to content

Commit

Permalink
1. update aec lib
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Dec 13, 2018
1 parent f693ad5 commit 6f72dc9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 71 deletions.
104 changes: 94 additions & 10 deletions src/Oni/CLI/Helper/ANSIEscapeCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,24 @@ public static function SGR($param) {
/**
* Cursor Position
*
* @param string $x
* @param string $y
* @param integer $x
* @param integer $y
*
* @return string
*/
public static function CUP($x, $y) {
public static function CUP($x = 0, $y = 0) {
return self::CSI . ($y + 1) . self::SEP . ($x + 1) . 'H';
}

/**
* CUP: Move To
*
* @param string $x
* @param string $y
* @param integer $x
* @param integer $y
*
* @return string
*/
public static function moveTo($x, $y) {
public static function moveTo($x = 0, $y = 0) {
return self::CUP($x, $y);
}

Expand All @@ -106,20 +106,20 @@ public static function reset() {
public static function color($text, $fgColor = null, $bgColor = null)
{
$startCodes = [];
$stopCodes = [];
$endCodes = [];

if (isset(self::$colorMapping[$fgColor]['fg'])) {
$startCodes[] = self::$colorMapping[$fgColor]['fg'];
$stopCodes[] = 39;
$endCodes[] = 39;
}

if (isset(self::$colorMapping[$bgColor]['bg'])) {
$startCodes[] = self::$colorMapping[$bgColor]['bg'];
$stopCodes[] = 49;
$endCodes[] = 49;
}

$start = self::SGR($startCodes);
$end = self::SGR($stopCodes);
$end = self::SGR($endCodes);

return "{$start}{$text}{$end}";
}
Expand All @@ -141,4 +141,88 @@ public static function cursorShow() {
public static function cursorHide() {
return self::CSI . '?25l';
}

/**
* Cursor Up
*
* @param integer $n
*
* @return string
*/
public static function cursorUp($n = 1) {
return self::CSI . "{$n}A";
}

/**
* Cursor Down
*
* @param integer $n
*
* @return string
*/
public static function cursorDown($n = 1) {
return self::CSI . "{$n}B";
}

/**
* Cursor Left
*
* @param integer $n
*
* @return string
*/
public static function cursorLeft($n = 1) {
return self::CSI . "{$n}C";
}

/**
* Cursor Right
*
* @param integer $n
*
* @return string
*/
public static function cursorRight($n = 1) {
return self::CSI . "{$n}D";
}

/**
* Cursor Next
*
* @param integer $n
*
* @return string
*/
public static function cursorNext($n = 1) {
return self::CSI . "{$n}E";
}

/**
* Cursor Prev
*
* @param integer $n
*
* @return string
*/
public static function cursorPrev($n = 1) {
return self::CSI . "{$n}F";
}

/**
* Cursor Save
*
* @return string
*/
public static function cursorSave() {
return self::CSI . (('Apple_Terminal' === getenv('TERM_PROGRAM')) ? '7' : 's');
}

/**
* Cursor Load
*
* @return string
*/
public static function cursorLoad() {
return self::CSI . (('Apple_Terminal' === getenv('TERM_PROGRAM')) ? '8' : 'u');
}
}
76 changes: 15 additions & 61 deletions src/Oni/CLI/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,64 +233,6 @@ public function ask($text, $callback = null, $fgColor = null, $bgColor = null)
return $answer;
}

/**
* Menu Render
*
* @param array $options
* @param integer $selectedIndex
*/
private function menuRender($options, $selectedIndex) {
$this->write(implode("\n", array_map(function ($option, $currentIndex) use ($selectedIndex) {
return ($selectedIndex === $currentIndex)
? "> {$option}" : " {$option}";
}, $options, array_keys($options))));
}

/**
* Menu Input
*
* @param array $options
*/
// public function menuInput($options) {
// $totalIndex = count($options);
// $selectedIndex = 0;
// $isBreakLoop = false;
// $char = null;

// readline_callback_handler_install('', function() {});

// do {
// switch (ord($char)) {
// case 10: // Enter Key
// $isBreakLoop = true;

// break;
// case 65: // Up Key
// if ($selectedIndex - 1 >= 0) {
// $selectedIndex--;
// }

// break;
// case 66: // Down Key
// if ($selectedIndex + 1 < $totalIndex) {
// $selectedIndex++;
// }

// break;
// }

// if ($isBreakLoop) {
// break;
// }

// $this->menuRender($options, $selectedIndex);
// } while ($char = stream_get_contents(STDIN, 1));

// readline_callback_handler_remove();

// return $selectedIndex;
// }

/**
* Menu Select
*
Expand All @@ -300,6 +242,7 @@ public function menuSelect($options) {
$totalIndex = count($options);
$selectedIndex = 0;
$isBreakLoop = false;
$isFirstLoop = true;
$char = null;

$wWidth = (int) exec('tput cols');
Expand All @@ -308,6 +251,7 @@ public function menuSelect($options) {

readline_callback_handler_install('', function() {});

// Set Cursor is Hide
$this->write(AEC::cursorHide());

do {
Expand All @@ -334,12 +278,22 @@ public function menuSelect($options) {
break;
}

$this->menuRender($options, $selectedIndex);
// Set Cursor Prev
if (false === $isFirstLoop) {
$this->write(AEC::cursorPrev($totalIndex - 1));
} else {
$isFirstLoop = false;
}

// Print Menu
$this->write(implode("\n", array_map(function ($option, $currentIndex) use ($selectedIndex) {
return ($selectedIndex === $currentIndex)
? "> {$option}" : " {$option}";
}, $options, array_keys($options))));

$this->write(AEC::moveTo(0, $wHeight - $bHeight));
} while ($char = stream_get_contents(STDIN, 1));

$this->write(AEC::moveTo(0, $wHeight));
// Set Cursor is Show
$this->writeln(AEC::cursorShow());

readline_callback_handler_remove();
Expand Down

0 comments on commit 6f72dc9

Please sign in to comment.