diff --git a/app/Console/Component/ContentsAutoReader.php b/app/Console/Component/ContentsAutoReader.php index dd951a8..d09cfee 100644 --- a/app/Console/Component/ContentsAutoReader.php +++ b/app/Console/Component/ContentsAutoReader.php @@ -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; @@ -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 */ @@ -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; } diff --git a/app/Console/Controller/GenerateController.php b/app/Console/Controller/GenerateController.php index dada1aa..1e14db8 100644 --- a/app/Console/Controller/GenerateController.php +++ b/app/Console/Controller/GenerateController.php @@ -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(); diff --git a/app/Console/Controller/PhpController.php b/app/Console/Controller/PhpController.php index c11882a..d539163 100644 --- a/app/Console/Controller/PhpController.php +++ b/app/Console/Controller/PhpController.php @@ -40,6 +40,7 @@ use function sprintf; use function str_contains; use function trim; +use function vdump; /** * Class GitGroup @@ -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. * @@ -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 = <<info('TODO'); + } + /** * Search php package from packagist.org * diff --git a/app/Helper/AppHelper.php b/app/Helper/AppHelper.php index 3dde46e..a18b580 100644 --- a/app/Helper/AppHelper.php +++ b/app/Helper/AppHelper.php @@ -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\""; } diff --git a/app/Lib/Parser/IniParser.php b/app/Lib/Parser/IniParser.php index aa82afe..655a8db 100644 --- a/app/Lib/Parser/IniParser.php +++ b/app/Lib/Parser/IniParser.php @@ -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. * diff --git a/app/Lib/Stream/ListStream.php b/app/Lib/Stream/ListStream.php index b10bec0..6411b74 100644 --- a/app/Lib/Stream/ListStream.php +++ b/app/Lib/Stream/ListStream.php @@ -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; } @@ -101,7 +100,7 @@ public function eachToMapStream(callable $func, MapStream $new): MapStream * * @return array */ - public function eachToMapArray(callable $func): array + public function eachToMap(callable $func): array { $map = []; foreach ($this as $item) { diff --git a/resource/templates/quick-jump/jump.zsh b/resource/templates/quick-jump/jump.zsh index 6a039da..9352566 100644 --- a/resource/templates/quick-jump/jump.zsh +++ b/resource/templates/quick-jump/jump.zsh @@ -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