Skip to content

Commit df3d174

Browse files
committed
some update for process tool. complete Show::pending()
1 parent 36e9b35 commit df3d174

File tree

12 files changed

+313
-174
lines changed

12 files changed

+313
-174
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ Show::table($data, 'a table', $opts);
553553

554554
> 渲染效果请看下面的预览
555555
556-
![table-show](images/table-show.png)
556+
![table-show](docs/screenshots/table-show.png)
557557

558558
### 快速的渲染一个帮助信息面板
559559

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
}
2727
},
2828
"suggest": {
29-
"inhere/simple-print-tool": "Very lightweight data printing tools"
29+
"symfony/process": "php process operation"
3030
}
3131
}
File renamed without changes.

examples/Controllers/HomeController.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Inhere\Console\Examples\Controllers;
44

5-
use Inhere\Console\Components\AnsiCode;
5+
use Inhere\Console\Components\Terminal;
66
use Inhere\Console\Components\ArtFont;
77
use Inhere\Console\Components\Download;
88
use Inhere\Console\Controller;
99
use Inhere\Console\IO\Input;
1010
use Inhere\Console\Utils\Helper;
1111
use Inhere\Console\Utils\Interact;
12+
use Inhere\Console\Utils\ProgressBar;
1213
use Inhere\Console\Utils\Show;
1314

1415
/**
@@ -157,23 +158,27 @@ public function spinnerCommand()
157158
Show::spinner();
158159
usleep(100);
159160
}
161+
162+
Show::spinner('Done', true);
160163
}
161164

162165
/**
163166
* dynamic notice message show: pending
164167
*/
165168
public function pendingCommand()
166169
{
167-
$total = 5000;
170+
$total = 8000;
168171

169172
while ($total--) {
170-
Show::spinner();
171-
usleep(100);
173+
Show::pending();
174+
usleep(200);
172175
}
176+
177+
Show::pending('Done', true);
173178
}
174179

175180
/**
176-
* a progress bar example show
181+
* a progress bar example show, by Show::progressBar()
177182
* @options
178183
* --type the progress type, allow: bar,txt. <cyan>txt</cyan>
179184
* --done-char the done show char. <info>=</info>
@@ -212,6 +217,26 @@ public function progressCommand($input)
212217
return 0;
213218
}
214219

220+
/**
221+
* a progress bar example show, by class ProgressBar
222+
* @throws \LogicException
223+
*/
224+
public function prgCommand()
225+
{
226+
$i = 0;
227+
$total = 120;
228+
$bar = new ProgressBar();
229+
$bar->start(120);
230+
231+
while ($i <= $total) {
232+
$bar->advance();
233+
usleep(50000);
234+
$i++;
235+
}
236+
237+
$bar->finish();
238+
}
239+
215240
/**
216241
* output format message: title
217242
*/
@@ -592,39 +617,36 @@ public function downCommand()
592617
}
593618

594619
/**
595-
* This is a demo for show cursor move on the screen
620+
* This is a demo for show cursor move on the Terminal screen
596621
*/
597622
public function cursorCommand()
598623
{
599624
$this->write('hello, this in ' . __METHOD__);
600-
601-
// $this->output->panel($_SERVER, 'Server information', '');
602-
603625
$this->write('this is a message text.', false);
604626

605627
sleep(1);
606-
AnsiCode::make()->cursor(AnsiCode::CURSOR_BACKWARD, 6);
628+
Terminal::make()->cursor(Terminal::CURSOR_BACKWARD, 6);
607629

608630
sleep(1);
609-
AnsiCode::make()->cursor(AnsiCode::CURSOR_FORWARD, 3);
631+
Terminal::make()->cursor(Terminal::CURSOR_FORWARD, 3);
610632

611633
sleep(1);
612-
AnsiCode::make()->cursor(AnsiCode::CURSOR_BACKWARD, 2);
634+
Terminal::make()->cursor(Terminal::CURSOR_BACKWARD, 2);
613635

614636
sleep(2);
615637

616-
AnsiCode::make()->screen(AnsiCode::CLEAR_LINE, 3);
638+
Terminal::make()->screen(Terminal::CLEAR_LINE, 3);
617639

618640
$this->write('after 2s scroll down 3 row.');
619641

620642
sleep(2);
621643

622-
AnsiCode::make()->screen(AnsiCode::SCROLL_DOWN, 3);
644+
Terminal::make()->screen(Terminal::SCROLL_DOWN, 3);
623645

624646
$this->write('after 3s clear screen.');
625647

626648
sleep(3);
627649

628-
AnsiCode::make()->screen(AnsiCode::CLEAR);
650+
Terminal::make()->screen(Terminal::CLEAR);
629651
}
630652
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-12-22
6+
* Time: 11:44
7+
*/
8+
9+
namespace Inhere\Console\Examples\Controllers;
10+
11+
use Inhere\Console\Controller;
12+
13+
/**
14+
* Class ProcessController
15+
* @package Inhere\Console\Examples\Controllers
16+
*/
17+
class ProcessController extends Controller
18+
{
19+
protected static $name = 'process';
20+
21+
protected static $description = 'Some simple process to create and use examples';
22+
}

