|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: inhere |
| 5 | + * Date: 2018-12-31 |
| 6 | + * Time: 14:27 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Inhere\Console\Examples\Controllers; |
| 10 | + |
| 11 | +use Inhere\Console\Controller; |
| 12 | +use Inhere\Console\Utils\Interact; |
| 13 | +use Inhere\Console\Utils\Show; |
| 14 | +use Toolkit\Cli\Terminal; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class InteractController |
| 18 | + * @package Inhere\Console\Examples\Controllers |
| 19 | + */ |
| 20 | +class InteractController extends Controller |
| 21 | +{ |
| 22 | + protected static $name = 'interact'; |
| 23 | + protected static $description = 'there are some demo commands for use interactive method'; |
| 24 | + |
| 25 | + public static function aliases(): array |
| 26 | + { |
| 27 | + return ['iact']; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return array |
| 32 | + */ |
| 33 | + protected static function commandAliases(): array |
| 34 | + { |
| 35 | + return [ |
| 36 | + 'cfm' => 'confirm', |
| 37 | + 'ms' => 'multiSelect', |
| 38 | + 'pwd' => 'password', |
| 39 | + 'lask' => 'limitedAsk', |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * This is a demo for use <magenta>Interact::confirm</magenta> method |
| 45 | + */ |
| 46 | + public function confirmCommand() |
| 47 | + { |
| 48 | + // can also: $this->confirm(); |
| 49 | + $a = Interact::confirm('continue'); |
| 50 | + |
| 51 | + $this->write('Your answer is: ' . ($a ? 'yes' : 'no')); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * This is a demo for use <magenta>Interact::select()</magenta> method |
| 56 | + */ |
| 57 | + public function selectCommand() |
| 58 | + { |
| 59 | + $opts = ['john', 'simon', 'rose']; |
| 60 | + // can also: $this->select(); |
| 61 | + $a = Interact::select('you name is', $opts); |
| 62 | + |
| 63 | + $this->write('Your answer is: ' . $opts[$a]); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * This is a demo for use <magenta>Interact::multiSelect()</magenta> method |
| 68 | + */ |
| 69 | + public function multiSelectCommand() |
| 70 | + { |
| 71 | + $opts = ['john', 'simon', 'rose', 'tom']; |
| 72 | + |
| 73 | + // can also: $a = Interact::multiSelect('Your friends are', $opts); |
| 74 | + $a = $this->multiSelect('Your friends are', $opts); |
| 75 | + |
| 76 | + $this->write('Your answer is: ' . json_encode($a)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * This is a demo for use <magenta>Interact::ask()</magenta> method |
| 81 | + */ |
| 82 | + public function askCommand() |
| 83 | + { |
| 84 | + $a = Interact::ask('you name is: ', null, function ($val, &$err) { |
| 85 | + if (!preg_match('/^\w{2,}$/', $val)) { |
| 86 | + $err = 'Your input must match /^\w{2,}$/'; |
| 87 | + |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + return true; |
| 92 | + }); |
| 93 | + |
| 94 | + $this->write('Your answer is: ' . $a); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * This is a demo for use <magenta>Interact::limitedAsk()</magenta> method |
| 99 | + * @options |
| 100 | + * --nv Not use validator. |
| 101 | + * --limit limit times.(default: 3) |
| 102 | + */ |
| 103 | + public function limitedAskCommand() |
| 104 | + { |
| 105 | + $times = (int)$this->input->getOpt('limit', 3); |
| 106 | + |
| 107 | + if ($this->input->getBoolOpt('nv')) { |
| 108 | + $a = Interact::limitedAsk('you name is: ', null, null, $times); |
| 109 | + } else { |
| 110 | + $a = Interact::limitedAsk('you name is: ', null, function ($val) { |
| 111 | + if (!\preg_match('/^\w{2,}$/', $val)) { |
| 112 | + Show::error('Your input must match /^\w{2,}$/'); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | + }, $times); |
| 118 | + } |
| 119 | + |
| 120 | + $this->write('Your answer is: ' . $a); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * This is a demo for input password. use: <magenta>Interact::askPassword()</magenta> |
| 125 | + * @usage {fullCommand} |
| 126 | + */ |
| 127 | + public function passwordCommand() |
| 128 | + { |
| 129 | + $pwd = $this->askPassword(); |
| 130 | + |
| 131 | + $this->write('Your input is: ' . $pwd); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * This is a demo for show cursor move on the Terminal screen |
| 136 | + */ |
| 137 | + public function cursorCommand() |
| 138 | + { |
| 139 | + $this->write('hello, this in ' . __METHOD__); |
| 140 | + $this->write('this is a message text.', false); |
| 141 | + |
| 142 | + sleep(1); |
| 143 | + Terminal::make()->cursor(Terminal::CUR_BACKWARD, 6); |
| 144 | + |
| 145 | + sleep(1); |
| 146 | + Terminal::make()->cursor(Terminal::CUR_FORWARD, 3); |
| 147 | + |
| 148 | + sleep(1); |
| 149 | + Terminal::make()->cursor(Terminal::CUR_BACKWARD, 2); |
| 150 | + |
| 151 | + sleep(2); |
| 152 | + |
| 153 | + Terminal::make()->screen(Terminal::CLEAR_LINE, 3); |
| 154 | + |
| 155 | + $this->write('after 2s scroll down 3 row.'); |
| 156 | + |
| 157 | + sleep(2); |
| 158 | + |
| 159 | + Terminal::make()->screen(Terminal::SCROLL_DOWN, 3); |
| 160 | + |
| 161 | + $this->write('after 3s clear screen.'); |
| 162 | + |
| 163 | + sleep(3); |
| 164 | + |
| 165 | + Terminal::make()->screen(Terminal::CLEAR); |
| 166 | + } |
| 167 | +} |
0 commit comments