Skip to content

Commit

Permalink
prof: add more str,json... util commands
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 1, 2021
1 parent 1721070 commit c1c2573
Show file tree
Hide file tree
Showing 22 changed files with 1,243 additions and 113 deletions.
9 changes: 7 additions & 2 deletions app/Console/Command/FindCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ public static function aliases(): array

/**
*
* @param Input $input
* @param Output $output
* @param Input $input
* @param Output $output
*
* @return int
*/
protected function execute(Input $input, Output $output): int
{
// $f = new FileFinder();
// $f->getIterator();

$output->info('recommended install fzf for search file');
return 0;
}
Expand Down
97 changes: 97 additions & 0 deletions app/Console/Component/ContentsTryReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\Component;

use Inhere\Kite\Kite;
use Toolkit\Cli\Cli;
use Toolkit\FsUtil\File;
use Toolkit\Stdlib\Obj\AbstractObj;
use function is_file;
use function substr;

/**
* class ContentsTryReader
*/
class ContentsTryReader extends AbstractObj
{
public const TYPE_CLIPBOARD = 'clipboard';

public const TYPE_FILE = 'file';
public const TYPE_STDIN = 'stdin';
public const TYPE_STRING = 'string';

/**
* @var string
*/
protected string $srcType = self::TYPE_STRING;

/**
* try read contents
*
* - input empty or '@i' or '@stdin' - will read from STDIN
* - input '@c' or '@cb' or '@clipboard' - will read from Clipboard
* - input '@l' or '@load' - will read from loaded file
* - input '@FILEPATH' or FILEPATH - will read from the filepath.
*
* @param string $source the input text
* @param array{print: bool, loadedFile: string} $opts
*
* @return string
*/
public function read(string $source, array $opts = []): string
{
$print = $opts['print'] ?? false;
$lFile = $opts['loadedFile'] ?? '';

$str = $source;
if (!$source) {
$this->srcType = self::TYPE_STDIN;
$print && Cli::info('try read contents from STDIN');
$str = Kite::cliApp()->getInput()->readAll();

// is one line text
} elseif (!str_contains($source, "\n")) {
if (str_starts_with($source, '@')) {
if ($source === '@c' || $source === '@cb' || $source === '@clipboard') {
$this->srcType = self::TYPE_CLIPBOARD;
$print && Cli::info('try read contents from Clipboard');
$str = Clipboard::new()->read();
} elseif ($source === '@i' || $source === '@stdin') {
$this->srcType = self::TYPE_STDIN;
$print && Cli::info('try read contents from STDIN');
$str = Kite::cliApp()->getInput()->readAll();
// $str = File::streamReadAll(STDIN);
// $str = File::readAll('php://stdin');
// vdump($str);
// Cli::info('try read contents from STDOUT'); // error
// $str = Kite::cliApp()->getOutput()->readAll();
} elseif (($source === '@l' || $source === '@load') && ($lFile && is_file($lFile))) {
$this->srcType = self::TYPE_FILE;
$print && Cli::info('try read contents from file: ' . $lFile);
$str = File::readAll($lFile);
} else {
$filepath = substr($source, 1);
if (is_file($filepath)) {
$this->srcType = self::TYPE_FILE;
$print && Cli::info('try read contents from file: ' . $filepath);
$str = File::readAll($filepath);
}
}
} elseif (is_file($source)) {
$this->srcType = self::TYPE_FILE;
$print && Cli::info('try read contents from file: ' . $source);
$str = File::readAll($source);
}
}

return $str;
}

/**
* @return string
*/
public function getSrcType(): string
{
return $this->srcType;
}
}
54 changes: 31 additions & 23 deletions app/Console/Controller/ConvertController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Lib\Convert\JavaProperties;
use Inhere\Kite\Lib\Convert\SQLMarkdown;
use Inhere\Kite\Lib\Parser\DBCreateSQL;
use InvalidArgumentException;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
Expand Down Expand Up @@ -62,53 +65,58 @@ protected static function commandAliases(): array
];
}