examples/routes.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,34 @@
44
* User: inhere
55
* Date: 2016/12/7
66
* Time: 12:46
7-
*
87
* @var Inhere\Console\Application $app
98
*/
109

1110
use Inhere\Console\BuiltIn\PharController;
1211
use Inhere\Console\Examples\Commands\DemoCommand;
13-
use Inhere\Console\Examples\Controllers\HomeController;
1412
use Inhere\Console\Examples\Commands\TestCommand;
13+
use Inhere\Console\Examples\Controllers\HomeController;
14+
use Inhere\Console\Examples\Controllers\ProcessController;
1515
use Inhere\Console\IO\Input;
1616
use Inhere\Console\IO\Output;
17-
use Inhere\Console\Utils\ProgressBar;
1817

1918
$app->command(DemoCommand::class);
2019
$app->command('exam', function (Input $in, Output $out) {
2120
$cmd = $in->getCommand();
2221

2322
$out->info('hello, this is a test command: ' . $cmd);
24-
});
23+
}, 'a description message');
2524

2625
$app->command('test', TestCommand::class, [
2726
'aliases' => ['t']
2827
]);
2928

30-
$app->command('prg', function () {
31-
$i = 0;
32-
$total = 120;
33-
$bar = new ProgressBar();
34-
$bar->start(120);
35-
36-
while ($i <= $total) {
37-
$bar->advance();
38-
usleep(50000);
39-
$i++;
40-
}
41-
42-
$bar->finish();
43-
44-
}, 'a description message');
29+
$app->controller(PharController::class);
4530

4631
$app->controller('home', HomeController::class, [
4732
'aliases' => ['h']
4833
]);
4934

50-
$app->controller(PharController::class);
35+
$app->controller(ProcessController::class, null, [
36+
'prc'
37+
]);

src/Components/Notify/Pending.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
*/
1717
class Pending extends NotifyMessage
1818
{
19-
19+
/** @var int Speed value. allow 1 - 100 */
20+
private $speed = 20;
2021
}

src/Components/AnsiCode.php renamed to src/Components/Terminal.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
use Inhere\Console\Utils\Show;
1212

1313
/**
14-
* Class AnsiCode terminal
14+
* Class Terminal - terminal control by ansiCode
1515
* @package Inhere\Console\Components
1616
*
1717
* 2K 清除本行
1818
* \x0D = \r = 13 回车,回到行首
1919
* ESC = \x1B = 27
2020
*/
21-
final class AnsiCode
21+
final class Terminal
2222
{
2323
const BEGIN_CHAR = "\033[";
2424

@@ -133,8 +133,8 @@ public static function make()
133133
* build ansi code string
134134
*
135135
* ```
136-
* AnsiCode::build(null, 'u'); // "\033[s" Saves the current cursor position
137-
* AnsiCode::build(0); // "\033[0m" Build end char, Resets any ANSI format
136+
* Terminal::build(null, 'u'); // "\033[s" Saves the current cursor position
137+
* Terminal::build(0); // "\033[0m" Build end char, Resets any ANSI format
138138
* ```
139139
*
140140
* @param mixed $format

src/Utils/CliUtil.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,62 @@ public static function getTempDir()
142142
return $tmp;
143143
}
144144

145+
/**
146+
* get screen size
147+
*
148+
* ```php
149+
* list($width, $height) = Helper::getScreenSize();
150+
* ```
151+
* @from Yii2
152+
* @param boolean $refresh whether to force checking and not re-use cached size value.
153+
* This is useful to detect changing window size while the application is running but may
154+
* not get up to date values on every terminal.
155+
* @return array|boolean An array of ($width, $height) or false when it was not able to determine size.
156+
*/
157+
public static function getScreenSize($refresh = false)
158+
{
159+
static $size;
160+
if ($size !== null && !$refresh) {
161+
return $size;
162+
}
163+
164+
if (self::bashIsAvailable()) {
165+
// try stty if available
166+
$stty = [];
167+
168+
if (
169+
exec('stty -a 2>&1', $stty) &&
170+
preg_match('/rows\s+(\d+);\s*columns\s+(\d+);/mi', implode(' ', $stty), $matches)
171+
) {
172+
return ($size = [$matches[2], $matches[1]]);
173+
}
174+
175+
// fallback to tput, which may not be updated on terminal resize
176+
if (($width = (int)exec('tput cols 2>&1')) > 0 && ($height = (int)exec('tput lines 2>&1')) > 0) {
177+
return ($size = [$width, $height]);
178+
}
179+
180+
// fallback to ENV variables, which may not be updated on terminal resize
181+
if (($width = (int)getenv('COLUMNS')) > 0 && ($height = (int)getenv('LINES')) > 0) {
182+
return ($size = [$width, $height]);
183+
}
184+
}
185+
186+
if (Helper::isOnWindows()) {
187+
$output = [];
188+
exec('mode con', $output);
189+
190+
if (isset($output[1]) && strpos($output[1], 'CON') !== false) {
191+
return ($size = [
192+
(int)preg_replace('~\D~', '', $output[3]),
193+
(int)preg_replace('~\D~', '', $output[4])
194+
]);
195+
}
196+
}
197+
198+
return ($size = false);
199+
}
200+
145201
/**
146202
* @param string $program
147203
* @return int|string

0 commit comments

Comments
 (0)