Skip to content

Commit 25c3f1a

Browse files
committed
docs: improve readability and organize
1 parent c71ef02 commit 25c3f1a

File tree

1 file changed

+94
-48
lines changed

1 file changed

+94
-48
lines changed

README.md

Lines changed: 94 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,9 @@ Framework agnostic Command Line Interface utilities and helpers for PHP. Build C
1717

1818
#### What's included
1919

20-
**Core**
20+
**Core:** [Argv parser](#as-argv-parser) · [Cli application](#as-console-app)
2121

22-
- [Argv parser](#as-argv-parser)
23-
- [Cli application](#as-console-app)
24-
25-
**IO**
26-
27-
- [Colorizer](#color)
28-
- [Cursor manipulator](#cursor)
29-
- [Stream writer](#writer)
30-
- [Stream reader](#reader)
22+
**IO:** [Colorizer](#color) · [Cursor manipulator](#cursor) · [Stream writer](#writer) · [Stream reader](#reader)
3123

3224
## Installation
3325
```bash
@@ -105,7 +97,7 @@ For above example, the output would be:
10597

10698
Definitely check [adhocore/phint](https://github.com/adhocore/phint) - a real world console application made using `adhocore/cli`.
10799

108-
We simulate a `git` app with limited functionality of `add`, and `checkout`.
100+
Here we simulate a `git` app with limited functionality of `add`, and `checkout`.
109101
You will see how intuitive, fluent and cheese building a console app is!
110102

111103
#### Git app
@@ -157,7 +149,6 @@ $app->handle(['git', 'co', '-b', 'master-2', '-f']);
157149
Instead of inline commands/actions, we define and add our own commands (having `interact()` and `execute()`) to the app:
158150

159151
```php
160-
161152
class InitCommand extends Ahc\Cli\Input\Command
162153
{
163154
public function __construct()
@@ -240,29 +231,42 @@ You can perform user interaction like printing colored output, reading user inpu
240231

241232
```php
242233
$interactor = new Ahc\Cli\IO\Interactor;
243-
// For mocking io: `$interactor = new Ahc\Cli\IO\Interactor($inputPath, $outputPath)`
244234

235+
// For mocking io:
236+
$interactor = new Ahc\Cli\IO\Interactor($inputPath, $outputPath);
237+
```
238+
239+
#### Confirm
240+
```php
245241
$confirm = $interactor->confirm('Are you happy?', 'n'); // Default: n (no)
246242
$confirm // is a boolean
247243
? $interactor->greenBold('You are happy :)', true) // Output green bold text
248244
: $interactor->redBold('You are sad :(', true); // Output red bold text
245+
```
249246

250-
// Single choice
247+
#### Single choice
248+
```php
251249
$fruits = ['a' => 'apple', 'b' => 'banana'];
252250
$choice = $interactor->choice('Select a fruit', $fruits, 'b');
253251
$interactor->greenBold("You selected: {$fruits[$choice]}", true);
252+
```
254253

255-
// Multiple choices
254+
#### Multiple choices
255+
```php
256256
$fruits = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry'];
257257
$choices = $interactor->choices('Select fruit(s)', $fruits, ['b', 'c']);
258258
$choices = \array_map(function ($c) use ($fruits) { return $fruits[$c]; }, $choices);
259259
$interactor->greenBold('You selected: ' . implode(', ', $choices), true);
260+
```
260261

261-
// Promt free input
262+
#### Prompt free input
263+
```php
262264
$any = $interactor->prompt('Anything', rand(1, 100)); // Random default
263265
$interactor->greenBold("Anything is: $any", true);
266+
```
264267

265-
// Prompting with validation
268+
#### Prompt with validation
269+
```php
266270
$nameValidator = function ($value) {
267271
if (\strlen($value) < 5) {
268272
throw new \Exception('Name should be atleast 5 chars');
@@ -278,24 +282,30 @@ $interactor->greenBold("The name is: $name", true);
278282

279283
![Interactive Preview](https://i.imgur.com/qYBNd29.gif "Interactive Preview")
280284

281-
### IO Components
285+
## IO Components
282286

283287
The interactor is composed of `Ahc\Cli\Input\Reader` and `Ahc\Cli\Output\Writer` while the `Writer` itself is composed of `Ahc\Cli\Output\Color`. All these components can be used standalone.
284288

285-
#### Color
289+
### Color
286290

287291
Color looks cool!
288292

289293
```php
290294
$color = new Ahc\Cli\Output\Color;
295+
```
291296

297+
#### Simple usage
298+
299+
```php
292300
echo $color->warn('This is warning');
293301
echo $color->info('This is info');
294302
echo $color->error('This is error');
295303
echo $color->comment('This is comment');
296304
echo $color->ok('This is ok msg');
305+
```
297306

298-
// Custom style:
307+
#### Custom style
308+
```php
299309
Ahc\Cli\Output\Color::style('mystyle', [
300310
'bg' => Ahc\Cli\Output\Color::CYAN,
301311
'fg' => Ahc\Cli\Output\Color::WHITE,
@@ -305,65 +315,96 @@ Ahc\Cli\Output\Color::style('mystyle', [
305315
echo $color->mystyle('My text');
306316
```
307317

308-
#### Cursor
318+
### Cursor
309319

310320
Move cursor around, erase line up or down, clear screen.
311321

312322
```php
313323
$cursor = new Ahc\Cli\Output\Cursor;
314324

315-
echo $cursor->up(1) . $cursor->down(2)
316-
. $cursor->right(3) . $cursor->left(4)
317-
. $cursor->next(0) . $cursor->prev(2);
318-
. $cursor->eraseLine() . $cursor->clear()
319-
. $cursor->clearUp() . $cursor->clearDown()
325+
echo $cursor->up(1)
326+
. $cursor->down(2)
327+
. $cursor->right(3)
328+
. $cursor->left(4)
329+
. $cursor->next(0)
330+
. $cursor->prev(2);
331+
. $cursor->eraseLine()
332+
. $cursor->clear()
333+
. $cursor->clearUp()
334+
. $cursor->clearDown()
320335
. $cursor->moveTo(5, 8); // x, y
321336
```
322337

323-
#### Writer
338+
### Writer
324339

325340
Write anything in style.
326341

327342
```php
328343
$writer = new Ahc\Cli\Output\Writer;
329344

330-
// Output formatting: You can call methods composed of:
331-
// ('<colorName>', 'bold', 'bg', 'fg', 'warn', 'info', 'error', 'ok', 'comment')
332-
// ... in any order (eg: bgRedFgBlaock, boldRed, greenBold, commentBgPurple and so on ...)
345+
// All writes are forwarded to STDOUT
346+
// But if you specify error, then to STDERR
347+
$writer->errorBold('This is error');
348+
```
349+
350+
#### Output formatting
351+
352+
You can call methods composed of any combinations:
353+
`'<colorName>', 'bold', 'bg', 'fg', 'warn', 'info', 'error', 'ok', 'comment'`
354+
... in any order (eg: `bgRedFgBlaock`, `boldRed`, `greenBold`, `commentBgPurple` and so on ...)
355+
356+
```php
333357
$writer->bold->green->write('It is bold green');
334358
$writer->boldGreen('It is bold green'); // Same as above
335359
$writer->comment('This is grayish comment', true); // True indicates append EOL character.
336360
$writer->bgPurpleBold('This is white on purple background');
361+
```
362+
363+
#### Free style
364+
365+
Many colors with one single call: wrap text with tags `<method>` and `</end>`
366+
For NL/EOL just use `<eol>` or `</eol>` or `<eol/>`.
337367

338-
// Many colors with one single call: wrap text with tags `<method>` and `</end>`
339-
// For NL/EOL just use `<eol>` or `</eol>` or `<eol/>`
368+
Great for writing long colorful texts for example command usage info.
369+
370+
```php
340371
$writer->colors('<red>This is red</end><eol><bgGreen>This has bg Green</end>');
372+
```
341373

342-
// All writes are forwarded to STDOUT
343-
// But if you specify error, then to STDERR
344-
$writer->errorBold('This is error');
374+
#### Raw output
345375

346-
// Write a normal raw text.
376+
```php
347377
$writer->raw('Enter name: ');
378+
```
379+
380+
#### Tables
348381

349-
// Creating tables: just pass array of assoc arrays.
350-
// The keys of first array will be taken as heading.
351-
// Heading is auto inflected to human readable capitalized words (ucwords).
382+
Just pass array of assoc arrays. The keys of first array will be taken as heading.
383+
Heading is auto inflected to human readable capitalized words (ucwords).
384+
385+
```php
352386
$writer->table([
353387
['a' => 'apple', 'b-c' => 'ball', 'c_d' => 'cat'],
354388
['a' => 'applet', 'b-c' => 'bee', 'c_d' => 'cute'],
355389
]);
390+
```
391+
392+
Gives something like:
393+
394+
```
395+
+--------+------+------+
396+
| A | B C | C D |
397+
+--------+------+------+
398+
| apple | ball | cat |
399+
| applet | bee | cute |
400+
+--------+------+------+
401+
```
402+
403+
> Designing table look and feel
356404
357-
// Gives something like:
358-
//
359-
// +--------+------+------+
360-
// | A | B C | C D |
361-
// +--------+------+------+
362-
// | apple | ball | cat |
363-
// | applet | bee | cute |
364-
// +--------+------+------+
405+
Just pass 2nd param `$styles`:
365406

366-
// Designing table look and feel: just pass 2nd param $styles.
407+
```php
367408
$writer->table([
368409
['a' => 'apple', 'b-c' => 'ball', 'c_d' => 'cat'],
369410
['a' => 'applet', 'b-c' => 'bee', 'c_d' => 'cute'],
@@ -373,6 +414,7 @@ $writer->table([
373414
'odd' => 'bold', // For the odd rows (1st row is odd, then 3, 5 etc)
374415
'even' => 'comment', // For the even rows (2nd row is even, then 4, 6 etc)
375416
]);
417+
376418
// 'head', 'odd', 'even' are all the styles for now
377419
// In future we may support styling a column by its name!
378420
```
@@ -406,3 +448,7 @@ Whenever an exception is caught by `Application::handle()`, it will show a beaut
406448
- [adhocore/phalcon-ext](https://github.com/adhocore/phalcon-ext) Phalcon extension using `adhocore/cli`
407449
- [adhocore/phint](https://github.com/adhocore/phint) PHP project scaffolding app using `adhocore/cli`
408450
- [adhocore/type-hinter](https://github.com/adhocore/php-type-hinter) Auto PHP7 typehinter tool using `adhocore/cli`
451+
452+
## LICENSE
453+
454+
> &copy; [MIT](./LICENSE) | 2018, Jitendra Adhikari

0 commit comments

Comments
 (0)