Skip to content

Commit

Permalink
some update for process tool. complete Show::pending()
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 22, 2017
1 parent 36e9b35 commit df3d174
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 174 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ Show::table($data, 'a table', $opts);

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

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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
}
},
"suggest": {
"inhere/simple-print-tool": "Very lightweight data printing tools"
"symfony/process": "php process operation"
}
}
File renamed without changes
52 changes: 37 additions & 15 deletions examples/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Inhere\Console\Examples\Controllers;

use Inhere\Console\Components\AnsiCode;
use Inhere\Console\Components\Terminal;
use Inhere\Console\Components\ArtFont;
use Inhere\Console\Components\Download;
use Inhere\Console\Controller;
use Inhere\Console\IO\Input;
use Inhere\Console\Utils\Helper;
use Inhere\Console\Utils\Interact;
use Inhere\Console\Utils\ProgressBar;
use Inhere\Console\Utils\Show;

/**
Expand Down Expand Up @@ -157,23 +158,27 @@ public function spinnerCommand()
Show::spinner();
usleep(100);
}

Show::spinner('Done', true);
}

/**
* dynamic notice message show: pending
*/
public function pendingCommand()
{
$total = 5000;
$total = 8000;

while ($total--) {
Show::spinner();
usleep(100);
Show::pending();
usleep(200);
}

Show::pending('Done', true);
}

/**
* a progress bar example show
* a progress bar example show, by Show::progressBar()
* @options
* --type the progress type, allow: bar,txt. <cyan>txt</cyan>
* --done-char the done show char. <info>=</info>
Expand Down Expand Up @@ -212,6 +217,26 @@ public function progressCommand($input)
return 0;
}

/**
* a progress bar example show, by class ProgressBar
* @throws \LogicException
*/
public function prgCommand()
{
$i = 0;
$total = 120;
$bar = new ProgressBar();
$bar->start(120);

while ($i <= $total) {
$bar->advance();
usleep(50000);
$i++;
}

$bar->finish();
}

/**
* output format message: title
*/
Expand Down Expand Up @@ -592,39 +617,36 @@ public function downCommand()
}

/**
* This is a demo for show cursor move on the screen
* This is a demo for show cursor move on the Terminal screen
*/
public function cursorCommand()
{
$this->write('hello, this in ' . __METHOD__);

// $this->output->panel($_SERVER, 'Server information', '');

$this->write('this is a message text.', false);

sleep(1);
AnsiCode::make()->cursor(AnsiCode::CURSOR_BACKWARD, 6);
Terminal::make()->cursor(Terminal::CURSOR_BACKWARD, 6);

sleep(1);
AnsiCode::make()->cursor(AnsiCode::CURSOR_FORWARD, 3);
Terminal::make()->cursor(Terminal::CURSOR_FORWARD, 3);

sleep(1);
AnsiCode::make()->cursor(AnsiCode::CURSOR_BACKWARD, 2);
Terminal::make()->cursor(Terminal::CURSOR_BACKWARD, 2);

sleep(2);

AnsiCode::make()->screen(AnsiCode::CLEAR_LINE, 3);
Terminal::make()->screen(Terminal::CLEAR_LINE, 3);

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

sleep(2);

AnsiCode::make()->screen(AnsiCode::SCROLL_DOWN, 3);
Terminal::make()->screen(Terminal::SCROLL_DOWN, 3);

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

sleep(3);

AnsiCode::make()->screen(AnsiCode::CLEAR);
Terminal::make()->screen(Terminal::CLEAR);
}
}
22 changes: 22 additions & 0 deletions examples/Controllers/ProcessController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-12-22
* Time: 11:44
*/

namespace Inhere\Console\Examples\Controllers;

use Inhere\Console\Controller;

/**
* Class ProcessController
* @package Inhere\Console\Examples\Controllers
*/
class ProcessController extends Controller
{
protected static $name = 'process';

protected static $description = 'Some simple process to create and use examples';
}
27 changes: 7 additions & 20 deletions examples/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,34 @@
* User: inhere
* Date: 2016/12/7
* Time: 12:46
*
* @var Inhere\Console\Application $app
*/

