Skip to content

Commit

Permalink
update: add some new methods for cli utils
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 24, 2019
1 parent d00da3b commit 498cd3d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
45 changes: 38 additions & 7 deletions libs/cli-utils/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

use InvalidArgumentException;
use Throwable;
use function array_merge;
use function array_shift;
use function array_values;
use function class_exists;
use function function_exists;
use function getcwd;
use function implode;
use function is_array;
use function is_object;
use function is_string;
Expand Down Expand Up @@ -120,14 +122,18 @@ public function dispatch(bool $exit = true): void
return;
}

$status = 0;
if (!isset($this->commands[$command])) {
$this->displayHelp("The command {$command} not exists!");
return;
}

if (isset($this->opts['h']) || isset($this->opts['help'])) {
$this->displayCommandHelp($command);
return;
}

try {
if (isset($this->commands[$command])) {
$status = $this->runHandler($command, $this->commands[$command]);
} else {
$this->displayHelp("The command {$command} not exists!");
}
$status = $this->runHandler($command, $this->commands[$command]);
} catch (Throwable $e) {
$status = $this->handleException($e);
}
Expand Down Expand Up @@ -230,7 +236,7 @@ public function addCommand(string $command, callable $handler, $config = null):
// save
$this->messages[$command] = $config;
} elseif (is_array($config)) {
$this->messages[$command] = \array_merge(self::COMMAND_CONFIG, $config);
$this->messages[$command] = array_merge(self::COMMAND_CONFIG, $config);
}
}

Expand Down Expand Up @@ -281,6 +287,31 @@ public function displayHelp(string $err = ''): void
exit(0);
}

/**
* @param string $name
*/
public function displayCommandHelp(string $name): void
{
$fullCmd = $this->script . " $name";
$config = $this->messages[$name] ?? [];
$usage = "$fullCmd [args ...] [--opts ...]";

if (!$config) {
$nodes = [
'No description for the command',
"<comment>Usage:</comment> \n $usage"
];
} else {
$nodes = [
ucfirst($config['desc']),
"<comment>Usage:</comment> \n " . ($config['usage'] ?: $usage),
$config['help']
];
}

echo Color::render(implode("\n", $nodes));
}

/**
* @param string|int $name
* @param mixed $default
Expand Down
14 changes: 14 additions & 0 deletions libs/cli-utils/src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ public static function printf(string $format, ...$args): void
echo self::render(sprintf($format, ...$args));
}

/**
* Print colored message to STDOUT
*
* @param string|array $messages
* @param string $style
*/
public function println($messages, string $style = 'info'): void
{
$string = is_array($messages) ? implode("\n", $messages) : (string)$messages;

echo self::render($string, $style);
}

/*******************************************************************************
* color render
******************************************************************************/
Expand Down Expand Up @@ -240,6 +253,7 @@ public static function render(string $text, $style = null): string
// use defined style: 'green'
if (is_string($style)) {
$color = self::STYLES[$style] ?? '0';

// custom style: [self::FG_GREEN, self::BG_WHITE, self::UNDERSCORE]
} elseif (is_array($style)) {
$color = implode(';', $style);
Expand Down
8 changes: 8 additions & 0 deletions libs/cli-utils/src/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function defined;
use function end;
use function explode;
use function file_get_contents;
use function function_exists;
use function implode;
use function is_array;
Expand Down Expand Up @@ -122,6 +123,13 @@ public function highlight(string $source, bool $withLineNumber = false): string
return implode(PHP_EOL, $lines);
}

public function highlightFile(string $file, bool $withLineNumber = false): string
{
$source = file_get_contents($file);

return $this->highlight($source, $withLineNumber);
}

/**
* @param string $source
* @param int $lineNumber
Expand Down

0 comments on commit 498cd3d

Please sign in to comment.