Skip to content

Commit

Permalink
update: update text parser some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 6, 2021
1 parent 8ca2262 commit 0eb9e40
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 107 deletions.
79 changes: 61 additions & 18 deletions app/Console/Controller/ConvertController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Lib\Convert\JavaProperties;
use Inhere\Kite\Lib\Parser\DBTable;
use Inhere\Kite\Lib\Parser\Text\TextParser;
use Inhere\Kite\Lib\Stream\ListStream;
use InvalidArgumentException;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
use Toolkit\FsUtil\File;
use Toolkit\PFlag\FlagsParser;
use function array_pad;
use function array_shift;
use function base_convert;
use function date;
use function file_get_contents;
use function implode;
use function is_file;
use function strlen;
use function substr;
use function trim;
use function vdump;

/**
* Class ConvertController
Expand Down Expand Up @@ -112,6 +119,58 @@ public function sql2mdCommand(FlagsParser $fs, Output $output): void
// $output->println($cm->parse($md));
}

/**
* convert create mysql table SQL to markdown table
*
* @arguments
* type The target text doc type, allow: raw, md-table,
*
* @options
* -s,--source string;The source code for convert. allow: FILEPATH, @clipboard;true
* -o,--output The output target. default is stdout.
* --item-sep The item sep char. default is NL.
* --value-sep The item value sep char. default is SPACE
*
* @param FlagsParser $fs
* @param Output $output
*/
public function textCommand(FlagsParser $fs, Output $output): void
{
$text = $fs->getOpt('source');
$text = ContentsAutoReader::readFrom($text);

$p = TextParser::new($text);
$p->setItemSep($fs->getOpt('item-sep'));

if ($vSep = $fs->getOpt('value-sep')) {
$p->setItemParser(TextParser::charSplitParser($vSep));
}

$p->parse();

switch ($fs->getArg('type')) {
case 'mdtable':
case 'mdTable':
case 'md-table':
$rows = ListStream::new($p->getData())
->eachToArray(function (array $item) {
return implode(' | ', $item);
});
$head = array_shift($rows);
$line = implode('|', array_pad(['-----'], $p->fieldNum, '-----'));

$result = $head . "\n" . $line . "\n". implode("\n", $rows);
break;
case 'raw':
default:
$result = $text;
break;
}

$outFile = $fs->getOpt('output');
ContentsAutoWriter::writeTo($outFile, $result);
}

/**
* convert YAML to java properties contents.
*
Expand All @@ -125,16 +184,8 @@ public function sql2mdCommand(FlagsParser $fs, Output $output): void
public function yaml2propCommand(FlagsParser $fs, Output $output): void
{
$file = $fs->getOpt('file');
if (!$file) {
$str = Clipboard::readAll();
} else {
if (!is_file($file)) {
throw new PromptException("input source file not exists, file: $file");
}

$str = file_get_contents($file);
}

$str = ContentsAutoReader::readFrom($file);
if (!$str) {
throw new InvalidArgumentException('the source yaml contents is empty');
}
Expand All @@ -151,16 +202,8 @@ public function yaml2propCommand(FlagsParser $fs, Output $output): void

$result = $jp->encode($data);
$outFile = $fs->getOpt('output');
if (!$outFile || $outFile === 'stdout') {
$output->println($result);
} elseif ($outFile === 'clipboard') {
$output->info('will send result to Clipboard');
Clipboard::writeString($result);
} else {
$output->info("will write result to $outFile");
File::putContents($outFile, $result);
}

ContentsAutoWriter::writeTo($outFile, $result);
$output->success('Complete');
}

Expand Down
23 changes: 19 additions & 4 deletions app/Helper/KiteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,25 @@
*/
class KiteUtil
{
public const STDIN_ALIAS = ['@i','@stdin',];
public const STDOUT_ALIAS = ['@o','@stdout',];

public const CLIPBOARD_ALIAS = ['@c','@cb','@clip','@clipboard',];
public const STDIN_ALIAS = [
'@i',
'@stdin',
'stdin',
];

public const STDOUT_ALIAS = [
'@o',
'@stdout',
'stdout',
];

public const CLIPBOARD_ALIAS = [
'@c',
'@cb',
'@clip',
'@clipboard',
'clipboard',
];

/**
* @param string $str
Expand Down
Loading

0 comments on commit 0eb9e40

Please sign in to comment.