use Inhere\Console\BuiltIn\PharController;
use Inhere\Console\Examples\Commands\DemoCommand;
use Inhere\Console\Examples\Controllers\HomeController;
use Inhere\Console\Examples\Commands\TestCommand;
use Inhere\Console\Examples\Controllers\HomeController;
use Inhere\Console\Examples\Controllers\ProcessController;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Utils\ProgressBar;

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

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

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

$app->command('prg', function () {
$i = 0;
$total = 120;
$bar = new ProgressBar();
$bar->start(120);

while ($i <= $total) {
$bar->advance();
usleep(50000);
$i++;
}

$bar->finish();

}, 'a description message');
$app->controller(PharController::class);

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

$app->controller(PharController::class);
$app->controller(ProcessController::class, null, [
'prc'
]);
3 changes: 2 additions & 1 deletion src/Components/Notify/Pending.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
*/
class Pending extends NotifyMessage
{

/** @var int Speed value. allow 1 - 100 */
private $speed = 20;
}
8 changes: 4 additions & 4 deletions src/Components/AnsiCode.php → src/Components/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use Inhere\Console\Utils\Show;

/**
* Class AnsiCode terminal
* Class Terminal - terminal control by ansiCode
* @package Inhere\Console\Components
*
* 2K 清除本行
* \x0D = \r = 13 回车,回到行首
* ESC = \x1B = 27
*/
final class AnsiCode
final class Terminal
{
const BEGIN_CHAR = "\033[";

Expand Down Expand Up @@ -133,8 +133,8 @@ public static function make()
* build ansi code string
*
* ```
* AnsiCode::build(null, 'u'); // "\033[s" Saves the current cursor position
* AnsiCode::build(0); // "\033[0m" Build end char, Resets any ANSI format
* Terminal::build(null, 'u'); // "\033[s" Saves the current cursor position
* Terminal::build(0); // "\033[0m" Build end char, Resets any ANSI format
* ```
*
* @param mixed $format
Expand Down
56 changes: 56 additions & 0 deletions src/Utils/CliUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,62 @@ public static function getTempDir()
return $tmp;
}

/**
* get screen size
*
* ```php
* list($width, $height) = Helper::getScreenSize();
* ```
* @from Yii2
* @param boolean $refresh whether to force checking and not re-use cached size value.
* This is useful to detect changing window size while the application is running but may
* not get up to date values on every terminal.
* @return array|boolean An array of ($width, $height) or false when it was not able to determine size.
*/
public static function getScreenSize($refresh = false)
{
static $size;
if ($size !== null && !$refresh) {
return $size;
}

if (self::bashIsAvailable()) {
// try stty if available
$stty = [];

if (
exec('stty -a 2>&1', $stty) &&
preg_match('/rows\s+(\d+);\s*columns\s+(\d+);/mi', implode(' ', $stty), $matches)
) {
return ($size = [$matches[2], $matches[1]]);
}

// fallback to tput, which may not be updated on terminal resize
if (($width = (int)exec('tput cols 2>&1')) > 0 && ($height = (int)exec('tput lines 2>&1')) > 0) {
return ($size = [$width, $height]);
}

// fallback to ENV variables, which may not be updated on terminal resize
if (($width = (int)getenv('COLUMNS')) > 0 && ($height = (int)getenv('LINES')) > 0) {
return ($size = [$width, $height]);
}
}

if (Helper::isOnWindows()) {
$output = [];
exec('mode con', $output);

if (isset($output[1]) && strpos($output[1], 'CON') !== false) {
return ($size = [
(int)preg_replace('~\D~', '', $output[3]),
(int)preg_replace('~\D~', '', $output[4])
]);
}
}

return ($size = false);
}

/**
* @param string $program
* @return int|string
Expand Down
Loading

0 comments on commit df3d174

Please sign in to comment.