Skip to content

Commit

Permalink
some update, add new screenshots images
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 22, 2017
1 parent df3d174 commit fee4d91
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 24 deletions.
Binary file added docs/screenshots/fmt-help-panel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/fmt-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/fmt-multi-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/fmt-panel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 42 additions & 6 deletions examples/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ protected static function commandAliases()
// now, 'home:i' is equals to 'home:index'
'i' => 'index',
'prg' => 'progress',
'pgb' => 'progressBar',
'pwd' => 'password',
'l' => 'list',
'h' => 'helpPanel',
'hp' => 'helpPanel',
'af' => 'artFont',
'ml' => 'multiList',
'ms' => 'multiSelect',
];
}

Expand All @@ -44,6 +48,11 @@ protected function init()
$this->addAnnotationVar('internalFonts', implode(',', ArtFont::getInternalFonts()));
}

protected function afterExecute()
{
$this->write('after command execute');
}

/**
* this is a command's description message
* the second line text
Expand Down Expand Up @@ -203,7 +212,7 @@ public function progressCommand($input)
'signChar' => $input->getOpt('sign-char', '>'),
]);
} else {
$bar = $this->output->progressTxt($total, 'Doing gggg ...', 'Done');
$bar = $this->output->progressTxt($total, 'Doing go g...', 'Done');
}

$this->write('Progress:');
Expand All @@ -221,7 +230,7 @@ public function progressCommand($input)
* a progress bar example show, by class ProgressBar
* @throws \LogicException
*/
public function prgCommand()
public function progressBarCommand()
{
$i = 0;
$total = 120;
Expand Down Expand Up @@ -278,7 +287,7 @@ public function panelCommand()
];

Show::panel($data, 'panel show', [
'borderChar' => '#'
'borderChar' => '*'
]);
}

Expand All @@ -304,7 +313,7 @@ public function helpPanelCommand()
}

/**
* output format message: aList
* output format message: list
*/
public function listCommand()
{
Expand All @@ -327,6 +336,33 @@ public function listCommand()
Show::aList($commands, 'a List show(Has key)');
}

/**
* output format message: multiList
*/
public function multiListCommand()
{
Show::multiList([
'list0' => [
'value in the list 0',
'key' => 'value in the list 0',
'key1' => 'value1 in the list 0',
'key2' => 'value2 in the list 0',
],
'list1' => [
'key' => 'value in the list 1',
'key1' => 'value1 in the list 1',
'key2' => 'value2 in the list 1',
'value in the list 1',
],
'list2' => [
'key' => 'value in the list 2',
'value in the list 2',
'key1' => 'value1 in the list 2',
'key2' => 'value2 in the list 2',
],
]);
}

/**
* output format message: table
*/
Expand Down Expand Up @@ -499,7 +535,7 @@ public function selectCommand()
/**
* This is a demo for use <magenta>Interact::multiSelect()</magenta> method
*/
public function msCommand()
public function multiSelectCommand()
{
$opts = ['john', 'simon', 'rose', 'tom'];

Expand Down Expand Up @@ -558,7 +594,7 @@ public function limitedAskCommand()
* This is a demo for input password. use: <magenta>Interact::askPassword()</magenta>
* @usage {fullCommand}
*/
public function pwdCommand()
public function passwordCommand()
{
$pwd = $this->askPassword();

Expand Down
50 changes: 50 additions & 0 deletions examples/Controllers/ProcessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Inhere\Console\Examples\Controllers;

use Inhere\Console\Controller;
use Inhere\Console\Utils\ProcessUtil;

/**
* Class ProcessController
Expand All @@ -19,4 +20,53 @@ class ProcessController extends Controller
protected static $name = 'process';

protected static $description = 'Some simple process to create and use examples';

protected static function commandAliases()
{
return [
'cpr' => 'childProcess',
'mpr' => 'multiProcess',
'dr' => 'daemonRun',
];
}

/**
* simple process example for child-process
*/
public function childProcessCommand()
{
$ret = ProcessUtil::create(function ($pid) {
echo "print in process $pid";

sleep(5);
});

if ($ret === false) {
$this->output->liteError('current env is not support process create.');
}
}

/**
* simple process example for daemon run
*/
public function daemonRunCommand()
{
$ret = ProcessUtil::daemonRun(function ($pid){
$this->output->info("will running background by new process: $pid");
});

if ($ret === false) {
$this->output->liteError('current env is not support process create.');
}
}

/**
* simple process example for multi-process
* @options
*
*/
public function multiProcessCommand()
{

}
}
2 changes: 1 addition & 1 deletion examples/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
]);

$app->controller(ProcessController::class, null, [
'prc'
'aliases' => 'prc'
]);
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ protected function dispatch($name)

