Skip to content

Commit

Permalink
up: update cheat command, support multi query question
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 26, 2021
1 parent d107a85 commit e9bfc05
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
31 changes: 19 additions & 12 deletions app/Console/Command/CheatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
use Toolkit\Cli\Cli;
use Toolkit\Cli\Color;
use Toolkit\FsUtil\Dir;
use Toolkit\PFlag\FlagType;
use function dirname;
use function explode;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function strpos;
use function substr;
use function trim;
Expand Down Expand Up @@ -56,8 +58,9 @@ protected function configure(): void
$fs = $this->getFlags();

$fs->addArg('topic', 'The language/topic for search. eg: go, php, java, lua, python, js ...');
$fs->addArg('question', 'The question search.');
$fs->addArg('question', 'The questions on the topic.', FlagType::ARRAY);

$fs->addOptByRule('refresh', 'bool;ignore cached result, re-request remote cheat server');
$fs->addOpt('search', 's', 'search by the keywords');
// $fs->addOpt('T', 't', 'query');

Expand Down Expand Up @@ -94,7 +97,9 @@ protected function configure(): void
HELP
);
$fs->setExampleHelp([
'{fullCmd} go reverse list'
'{binWithCmd} php strlen',
'{binWithCmd} go reverse list',
'{binWithCmd} java Optional',
]);
}

Expand Down Expand Up @@ -135,12 +140,12 @@ protected function execute(Input $input, Output $output)
}

$topic = $this->flags->getArg('topic');
$query = $this->flags->getArg('question');
if (!$topic) {
throw new InvalidArgumentException('please input an topic name for query.');
}

$result = $this->queryResult($topic, $query);
$queries = $this->flags->getArg('question');
$result = $this->queryResult($topic, $queries);

$output->colored('RESULT:');
$output->write($result);
Expand All @@ -150,23 +155,25 @@ protected function execute(Input $input, Output $output)

/**
* @param string $topic
* @param string $query query question
* @param string[] $queries query questions
* @param bool $refresh
*
* @return string
*/
protected function queryResult(string $topic, string $query, bool $refresh = false): string
protected function queryResult(string $topic, array $queries, bool $refresh = false): string
{
if (!$topic = trim($topic)) {
throw new InvalidArgumentException('topic cannot be empty');
}

$cacheDir = Kite::getPath('tmp/cheat');
$queryPath = implode('/', $queries);
$cacheDir = Kite::getTmpPath('cheat');

if ($topic[0] === ':') {
$cacheFile = $cacheDir . "/$topic.txt";
} else {
$cacheFile = $cacheDir . '/' . $topic;
$cacheFile .= $query ? "/$query.txt" : '/_topic.txt';
$cacheFile .= $queryPath ? "/$queryPath.txt" : '/_topic.txt';
}

if (!$refresh && file_exists($cacheFile)) {
Expand All @@ -175,8 +182,8 @@ protected function queryResult(string $topic, string $query, bool $refresh = fal
}

$chtApiUrl = self::CHT_HOST . $topic;
if ($query) {
$chtApiUrl .= "/$query";
if ($queryPath) {
$chtApiUrl .= '/' . $queryPath;
}

Cli::info('will request remote URL: ' . $chtApiUrl);
Expand All @@ -191,7 +198,7 @@ protected function queryResult(string $topic, string $query, bool $refresh = fal
// not found
if (
$bodyLen < 300 &&
(strpos($result, 'Unknown topic.') !== false || strpos($result, 'Unknown cheat sheet') !== false)
(str_contains($result, 'Unknown topic.') || str_contains($result, 'Unknown cheat sheet'))
) {
return $result;
}
Expand All @@ -201,7 +208,7 @@ protected function queryResult(string $topic, string $query, bool $refresh = fal
[$firstLine,] = explode("\n", $result);
// vdump($firstLine);
$name = trim(Color::clearColor($firstLine), "#/ \t\n\r\0\x0B");
if (strpos($name, 'cheat:') === 0) {
if (str_starts_with($name, 'cheat:')) {
$name = substr($name, 6);
}

Expand Down
11 changes: 11 additions & 0 deletions app/Kite.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public static function basePath(bool $rmPharMark = true): string
return $rmPharMark ? Dir::clearPharPath(BASE_PATH) : BASE_PATH;
}

/**
* @param string $path
*
* @return string
*/
public static function getTmpPath(string $path): string
{
// TODO tmp path 不该跟着 kite 执行文件
return self::getPath("/tmp/$path");
}

/**
* @param string $path
* @param bool $rmPharMark Will clear prefix 'phar://', if on phar package.
Expand Down

0 comments on commit e9bfc05

Please sign in to comment.