Skip to content

Commit

Permalink
1. update io lib & aec helper
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Dec 17, 2018
1 parent 6f72dc9 commit 63c874c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
42 changes: 28 additions & 14 deletions src/Oni/CLI/Helper/ANSIEscapeCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ private function __construct() {}
*
* @return string
*/
public static function SGR($param) {
public static function SGR($param)
{
if (is_array($param)) {
$param = implode(self::SEP, $param);
}
Expand All @@ -69,7 +70,8 @@ public static function SGR($param) {
*
* @return string
*/
public static function CUP($x = 0, $y = 0) {
public static function CUP($x = 0, $y = 0)
{
return self::CSI . ($y + 1) . self::SEP . ($x + 1) . 'H';
}

Expand All @@ -81,7 +83,8 @@ public static function CUP($x = 0, $y = 0) {
*
* @return string
*/
public static function moveTo($x = 0, $y = 0) {
public static function moveTo($x = 0, $y = 0)
{
return self::CUP($x, $y);
}

Expand All @@ -90,7 +93,8 @@ public static function moveTo($x = 0, $y = 0) {
*
* @return string
*/
public static function reset() {
public static function reset()
{
return self::SGR(0);
}

Expand Down Expand Up @@ -129,7 +133,8 @@ public static function color($text, $fgColor = null, $bgColor = null)
*
* @return string
*/
public static function cursorShow() {
public static function cursorShow()
{
return self::CSI . '?25h';
}

Expand All @@ -138,7 +143,8 @@ public static function cursorShow() {
*
* @return string
*/
public static function cursorHide() {
public static function cursorHide()
{
return self::CSI . '?25l';
}

Expand All @@ -149,7 +155,8 @@ public static function cursorHide() {
*
* @return string
*/
public static function cursorUp($n = 1) {
public static function cursorUp($n = 1)
{
return self::CSI . "{$n}A";
}

Expand All @@ -160,7 +167,8 @@ public static function cursorUp($n = 1) {
*
* @return string
*/
public static function cursorDown($n = 1) {
public static function cursorDown($n = 1)
{
return self::CSI . "{$n}B";
}

Expand All @@ -171,7 +179,8 @@ public static function cursorDown($n = 1) {
*
* @return string
*/
public static function cursorLeft($n = 1) {
public static function cursorLeft($n = 1)
{
return self::CSI . "{$n}C";
}

Expand All @@ -182,7 +191,8 @@ public static function cursorLeft($n = 1) {
*
* @return string
*/
public static function cursorRight($n = 1) {
public static function cursorRight($n = 1)
{
return self::CSI . "{$n}D";
}

Expand All @@ -193,7 +203,8 @@ public static function cursorRight($n = 1) {
*
* @return string
*/
public static function cursorNext($n = 1) {
public static function cursorNext($n = 1)
{
return self::CSI . "{$n}E";
}

Expand All @@ -204,7 +215,8 @@ public static function cursorNext($n = 1) {
*
* @return string
*/
public static function cursorPrev($n = 1) {
public static function cursorPrev($n = 1)
{
return self::CSI . "{$n}F";
}

Expand All @@ -213,7 +225,8 @@ public static function cursorPrev($n = 1) {
*
* @return string
*/
public static function cursorSave() {
public static function cursorSave()
{
return self::CSI . (('Apple_Terminal' === getenv('TERM_PROGRAM')) ? '7' : 's');
}

Expand All @@ -222,7 +235,8 @@ public static function cursorSave() {
*
* @return string
*/
public static function cursorLoad() {
public static function cursorLoad()
{
return self::CSI . (('Apple_Terminal' === getenv('TERM_PROGRAM')) ? '8' : 'u');
}
}
33 changes: 21 additions & 12 deletions src/Oni/CLI/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ public function ask($text, $callback = null, $fgColor = null, $bgColor = null)
*
* @param array $options
*/
public function menuSelect($options) {
public function menuSelect($options)
{
$totalIndex = count($options);
$selectedIndex = 0;
$isBreakLoop = false;
Expand All @@ -247,7 +248,6 @@ public function menuSelect($options) {

$wWidth = (int) exec('tput cols');
$wHeight = (int) exec('tput lines');
$bHeight = count($options);

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

Expand Down Expand Up @@ -285,11 +285,20 @@ public function menuSelect($options) {
$isFirstLoop = false;
}

// Get Skip Index
$skipIndex = $selectedIndex < $wHeight
? 0 : $selectedIndex - $wHeight + 1;

// Get Current Options
$currentOptions = array_slice($options, $skipIndex, $wHeight);

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

return AEC::CSI . "2K{$padding}{$option}";
}, $currentOptions, array_keys($currentOptions))));

} while ($char = stream_get_contents(STDIN, 1));

Expand Down Expand Up @@ -336,7 +345,7 @@ public function writeln($text = '', $fgColor = null, $bgColor = null)
*/
public function error($text)
{
$this->write("{$text}\n", 'red');
$this->writeln($text, 'red');
}

/**
Expand All @@ -346,7 +355,7 @@ public function error($text)
*/
public function warning($text)
{
$this->write("{$text}\n", 'yellow');
$this->writeln($text, 'yellow');
}

/**
Expand All @@ -356,7 +365,7 @@ public function warning($text)
*/
public function notice($text)
{
$this->write("{$text}\n", 'green');
$this->writeln($text, 'green');
}

/**
Expand All @@ -366,7 +375,7 @@ public function notice($text)
*/
public function info($text)
{
$this->write("{$text}\n", 'brightBlack');
$this->writeln($text, 'brightBlack');
}

/**
Expand All @@ -376,7 +385,7 @@ public function info($text)
*/
public function debug($text)
{
$this->write("{$text}\n", 'white');
$this->writeln($text, 'white');
}

/**
Expand All @@ -386,6 +395,6 @@ public function debug($text)
*/
public function log($text)
{
$this->write("{$text}\n");
$this->writeln($text);
}
}

0 comments on commit 63c874c

Please sign in to comment.