// command not found
if (true !== self::fire(self::ON_NOT_FOUND, [$this])) {
$this->output->liteError("The console command '{$name}' not exists!");
$this->output->liteError("The command '{$name}' is not exists in the console application!");

$commands = array_merge($this->getControllerNames(), $this->getCommandNames());

Expand Down
12 changes: 6 additions & 6 deletions src/Base/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function showHelpInfo($quit = true, string $command = null)
$sep = $this->delimiter;

$this->output->helpPanel([
'usage' => "$script {command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
'usage' => "$script <info>{command}</info> [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
'example' => [
"$script test (run a independent command)",
"$script home{$sep}index (run a command of the group)",
Expand Down Expand Up @@ -397,7 +397,7 @@ public function showCommandList($quit = true)
$desPlaceholder = 'No description of the command';

// all console controllers
$controllerArr[] = PHP_EOL . '- <cyan>Group Commands</cyan>';
$controllerArr[] = PHP_EOL . '- <bold>Group Commands</bold>';
$controllers = $this->controllers;
ksort($controllers);

Expand All @@ -414,9 +414,9 @@ public function showCommandList($quit = true)
$controllerArr[] = '... No register any group command(controller)';
}

// all independent commands
// all independent commands, Independent, Single, Alone
$commands = $this->commands;
$commandArr[] = PHP_EOL . '- <cyan>Independent Commands</cyan>';
$commandArr[] = PHP_EOL . '- <bold>Alone Commands</bold>';
ksort($commands);

foreach ($commands as $name => $command) {
Expand Down Expand Up @@ -448,10 +448,10 @@ public function showCommandList($quit = true)
ksort($internalCommands);

// built in options
$internalOptions = FormatUtil::commandOptions(self::$internalOptions);
$internalOptions = FormatUtil::arrayOptions(self::$internalOptions);

$this->output->mList([
'Usage:' => "$script {command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
'Usage:' => "$script <info>{command}</info> [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
'Options:' => $internalOptions,
'Internal Commands:' => $internalCommands,
'Available Commands:' => array_merge($controllerArr, $commandArr),
Expand Down
8 changes: 5 additions & 3 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ final public function showCommandList()

if ($this->standAlone) {
$name = $sName . ' ';
$usage = "$script <info>{command}</info> [arguments] [options]";
$usage = "$script <info>{command}</info> [arguments ...] [options ...]";
} else {
$name = $sName . $this->delimiter;
$usage = "$script {$name}<info>{command}</info> [arguments] [options]";
$usage = "$script {$name}<info>{command}</info> [arguments ...] [options ...]";
}

$this->output->startBuffer();
Expand All @@ -220,9 +220,11 @@ final public function showCommandList()
'Usage:' => $usage,
//'Group Name:' => "<info>$sName</info>",
'Options:' => [
'-h,--help' => 'Show help of the command group or specified command action',
'-h, --help' => 'Show help of the command group or specified command action',
],
'Commands:' => $commands,
], [
'sepChar' => ' ',
]);

$this->write(sprintf(
Expand Down
8 changes: 8 additions & 0 deletions src/Traits/FormatOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
* @method int liteWarning($messages, $quit = false)
* @method int liteDanger($messages, $quit = false)
* @method int liteError($messages, $quit = false)
*
* @method padding(array $data, string $title = null, array $opts = [])
*
* @method spinner($msg = '', $ended = false)
* @method loading($msg = 'Loading ', $ended = false)
* @method pending($msg = 'Pending ', $ended = false)
*
* @method \Generator counterTxt($msg = 'Pending ', $ended = false)
*/
trait FormatOutputAwareTrait
{
Expand Down
45 changes: 44 additions & 1 deletion src/Utils/FormatUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,47 @@ public static function camelCase($name)
return $name;
}

/**
* @param string $string
* @param int $indent
* @param string $indentChar
* @return string
*/
public static function applyIndent(string $string, int $indent = 2, string $indentChar = ' ')
{
if (!$string || $indent <= 0) {
return $string;
}

$new = '';
$list = explode("\n", $string);
$indentStr = str_repeat($indentChar ?: ' ', $indent);

foreach ($list as $value) {
$new .= $indentStr . trim($value) . "\n";
}

return $new;
}

/**
* @param string $optsStr
*/
public static function annotationOptions(string $optsStr)
{

}

public static function annotationArguments(string $optsStr)
{

}

/**
* @param array $options
* @return array
*/
public static function commandOptions(array $options)
public static function arrayOptions(array $options)
{
// e.g '-h, --help'
$hasShort = (bool)strpos(implode(array_keys($options), ''), ',');
Expand All @@ -47,8 +83,15 @@ public static function commandOptions(array $options)

$formatted = [];
foreach ($options as $name => $des) {
if (!$name = trim($name, ', ')) {
continue;
}

if (!strpos($name, ',')) {
// padding length equals to '-h, '
$name = ' ' . $name;
} else {
$name = str_replace([' ', ','], ['', ', '], $name);
}

$formatted[$name] = $des;
Expand Down
10 changes: 5 additions & 5 deletions src/Utils/ProcessUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ public static function forks(int $number, callable $onStart = null, callable $on

/**
* @see ProcessUtil::fork()
* @param int $id
* @param callable|null $onStart
* @param callable|null $onError
* @param int $id
* @return array|false
*/
public static function create($id = 0, callable $onStart = null, callable $onError = null)
public static function create(callable $onStart = null, callable $onError = null, $id = 0)
{
return self::fork($id, $onStart, $onError);
return self::fork($onStart, $onError, $id);
}

/**
* fork/create a child process.
* @param callable|null $onStart Will running on the child process start.
* @param int $id The process index number. will use `forks()`
* @param callable|null $onError
* @param int $id The process index number. will use `forks()`
* @return array|false
*/
public static function fork(callable $onStart = null, callable $onError = null, $id = 0)
Expand All @@ -154,7 +154,7 @@ public static function fork(callable $onStart = null, callable $onError = null,
$pid = getmypid();

if ($onStart) {
$onStart($id, $pid);
$onStart($pid, $id);
}
} else {
if ($onError) {
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public static function mList(array $data, array $opts = [])
$buffer[] = self::aList($list, $title, $opts);
}

self::write($buffer);
self::write(implode("\n", $buffer), false);
}

/**
Expand Down

0 comments on commit fee4d91

Please sign in to comment.