Skip to content

Commit

Permalink
update: update some commands logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 6, 2021
1 parent 0eb9e40 commit 6a570bb
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 50 deletions.
66 changes: 33 additions & 33 deletions app/Console/Component/ContentsAutoReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Inhere\Kite\Console\Component;

use Inhere\Kite\Helper\KiteUtil;
use Inhere\Kite\Kite;
use InvalidArgumentException;
use Toolkit\Cli\Cli;
use Toolkit\FsUtil\File;
use Toolkit\Stdlib\Obj\AbstractObj;
Expand Down Expand Up @@ -50,7 +52,7 @@ public static function readFrom(string $source, array $opts = []): string
* - input '@FILEPATH' or FILEPATH - will read from the filepath.
*
* @param string $source the input text
* @param array{print: bool, loadedFile: string} $opts
* @param array{print: bool, loadedFile: string, throwOnEmpty: true} $opts
*
* @return string
*/
Expand All @@ -67,43 +69,41 @@ public function read(string $source, array $opts = []): string

// 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))) {
if (KiteUtil::isStdinAlias($source)) {
$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 (KiteUtil::isClipboardAlias($source)) {
$this->srcType = self::TYPE_CLIPBOARD;
$print && Cli::info('try read contents from Clipboard');
$str = Clipboard::new()->read();
} 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 = Kite::alias($source);
if ($filepath[0] === '@') {
$filepath = substr($filepath, 1);
}

if (is_file($filepath)) {
$this->srcType = self::TYPE_FILE;
$print && Cli::info('try read contents from file: ' . $lFile);
$str = File::readAll($lFile);
} else {
$filepath = Kite::alias($source);
if ($filepath[0] === '@') {
$filepath = substr($filepath, 1);
}

if (is_file($filepath)) {
$this->srcType = self::TYPE_FILE;
$print && Cli::info('try read contents from file: ' . $filepath);
$str = File::readAll($filepath);
}
$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);
}
}

if (($opts['throwOnEmpty'] ?? true) && !$str) {
throw new InvalidArgumentException('Nothing contents was read');
}

return $str;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Console/Controller/GenerateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function parseCommand(FlagsParser $fs, Output $output): void
[$varDefine, $template] = explode('###', $content);

// $vars = (array)parse_ini_string(trim($varDefine), true);
$vars = IniParser::parseString(trim($varDefine));
$vars = IniParser::decode(trim($varDefine));
$output->aList($vars, 'template vars', ['ucFirst' => false]);

$logic = new TextTemplate();
Expand Down
31 changes: 24 additions & 7 deletions app/Console/Controller/PhpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use function sprintf;
use function str_contains;
use function trim;
use function vdump;

/**
* Class GitGroup
Expand Down Expand Up @@ -166,7 +167,7 @@ public function sql2arrCommand(FlagsParser $fs, Output $output): void
*
* @options
* --no-debug bool;not set the --debug option on run test
* -f, --filter Set the --filter option for phpunit
* -f, --filter Set keywords for the --filter option
* --php-bin manual set the php bin file path.
* --phpunit manual set the phpunit(.phar) file path.
*
Expand Down Expand Up @@ -407,18 +408,23 @@ public function runCodeCommand(FlagsParser $fs, Output $output): void

$ret = $this->evalCode($code);

// echo Php::dumpVars($box->get($objName));
$output->info('TODO');
$output->info('RESULT:');
$output->writeRaw($ret);
}

/**
* @param string $code
*
* @return string
*/
private function evalCode(string $code): string
{
$code = trim($code);
$code = trim($code, ';') . ';';
$code = rtrim(trim($code), ';');

$phpCode = <<<CODE
<?php
// print
use Toolkit\Stdlib\Php;
// run
echo Php::dumpVars($code);
CODE;

Expand All @@ -427,6 +433,17 @@ private function evalCode(string $code): string
return ob_get_clean();
}

/**
* open the php doc sites
*
* @param FlagsParser $fs
* @param Output $output
*/
public function docOpenCommand(FlagsParser $fs, Output $output): void
{
$output->info('TODO');
}

/**
* Search php package from packagist.org
*
Expand Down
3 changes: 2 additions & 1 deletion app/Helper/AppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ public static function openBrowser(string $pageUrl): void
if (Sys::isMac()) {
$cmd = "open \"$pageUrl\"";
} elseif (Sys::isWin()) {
// $cmd = 'cmd /c start';
// $cmd = 'cmd /c start URL';
$cmd = "start $pageUrl";
} else {
// $cmd = 'xdg-open URL';
$cmd = "x-www-browser \"$pageUrl\"";
}

Expand Down
12 changes: 11 additions & 1 deletion app/Lib/Parser/IniParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,21 @@ public static function new(string $source): self
*
* @return array
*/
public static function parseString(string $str): array
public static function decode(string $str): array
{
return (new self($str))->parse();
}

/**
* @param array $data
*
* @return string
*/
public static function encode(array $data): string
{
return '';
}

/**
* Class constructor.
*
Expand Down
9 changes: 4 additions & 5 deletions app/Lib/Stream/ListStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,16 @@ public function eachTo(callable $func, BaseStream $new): BaseStream
}

/**
* @param callable(array): array $func
* @param callable(array, int): array $func
*
* @return array
*/
public function eachToArray(callable $func): array
{
$arr = [];
foreach ($this as $item) {
$arr[] = $func($item);
foreach ($this as $idx => $item) {
$arr[] = $func($item, $idx);
}

return $arr;
}

Expand All @@ -101,7 +100,7 @@ public function eachToMapStream(callable $func, MapStream $new): MapStream
*
* @return array<string, mixed>
*/
public function eachToMapArray(callable $func): array
public function eachToMap(callable $func): array
{
$map = [];
foreach ($this as $item) {
Expand Down
4 changes: 2 additions & 2 deletions resource/templates/quick-jump/jump.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
__jump_chpwd() {
# x: is number
# cmd: the command name
# eg: "j" or "j /path/to/dir"
# eg: "123 j" or "124 j /path/to/dir"
local lastCmd=$(history -1 | {
read x cmd args
echo "$cmd"
})

# kite util log "lastCmd $lastCmd $@" --type zsh-jump-chdir
# kite util log "lastCmd $lastCmd $@" --type zsh-jump-chdir
# Do not process other commands executed
if [[ $lastCmd != "{{bindFunc}}" ]]; then
return 0
Expand Down

0 comments on commit 6a570bb

Please sign in to comment.