Skip to content

Commit

Permalink
up: move ts2date to a alone command class, and attach to str and conv…
Browse files Browse the repository at this point in the history
… group
  • Loading branch information
inhere committed Oct 30, 2022
1 parent 9d806fc commit 4e6525d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 77 deletions.
81 changes: 14 additions & 67 deletions app/Console/Controller/ConvertController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Console\SubCmd\ConvCmd\Ts2dateCmd;
use Inhere\Kite\Helper\KiteUtil;
use Inhere\Kite\Lib\Convert\JavaProperties;
use Inhere\Kite\Lib\Parser\DBTable;
Expand All @@ -26,9 +27,7 @@
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Json;
use function base_convert;
use function count;
use function date;
use function preg_match_all;
use function strlen;
use function strtotime;
use function time;
Expand Down Expand Up @@ -58,16 +57,19 @@ public static function aliases(): array
protected static function commandAliases(): array
{
return [
'ts2date' => [
'tc',
'td',
],
'date2ts' => [
'dt',
],
'yaml2json' => ['yml2json', 'y2j'],
'yaml2prop' => ['yml2prop', 'y2p'],
'prop2yaml' => ['prop2yml', 'p2y'],
'date2ts' => ['dt',],
'yaml2json' => ['yml2json', 'y2j'],
'yaml2prop' => ['yml2prop', 'y2p'],
'prop2yaml' => ['prop2yml', 'p2y'],
] + [
Ts2dateCmd::getName() => Ts2dateCmd::aliases(),
];
}

public function subCommands(): array
{
return [
Ts2dateCmd::class,
];
}

Expand Down Expand Up @@ -272,59 +274,4 @@ public function date2tsCommand(FlagsParser $fs, Output $output): void
], 'today');
}

/**
* convert timestamp to datetime
*
* @arguments
* times array;The want convert timestamps, allow @clipboard;true
*
* @param FlagsParser $fs
* @param Output $output
*/
public function ts2dateCommand(FlagsParser $fs, Output $output): void
{
$args = $fs->getArg('times');

if (count($args) === 1 && KiteUtil::isClipboardAlias($args[0])) {
$text = Clipboard::new()->read();
$args = $text ? [$text] : [];

if (!$args) {
throw new PromptException('no contents in clipboard');
}
}

$output->info('Input Data:');
$output->writeRaw($args);

$data = [];
foreach ($args as $time) {
if (strlen($time) > 10) {
preg_match_all('/1\d{9}/', $time, $matches);
if (empty($matches[0])) {
$output->warning("not found time in the: $time");
continue;
}

foreach ($matches[0] as $match) {
$data[] = [
'timestamp' => $match,
'datetime' => date('Y-m-d H:i:s', (int)$match),
];
}
continue;
}

$data[] = [
'timestamp' => $time,
'datetime' => date('Y-m-d H:i:s', (int)$time),
];
}

$output->info('Parsed Result:');
$output->colored('- Current Time: ' . date('Y-m-d H:i:s'));
// opts
$output->table($data, 'Time to date', []);
}

}
21 changes: 13 additions & 8 deletions app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Console\SubCmd\ConvCmd\Ts2dateCmd;
use Inhere\Kite\Console\SubCmd\ParseUrlQueryCmd;
use Inhere\Kite\Console\SubCmd\ToolCmd\HashCommand;
use Inhere\Kite\Console\SubCmd\ToolCmd\RandomCommand;
Expand Down Expand Up @@ -70,14 +71,17 @@ public static function aliases(): array
protected static function commandAliases(): array
{
return [
'join' => ['implode', 'j'],
'split' => ['s'],
'process' => ['p', 'filter', 'f'],
'replace' => ['r'],
'parse' => ['fields'],
'random' => RandomCommand::aliases(),
'dequery' => ParseUrlQueryCmd::aliases(),
];
'join' => ['implode', 'j'],
'split' => ['s'],
'process' => ['p', 'filter', 'f'],
'replace' => ['r'],
'parse' => ['fields'],
] + [
RandomCommand::getName() => RandomCommand::aliases(),
ParseUrlQueryCmd::getName() => ParseUrlQueryCmd::aliases(),
HashCommand::getName() => HashCommand::aliases(),
Ts2dateCmd::getName() => Ts2dateCmd::aliases(),
];
}

protected function init(): void
Expand All @@ -90,6 +94,7 @@ protected function init(): void
protected function subCommands(): array
{
return [
Ts2dateCmd::class,
ParseUrlQueryCmd::class,
HashCommand::class,
RandomCommand::class,
Expand Down
93 changes: 93 additions & 0 deletions app/Console/SubCmd/ConvCmd/Ts2dateCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd\ConvCmd;

use Inhere\Console\Command;
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Helper\KiteUtil;
use Toolkit\PFlag\FlagsParser;
use function count;
use function date;
use function preg_match_all;
use function strlen;

/**
* Class Ts2dateCmd
*
* @package Inhere\Kite\Console\Controller\Gitlab
*/
class Ts2dateCmd extends Command
{
protected static string $name = 'ts2date';
protected static string $desc = 'quick convert all timestamp number to datetime';

public static function aliases(): array
{
return ['t2d', 'ts', 'td'];
}

protected function configFlags(FlagsParser $fs): void
{
// $this->flags->addOptByRule($name, $rule);
}

/**
* @arguments
* times array;The want convert timestamps, allow @clipboard;true
*
* @param Input $input
* @param Output $output
*
* @return mixed
*/
protected function execute(Input $input, Output $output): mixed
{
$fs = $this->flags;

$args = $fs->getArg('times');
if (count($args) === 1 && KiteUtil::isClipboardAlias($args[0])) {
$text = Clipboard::new()->read();
$args = $text ? [$text] : [];

if (!$args) {
throw new PromptException('no contents in clipboard');
}
}

$output->info('Input Data:');
$output->writeRaw($args);

$data = [];
foreach ($args as $time) {
if (strlen($time) > 10) {
preg_match_all('/1\d{9}/', $time, $matches);
if (empty($matches[0])) {
$output->warning("not found time in the: $time");
continue;
}

foreach ($matches[0] as $match) {
$data[] = [
'timestamp' => $match,
'datetime' => date('Y-m-d H:i:s', (int)$match),
];
}
continue;
}

$data[] = [
'timestamp' => $time,
'datetime' => date('Y-m-d H:i:s', (int)$time),
];
}

$output->info('Parsed Result:');
// opts
$output->table($data, 'Time to date', []);
$output->colored('> Current Time: ' . date('Y-m-d H:i:s'));
return 0;
}
}
4 changes: 2 additions & 2 deletions app/Console/SubCmd/Gitflow/UpdatePushCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function configFlags(FlagsParser $fs): void
*
* @options
* --dr, --dry-run bool;Dry-run the workflow, dont real execute
* --np, --not-push bool;Not push to remote repo after updated.
* --np, --no-push bool;Not push to remote repo after updated.
* --rb, --remote-branch The remote branch name, default is current branch name.
*
* @param Input $input
Expand Down Expand Up @@ -64,7 +64,7 @@ protected function execute(Input $input, Output $output): mixed

$si = $gx->getStatusInfo();

if (!$fs->getOpt('not-push')) {
if (!$fs->getOpt('no-push')) {
$runner->addf('git push %s', $si->upRemote);
}

Expand Down

0 comments on commit 4e6525d

Please sign in to comment.