Skip to content

Commit b15716d

Browse files
committed
update, complete progress bar
1 parent 606384c commit b15716d

File tree

7 files changed

+285
-60
lines changed

7 files changed

+285
-60
lines changed

examples/baks/auto_complete.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
function your_callback($input, $index) {
1212
// Get info about the current buffer
13-
$rl_info = readline_info();
14-
13+
$info = readline_info();
1514
// Figure out what the entire input is
16-
// $full_input = substr($rl_info['line_buffer'], 0, $rl_info['end']);
15+
$line = substr($info['line_buffer'], 0, $info['end']);
16+
$tokens = token_get_all('<?php ' . $line);
17+
18+
var_dump($input, $index, $info, $line, $tokens);
1719

18-
//var_dump($input, $index, $rl_info);die;
1920
$matches = array();
2021

2122
// Get all matches based on the entire input buffer

examples/routes.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@
1212
use inhere\console\examples\TestCommand;
1313
use inhere\console\io\Input;
1414
use inhere\console\io\Output;
15+
use inhere\console\utils\ProgressBar;
1516

17+
$app->command(\inhere\console\examples\DemoCommand::class);
1618
$app->command('exam', function (Input $in, Output $out) {
1719
$cmd = $in->getCommand();
1820

1921
$out->info('hello, this is a test command: ' . $cmd);
2022
});
2123

2224
$app->command('test', TestCommand::class);
23-
$app->command(\inhere\console\examples\DemoCommand::class);
25+
$app->command('prg', function () {
26+
$i = 0;
27+
$total = 120;
28+
$bar = new ProgressBar();
29+
$bar->start(120);
30+
31+
while ($i <= $total) {
32+
$bar->advance();
33+
usleep(50000);
34+
$i++;
35+
}
36+
37+
$bar->finish();
38+
39+
}, 'a description message');
40+
2441
$app->controller('home', HomeController::class);

src/AbstractApp.php

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Class AbstractApp
1818
* @package inhere\console
1919
*/
20-
abstract class AbstractApp
20+
abstract class AbstractApp implements AppInterface
2121
{
2222
use InputOutputTrait;
2323
use SimpleEventStaticTrait;
@@ -84,6 +84,11 @@ abstract class AbstractApp
8484
*/
8585
protected $commands = [];
8686

87+
/**
88+
* @var array
89+
*/
90+
private $commandMessages = [];
91+
8792
/**
8893
* @var string
8994
*/
@@ -135,6 +140,7 @@ protected function init()
135140
protected function prepareRun()
136141
{
137142
// date_default_timezone_set($this->config('timeZone', 'UTC'));
143+
//new AutoCompletion(array_merge($this->getCommandNames(), $this->getControllerNames()));
138144
}
139145

140146
/**
@@ -241,9 +247,10 @@ public function controllers(array $controllers)
241247
* Register a app independent console command
242248
* @param string $name
243249
* @param string|\Closure $handler
250+
* @param null|string $description
244251
* @return $this
245252
*/
246-
public function command(string $name, $handler = null)
253+
public function command(string $name, $handler = null, $description = null)
247254
{
248255
if (!$handler && class_exists($name)) {
249256
/** @var Command $handler */
@@ -279,6 +286,10 @@ public function command(string $name, $handler = null)
279286
// is an class name string
280287
$this->commands[$name] = $handler;
281288

289+
if ($description) {
290+
$this->addCommandMessage($name, $description);
291+
}
292+
282293
return $this;
283294
}
284295

@@ -455,6 +466,8 @@ public function showCommandList($quit = true)
455466
/** @var AbstractCommand $command */
456467
if (is_subclass_of($command, Command::class)) {
457468
$desc = $command::getDescription() ?: $desPlaceholder;
469+
} else if ($msg = $this->getCommandMessage($name)) {
470+
$desc = $msg;
458471
} else if (is_string($command)) {
459472
$desc = 'A handler: ' . $command;
460473
} else if (is_object($command)) {
@@ -478,6 +491,26 @@ public function showCommandList($quit = true)
478491
$quit && $this->stop();
479492
}
480493

494+
/**
495+
* @param string $name
496+
* @param string $default
497+
* @return string
498+
*/
499+
public function getCommandMessage($name, $default = null)
500+
{
501+
return $this->commandMessages[$name] ?? $default;
502+
}
503+
504+
/**
505+
* @param string $name The command name
506+
* @param string $message
507+
* @return string
508+
*/
509+
public function addCommandMessage($name, $message)
510+
{
511+
return $this->commandMessages[$name] = $message;
512+
}
513+
481514
/**********************************************************
482515
* getter/setter methods
483516
**********************************************************/
@@ -490,6 +523,22 @@ public static function isNoColor(): bool
490523
return self::$noColor;
491524
}
492525

526+
/**
527+
* @return array
528+
*/
529+
public function getControllerNames()
530+
{
531+
return array_keys($this->controllers);
532+
}
533+
534+
/**
535+
* @return array
536+
*/
537+
public function getCommandNames()
538+
{
539+
return array_keys($this->commands);
540+
}
541+
493542
/**
494543
* @param array $controllers
495544
*/
@@ -591,4 +640,20 @@ public function isDebug()
591640
{
592641
return $this->input->getOpt('debug');
593642
}
643+
644+
/**
645+
* @return array
646+
*/
647+
public function getCommandMessages(): array
648+
{
649+
return $this->commandMessages;
650+
}
651+
652+
/**
653+
* @param array $commandMessages
654+
*/
655+
public function setCommandMessages(array $commandMessages)
656+
{
657+
$this->commandMessages = $commandMessages;
658+
}
594659
}

src/AppInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-08-14
6+
* Time: 16:51
7+
*/
8+
9+
namespace inhere\console;
10+
11+
/**
12+
* Interface AppInterface
13+
* @package inhere\console
14+
*/
15+
interface AppInterface
16+
{
17+
public function run($exit = true);
18+
public function stop($code = 0);
19+
20+
public function controller(string $name, string $controller = null);
21+
}

src/utils/AutoCompleter.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/utils/AutoCompletion.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-08-11
6+
* Time: 17:56
7+
*/
8+
9+
namespace inhere\console\utils;
10+
11+
/**
12+
* Class AutoCompletion - a simple command auto-completion tool
13+
*
14+
* @todo not available
15+
* @package inhere\console\utils
16+
*/
17+
class AutoCompletion
18+
{
19+
/**
20+
* @var callable
21+
*/
22+
private $matcher;
23+
24+
/**
25+
* @var array
26+
*/
27+
private $data;
28+
29+
/**
30+
* AutoCompletion constructor.
31+
* @param array $data
32+
* @param bool $enable
33+
*/
34+
public function __construct(array $data = [], $enable = true)
35+
{
36+
$this->data = $data;
37+
38+
if ($enable) {
39+
$this->register();
40+
}
41+
}
42+
43+
/**
44+
* Activate readline tab completion.
45+
*/
46+
public function register()
47+
{
48+
readline_completion_function([&$this, 'completionHandler']);
49+
}
50+
51+
/**
52+
* The readline_completion_function callback handler.
53+
* @param string $input the user input
54+
* @param $index
55+
*
56+
* @return array
57+
*/
58+
public function completionHandler($input, $index)
59+
{
60+
$info = readline_info();
61+
$line = substr($info['line_buffer'], 0, $info['end']);
62+
$tokens = token_get_all('<?php ' . $line);
63+
$input = trim($input);
64+
65+
if (!$input) {
66+
return $this->data;
67+
}
68+
69+
$matches = [];
70+
$matcher = $this->matcher;
71+
72+
foreach ($this->data as $item) {
73+
if ($matcher && $matcher($input, $item)) {
74+
$matches[] = $item;
75+
} elseif ($this->internalMatcher($input, $item)) {
76+
$matches[] = $item;
77+
}
78+
}
79+
80+
if (!$matches) {
81+
return [];
82+
}
83+
84+
$matches = array_unique($matches);
85+
return $matches ?: [''];
86+
}
87+
88+
/**
89+
* @param $input
90+
* @param $target
91+
* @return bool
92+
*/
93+
protected function internalMatcher($input, $target)
94+
{
95+
return stripos($target, $input) !== false;
96+
}
97+
98+
/**
99+
* @return callable
100+
*/
101+
public function getMatcher()
102+
{
103+
return $this->matcher;
104+
}
105+
106+
/**
107+
* @param callable $matcher
108+
*/
109+
public function setMatcher(callable $matcher)
110+
{
111+
$this->matcher = $matcher;
112+
}
113+
114+
/**
115+
* @return array
116+
*/
117+
public function getData(): array
118+
{
119+
return $this->data;
120+
}
121+
122+
/**
123+
* @param array $data
124+
*/
125+
public function setData(array $data)
126+
{
127+
$this->data = $data;
128+
}
129+
130+
/**
131+
* @return $this
132+
*/
133+
public function reset()
134+
{
135+
$this->data = [];
136+
137+
return $this;
138+
}
139+
}

0 commit comments

Comments
 (0)