Skip to content

Commit 324e80e

Browse files
committed
some modify for user interactive method. bug fixed for write() need quit
1 parent b47e8a5 commit 324e80e

File tree

7 files changed

+204
-67
lines changed

7 files changed

+204
-67
lines changed
38.4 KB
Loading
119 KB
Loading

examples/Controllers/HomeController.php

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Inhere\Console\Examples\Controllers;
44

5-
use Inhere\Console\Controller;
6-
use Inhere\Console\IO\Input;
75
use Inhere\Console\Components\AnsiCode;
86
use Inhere\Console\Components\Download;
7+
use Inhere\Console\Controller;
8+
use Inhere\Console\IO\Input;
99
use Inhere\Console\Utils\Helper;
1010
use Inhere\Console\Utils\Interact;
1111
use Inhere\Console\Utils\Show;
@@ -37,17 +37,6 @@ public function indexCommand()
3737
$this->write('hello, welcome!! this is ' . __METHOD__);
3838
}
3939

40-
/**
41-
* a example for input password on command line
42-
* @usage {fullCommand}
43-
*/
44-
public function passwdCommand()
45-
{
46-
$pwd = $this->askPassword();
47-
48-
$this->write('Your input is:' . $pwd);
49-
}
50-
5140
/**
5241
* a example for use color text output on command
5342
* @usage {fullCommand}
@@ -392,18 +381,74 @@ public function confirmCommand()
392381
{
393382
$a = Interact::confirm('continue');
394383

395-
$this->write('you answer is: ' . ($a ? 'yes' : 'no'));
384+
$this->write('Your answer is: ' . ($a ? 'yes' : 'no'));
396385
}
397386

398387
/**
399-
* This is a demo for use <magenta>Interact::select</magenta> method
388+
* This is a demo for use <magenta>Interact::select()</magenta> method
400389
*/
401390
public function selectCommand()
402391
{
403392
$opts = ['john', 'simon', 'rose'];
404393
$a = Interact::select('you name is', $opts);
405394

406-
$this->write('you answer is: ' . $opts[$a]);
395+
$this->write('Your answer is: ' . $opts[$a]);
396+
}
397+
398+
/**
399+
* This is a demo for use <magenta>Interact::ask()</magenta> method
400+
*/
401+
public function askCommand()
402+
{
403+
$a = Interact::ask('you name is: ', null, function ($val, &$err) {
404+
if (!preg_match('/^\w{2,}$/', $val)) {
405+
$err = 'Your input must match /^\w{2,}$/';
406+
407+
return false;
408+
}
409+
410+
return true;
411+
});
412+
413+
$this->write('Your answer is: ' . $a);
414+
}
415+
416+
/**
417+
* This is a demo for use <magenta>Interact::limitedAsk()</magenta> method
418+
* @options
419+
* --nv Not use validator.
420+
* --limit limit times.(default: 3)
421+
*/
422+
public function limitedAskCommand()
423+
{
424+
$times = (int)$this->input->getOpt('limit', 3);
425+
426+
if ($this->input->getBoolOpt('nv')) {
427+
$a = Interact::limitedAsk('you name is: ', null, null, $times);
428+
} else {
429+
$a = Interact::limitedAsk('you name is: ', null, function ($val) {
430+
if (!preg_match('/^\w{2,}$/', $val)) {
431+
Show::error('Your input must match /^\w{2,}$/');
432+
433+
return false;
434+
}
435+
436+
return true;
437+
}, $times);
438+
}
439+
440+
$this->write('Your answer is: ' . $a);
441+
}
442+
443+
/**
444+
* a example for input password on command line. use: <magenta>Interact::askPassword()</magenta>
445+
* @usage {fullCommand}
446+
*/
447+
public function pwdCommand()
448+
{
449+
$pwd = $this->askPassword();
450+
451+
$this->write('Your input is: ' . $pwd);
407452
}
408453

409454
/**

src/IO/Output.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,20 @@ public function clearBuffer()
7676
/**
7777
* stop buffering and flush buffer text
7878
* {@inheritdoc}
79+
* @see Show::stopBuffer()
7980
*/
80-
public function stopBuffer($nl = true, $quit = false, array $opts = [])
81+
public function stopBuffer($flush = true, $nl = false, $quit = false, array $opts = [])
8182
{
82-
Show::stopBuffer($nl, $quit, $opts);
83+
Show::stopBuffer($flush, $nl, $quit, $opts);
8384
}
8485

8586
/**
8687
* stop buffering and flush buffer text
8788
* {@inheritdoc}
8889
*/
89-
public function flush($nl = true, $quit = false, array $opts = [])
90+
public function flush($nl = false, $quit = false, array $opts = [])
9091
{
91-
$this->stopBuffer($nl, $quit, $opts);
92+
$this->stopBuffer(true, $nl, $quit, $opts);
9293
}
9394

9495
/***************************************************************************
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-12-19
6+
* Time: 9:44
7+
*/
8+
9+
namespace Inhere\Console\Traits;
10+
11+
/**
12+
* Trait AdvancedFormatOutputTrait
13+
* @package Inhere\Console\Traits
14+
*/
15+
trait AdvancedFormatOutputTrait
16+
{
17+
18+
}

0 commit comments

Comments
 (0)