/**
* convert input string to PHP array.
*
* @options
* --cb bool;read source code from clipboard
* -f, --file The source code file
* -s, --sep The sep char for split.
* -o, --output The output target. default is stdout.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function str2arrCommand(FlagsParser $fs, Output $output): void
{
$output->success('Complete');
}

/**
* convert markdown table to create mysql table SQL
*
* @options
* --cb bool;read source code from clipboard
* -f,--file The source code file
* -s,--source string;The source code for convert. allow: FILEPATH, @clipboard;true
* -o,--output The output target. default is stdout.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function md2sqlCommand(FlagsParser $fs, Output $output): void
{
$output->success('Complete');
$source = $fs->getOpt('source');
$source = AppHelper::tryReadContents($source);

if (!$source) {
throw new InvalidArgumentException('empty source code for convert');
}

$obj = new SQLMarkdown();
$sql = $obj->toCreateSQL($source);

$output->writeRaw($sql);
}

/**
* convert create mysql table SQL to markdown table
*
* @options
* --cb bool;read input from clipboard
* -f,--file The source markdown code
* -s,--source string;The source code for convert. allow: FILEPATH, @clipboard;true
* -o,--output The output target. default is stdout.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function sql2mdCommand(FlagsParser $fs, Output $output): void
{
$output->success('Complete');
$source = $fs->getOpt('source');
$source = AppHelper::tryReadContents($source);

if (!$source) {
throw new InvalidArgumentException('empty source code for convert');
}

// $obj = new SQLMarkdown();
// $sql = $obj->toMdTable($source);

$p = new DBCreateSQL();
$p->parse($source);

$sql = $p->genMDTable();
$output->writeRaw($sql);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions app/Console/Controller/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ public function lnCommand(Output $output): void
// $output->success('hello');
}

/**
* create ln
*
* @options
* -s, --src The server address. e.g 127.0.0.1:5577
* -d, --dst The server host address. e.g 127.0.0.1
*
* @param Output $output
*/
public function findCommand(Output $output): void
{
// ln -s "$PWD"/bin/htu /usr/local/bin/htu

Show::success('ddd');
// $output->success('hello');
}

/**
* use vim edit an input file
*
Expand Down
65 changes: 62 additions & 3 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
use Inhere\Kite\Kite;
use InvalidArgumentException;
use JsonException;
use Toolkit\Cli\App;
use Toolkit\FsUtil\File;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Arr;
use Toolkit\Stdlib\Helper\JsonHelper;
use Toolkit\Stdlib\Str;
use function explode;
use function is_file;
use function is_scalar;
use function json_decode;
use function preg_match;
use function preg_replace;
use function str_contains;
use function trim;
use function vdump;

/**
* Class DemoController
Expand Down Expand Up @@ -91,7 +97,9 @@ private function loadDumpfileJSON(): void
*/
private function autoReadJSON(string $source): void
{
$this->json = AppHelper::tryReadContents($source, $this->dumpfile);
$this->json = AppHelper::tryReadContents($source, [
'loadedFile' => $this->dumpfile,
]);
if (!$this->json) {
throw new InvalidArgumentException('the source json data is empty');
}
Expand Down Expand Up @@ -193,7 +201,9 @@ public function searchCommand(FlagsParser $fs, Output $output): void
public function prettyCommand(FlagsParser $fs, Output $output): void
{
$json = $fs->getArg('json');
$json = AppHelper::tryReadContents($json, $this->dumpfile);
$json = AppHelper::tryReadContents($json, [
'loadedFile' => $this->dumpfile,
]);

if (!$json) {
throw new InvalidArgumentException('please input json text for pretty');
Expand All @@ -205,6 +215,55 @@ public function prettyCommand(FlagsParser $fs, Output $output): void
$output->write($this->jsonRender()->render($json));
}

/**
* collect field and comments from JSON5 contents
*
* @arguments
* json5 The json text line. if empty will try read text from clipboard
*
*/
public function fieldsCommand(FlagsParser $fs, Output $output): void
{
$json = $fs->getArg('json5');
$json = AppHelper::tryReadContents($json, [
'loadedFile' => $this->dumpfile,
]);

if (!$json) {
throw new InvalidArgumentException('please input json(5) text for handle');
}

$fields = [];
foreach (explode("\n", $json) as $line) {
if (!str_contains($line, ':') || !str_contains($line, '//')) {
continue;
}

// is comments line
$trimmed = trim($line);
if (str_starts_with($trimmed, '#') || str_starts_with($trimmed, '//')) {
continue;
}

if (str_contains($trimmed, '://')) {
$trimmed = preg_replace('/https?:\/\//', 'XX', $trimmed);
if (!str_contains($trimmed, '//')) {
continue;
}
}

[$jsonLine, $comments] = Str::explode($trimmed, '//', 2);
if (!preg_match('/[a-zA-Z][\w_]+/', $jsonLine, $matches)) {
continue;
}

// vdump($matches);
$fields[$matches[0]] = $comments;
}

$output->aList($fields);
}

/**
* multi line JSON logs.
*/
Expand Down
17 changes: 17 additions & 0 deletions app/Console/Controller/PhpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ protected static function commandAliases(): array
];
}

/**
* convert input string to PHP array.
*
* @options
* --cb bool;read source code from clipboard
* -f, --file The source code file
* -s, --sep The sep char for split.
* -o, --output The output target. default is stdout.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function str2arrCommand(FlagsParser $fs, Output $output): void
{
$output->success('Complete');
}

/**
* convert create mysql table SQL to PHP class
*
Expand Down
Loading

0 comments on commit c1c2573

Please sign in to comment.