基础代码

This commit is contained in:
2021-02-26 22:23:13 +08:00
parent 7884df52f0
commit a719feebba
2585 changed files with 328263 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
<?php
/**
* Sample invocations:
*
* # php example_args.php -vC ./ --version
* {"verbose":true,"cache":".\/","version":true}
* # php example_args.php -vC --version
* PHP Warning: [cli\Arguments] no value given for -C
* # php example_args.php -vC multi word --version
* {"verbose":true,"cache":"multi word","version":true}
*
*/
require 'common.php';
$strict = in_array('--strict', $_SERVER['argv']);
$arguments = new \cli\Arguments(compact('strict'));
$arguments->addFlag(array('verbose', 'v'), 'Turn on verbose output');
$arguments->addFlag('version', 'Display the version');
$arguments->addFlag(array('quiet', 'q'), 'Disable all output');
$arguments->addFlag(array('help', 'h'), 'Show this help screen');
$arguments->addOption(array('cache', 'C'), array(
'default' => getcwd(),
'description' => 'Set the cache directory'));
$arguments->addOption(array('name', 'n'), array(
'default' => 'James',
'description' => 'Set a name with a really long description and a default so we can see what line wrapping looks like which is probably a goo idea'));
$arguments->parse();
if ($arguments['help']) {
echo $arguments->getHelpScreen();
echo "\n\n";
}
echo $arguments->asJSON() . "\n";
+32
View File
@@ -0,0 +1,32 @@
<?php
// Samples. Lines marked with * should be colored in output
// php examples/colors.php
// * All output is run through \cli\Colors::colorize before display
// * All output is run through \cli\Colors::colorize before display
// * All output is run through \cli\Colors::colorize before display
// * All output is run through \cli\Colors::colorize before display
// All output is run through \cli\Colors::colorize before display
// * All output is run through \cli\Colors::colorize before display
// php examples/colors.php | cat
// All output is run through \cli\Colors::colorize before display
// * All output is run through \cli\Colors::colorize before display
// All output is run through \cli\Colors::colorize before display
// * All output is run through \cli\Colors::colorize before display
// All output is run through \cli\Colors::colorize before display
// All output is run through \cli\Colors::colorize before display
require_once 'common.php';
\cli\line(' %C%5All output is run through %Y%6\cli\Colors::colorize%C%5 before display%n');
echo \cli\Colors::colorize(' %C%5All output is run through %Y%6\cli\Colors::colorize%C%5 before display%n', true) . "\n";
echo \cli\Colors::colorize(' %C%5All output is run through %Y%6\cli\Colors::colorize%C%5 before display%n') . "\n";
\cli\Colors::enable(); // Forcefully enable
\cli\line(' %C%5All output is run through %Y%6\cli\Colors::colorize%C%5 before display%n');
\cli\Colors::disable(); // Disable forcefully!
\cli\line(' %C%5All output is run through %Y%6\cli\Colors::colorize%C%5 before display%n', true);
\cli\Colors::enable(false); // Enable, but not forcefully
\cli\line(' %C%5All output is run through %Y%6\cli\Colors::colorize%C%5 before display%n');
+37
View File
@@ -0,0 +1,37 @@
<?php
if (php_sapi_name() != 'cli') {
die('Must run from command line');
}
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
ini_set('log_errors', 0);
ini_set('html_errors', 0);
foreach(array(__DIR__ . '/../vendor', __DIR__ . '/../../../../vendor') as $vendorDir) {
if(is_dir($vendorDir)) {
require_once $vendorDir . '/autoload.php';
break;
}
}
function test_notify(cli\Notify $notify, $cycle = 1000000, $sleep = null) {
for ($i = 0; $i < $cycle; $i++) {
$notify->tick();
if ($sleep) usleep($sleep);
}
$notify->finish();
}
function test_notify_msg(cli\Notify $notify, $cycle = 1000000, $sleep = null) {
$notify->display();
for ($i = 0; $i < $cycle; $i++) {
// Sleep before tick to simulate time-intensive work and give time
// for the initial message to display before it is changed
if ($sleep) usleep($sleep);
$msg = sprintf(' Finished step %d', $i + 1);
$notify->tick(1, $msg);
}
$notify->finish();
}
+24
View File
@@ -0,0 +1,24 @@
<?php
require_once 'common.php';
$menu = array(
'output' => 'Output Examples',
'notify' => 'cli\Notify Examples',
'progress' => 'cli\Progress Examples',
'table' => 'cli\Table Example',
'colors' => 'cli\Colors example',
'quit' => 'Quit',
);
while (true) {
$choice = \cli\menu($menu, null, 'Choose an example');
\cli\line();
if ($choice == 'quit') {
break;
}
include "${choice}.php";
\cli\line();
}
+12
View File
@@ -0,0 +1,12 @@
<?php
require_once 'common.php';
\cli\line("========\nDots\n");
test_notify(new \cli\notify\Dots(' \cli\notify\Dots cycles through a set number of dots'));
test_notify(new \cli\notify\Dots(' You can disable the delay if ticks take longer than a few milliseconds', 5, 0), 10, 100000);
\cli\line("\n========\nSpinner\n");
test_notify(new \cli\notify\Spinner(' \cli\notify\Spinner cycles through a set of characters to emulate a spinner'));
+18
View File
@@ -0,0 +1,18 @@
<?php
require_once 'common.php';
\cli\out(" \\cli\\out sends output to STDOUT\n");
\cli\out(" It does not automatically append a new line\n");
\cli\out(" It does accept any number of %s which are then %s to %s for formatting\n", 'arguments', 'passed', 'sprintf');
\cli\out(" Alternatively, {:a} can use an {:b} as the second argument.\n\n", array('a' => 'you', 'b' => 'array'));
\cli\err(' \cli\err sends output to STDERR');
\cli\err(' It does automatically append a new line');
\cli\err(' It does accept any number of %s which are then %s to %s for formatting', 'arguments', 'passed', 'sprintf');
\cli\err(" Alternatively, {:a} can use an {:b} as the second argument.\n", array('a' => 'you', 'b' => 'array'));
\cli\line(' \cli\line forwards to \cli\out for output');
\cli\line(' It does automatically append a new line');
\cli\line(' It does accept any number of %s which are then %s to %s for formatting', 'arguments', 'passed', 'sprintf');
\cli\line(" Alternatively, {:a} can use an {:b} as the second argument.\n", array('a' => 'you', 'b' => 'array'));
+7
View File
@@ -0,0 +1,7 @@
<?php
require_once 'common.php';
test_notify(new \cli\progress\Bar(' \cli\progress\Bar displays a progress bar', 1000000));
test_notify(new \cli\progress\Bar(' It sizes itself dynamically', 1000000));
test_notify_msg(new \cli\progress\Bar(' It can even change its message', 5), 5, 1000000);
+42
View File
@@ -0,0 +1,42 @@
<?php
require_once 'common.php';
/*
* Please notice that the data has to be an 0-based array,
* not an associative one if you intend to work with custom
* column widths.
*/
$headers = array('First Name', 'Last Name', 'City', 'State');
$data = array(
array('Maryam', 'Elliott', 'Elizabeth City', 'SD'),
array('Jerry', 'Washington', 'Bessemer', 'ME'),
array('Allegra', 'Hopkins', 'Altoona', 'ME'),
array('Audrey', 'Oneil', 'Dalton', 'SK'),
array('Ruth', 'Mcpherson', 'San Francisco', 'ID'),
array('Odessa', 'Tate', 'Chattanooga', 'FL'),
array('Violet', 'Nielsen', 'Valdosta', 'AB'),
array('Summer', 'Rollins', 'Revere', 'SK'),
array('Mufutau', 'Bowers', 'Scottsbluff', 'WI'),
array('Grace', 'Rosario', 'Garden Grove', 'KY'),
array('Amanda', 'Berry', 'La Habra', 'AZ'),
array('Cassady', 'York', 'Fulton', 'BC'),
array('Heather', 'Terrell', 'Statesboro', 'SC'),
array('Dominic', 'Jimenez', 'West Valley City', 'ME'),
array('Rhonda', 'Potter', 'Racine', 'BC'),
array('Nathan', 'Velazquez', 'Cedarburg', 'BC'),
array('Richard', 'Fletcher', 'Corpus Christi', 'BC'),
array('Cheyenne', 'Rios', 'Broken Arrow', 'VA'),
array('Velma', 'Clemons', 'Helena', 'IL'),
array('Samuel', 'Berry', 'Lawrenceville', 'NU'),
array('Marcia', 'Swanson', 'Fontana', 'QC'),
array('Zachary', 'Silva', 'Port Washington', 'MB'),
array('Hilary', 'Chambers', 'Suffolk', 'HI'),
array('Idola', 'Carroll', 'West Sacramento', 'QC'),
array('Kirestin', 'Stephens', 'Fitchburg', 'AB'),
);
$table = new \cli\Table();
$table->setHeaders($headers);
$table->setRows($data);
$table->setRenderer(new \cli\table\Ascii([10, 10, 20, 5]));
$table->display();
+72
View File
@@ -0,0 +1,72 @@
<?php
require_once 'common.php';
$data = array(
'Test' => array(
'Something Cool' => array(
'This is a 3rd layer',
),
'This is a 2nd layer',
),
'Other test' => array(
'This is awesome' => array(
'This is also cool',
'This is even cooler',
'Wow like what is this' => array(
'Awesome eh?',
'Totally' => array(
'Yep!'
),
),
),
),
);
printf("ASCII:\n");
/**
* ASCII should look something like this:
*
* -Test
* |\-Something Cool
* ||\-This is a 3rd layer
* |\-This is a 2nd layer
* \-Other test
* \-This is awesome
* \-This is also cool
* \-This is even cooler
* \-Wow like what is this
* \-Awesome eh?
* \-Totally
* \-Yep!
*/
$tree = new \cli\Tree;
$tree->setData($data);
$tree->setRenderer(new \cli\tree\Ascii);
$tree->display();
printf("\nMarkdown:\n");
/**
* Markdown looks like this:
*
* - Test
* - Something Cool
* - This is a 3rd layer
* - This is a 2nd layer
* - Other test
* - This is awesome
* - This is also cool
* - This is even cooler
* - Wow like what is this
* - Awesome eh?
* - Totally
* - Yep!
*/
$tree = new \cli\Tree;
$tree->setData($data);
$tree->setRenderer(new \cli\tree\Markdown(4));
$tree->display();