Skip to content

Commit

Permalink
1. add menu funcs to cli/io
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Nov 29, 2018
1 parent cce5039 commit 92ad6d6
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 39 deletions.
12 changes: 11 additions & 1 deletion example/CLI/tasks/ReadTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ class ReadTask extends Task
{
public function run()
{
$this->io->writeln("What is your gender?");
$gender = $this->io->menuSelect([
'male',
'female'
]);

// $this->io->write("What is your name? ");
// $name = $this->io->read();

// or

$name = $this->io->ask('What is your name? ');

$this->io->log("Hi, {$name}!");
if (0 === $gender) {
$this->io->log("Hi, Mr.{$name}!");
} else {
$this->io->log("Hi, Ms.{$name}!");
}
}
}
182 changes: 144 additions & 38 deletions src/Oni/CLI/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,52 @@ class IO extends Basic
*/
private $_configs = [];

/**
* @var array
*/
private static $textColor = [
'black' => '0;30',
'red' => '0;31',
'green' => '0;32',
'brown' => '0;33',
'blue' => '0;34',
'purple' => '0;35',
'cyan' => '0;36',
'light_gray' => '0;37',

'dark_gray' => '1;30',
'light_red' => '1;31',
'light_green' => '1;32',
'yellow' => '1;33',
'light_blue' => '1;34',
'light_purple' => '1;35',
'light_cyan' => '1;36',
'white' => '1;37'
];

/**
* @var array
*/
private static $bgColor = [
'black' => '0;40',
'red' => '0;41',
'green' => '0;42',
'brown' => '0;43',
'blue' => '0;44',
'purple' => '0;45',
'cyan' => '0;46',
'light_gray' => '0;47',

'dark_gray' => '1;40',
'light_red' => '1;41',
'light_green' => '1;42',
'yellow' => '1;43',
'light_blue' => '1;44',
'light_purple' => '1;45',
'light_cyan' => '1;46',
'white' => '1;47'
];

/**
* Construct
*
Expand Down Expand Up @@ -233,50 +279,110 @@ public function ask($text, $callback = null, $textColor = null, $bgColor = null)
}

/**
* @var array
* Menu Render
*
* @param array $options
* @param integer $selectedIndex
*/
private static $textColor = [
'black' => '0;30',
'red' => '0;31',
'green' => '0;32',
'brown' => '0;33',
'blue' => '0;34',
'purple' => '0;35',
'cyan' => '0;36',
'light_gray' => '0;37',
private function menuRender($options, $selectedIndex) {
foreach ($options as $currentIndex => $option) {
if ($selectedIndex === $currentIndex) {
$this->writeln("> {$option}");
} else {
$this->writeln(" {$option}");
}
}
}

'dark_gray' => '1;30',
'light_red' => '1;31',
'light_green' => '1;32',
'yellow' => '1;33',
'light_blue' => '1;34',
'light_purple' => '1;35',
'light_cyan' => '1;36',
'white' => '1;37'
];
/**
* 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;
}

/**
* @var array
* Menu Select
*
* @param array $options
*/
private static $bgColor = [
'black' => '0;40',
'red' => '0;41',
'green' => '0;42',
'brown' => '0;43',
'blue' => '0;44',
'purple' => '0;45',
'cyan' => '0;46',
'light_gray' => '0;47',
public function menuSelect($options) {
$totalIndex = count($options);
$selectedIndex = 0;
$isBreakLoop = false;
$char = null;

'dark_gray' => '1;40',
'light_red' => '1;41',
'light_green' => '1;42',
'yellow' => '1;43',
'light_blue' => '1;44',
'light_purple' => '1;45',
'light_cyan' => '1;46',
'white' => '1;47'
];
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;
}

/**
* Color
Expand Down

0 comments on commit 92ad6d6

Please sign in to comment.