Skip to content

Commit

Permalink
👔 up: update some git and doc commands logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 14, 2023
1 parent 71ed5c3 commit 7d44c41
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 228 deletions.
12 changes: 12 additions & 0 deletions app/Common/CmdRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Inhere\Kite\Common;

use RuntimeException;
use Toolkit\Cli\Cli;
use Toolkit\Cli\Color;
use Toolkit\Sys\Cmd\AbstractCmdBuilder;
use function is_array;
Expand Down Expand Up @@ -153,6 +154,17 @@ public function batch(array $commands): self
return $this;
}

/**
* @param string $subCmd
* @param ...$args
*
* @return $this
*/
public function git(string $subCmd, ...$args): self
{
return $this->add(Cli::toCmdline($args, "git $subCmd"));
}

/**
* @param string $cmdTpl
* @param mixed ...$args
Expand Down
23 changes: 19 additions & 4 deletions app/Console/Command/DocCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use Toolkit\Cli\Color;
use Toolkit\PFlag\FlagType;
use Toolkit\Sys\Proc\ProcWrapper;
use function array_keys;
use function array_pop;
use function count;
use function dirname;
use function implode;
use function rtrim;
Expand Down Expand Up @@ -79,6 +81,7 @@ protected function configure(): void

$fs->addOpt('lang', '', 'use the language for find topic document', FlagType::STRING, false, $lang);
$fs->addOpt('create', '', 'create an new topic document', FlagType::BOOL);
$fs->addOpt('config', '', 'show config for document settings', FlagType::BOOL);
$fs->addOpt('cat', '', 'see the document file contents', FlagType::BOOL);
$fs->addOpt('edit', 'e', 'edit an topic document', FlagType::BOOL);
$fs->addOpt('editor', '', 'editor for edit the topic document', FlagType::STRING, false, 'vim');
Expand Down Expand Up @@ -120,7 +123,7 @@ private function prepareManDoc(): Document
}

/**
* @param Input $input
* @param Input $input
* @param Output $output
*/
protected function execute(Input $input, Output $output): void
Expand All @@ -135,6 +138,18 @@ protected function execute(Input $input, Output $output): void
return;
}

$fs = $this->flags;
if ($fs->getOpt('config')) {
$tNames = array_keys($man->getTopicNames());
$total = count($tNames);
$output->aList([
'Language' => $man->getLang(),
'Docs paths' => $man->getPaths(),
"Topic names(total: $total)" => $tNames,
]);
return;
}

$this->topName = $this->flags->getArg('top');
$this->subNames = $this->flags->getArg('subs', []);

Expand Down Expand Up @@ -190,8 +205,8 @@ protected function execute(Input $input, Output $output): void

/**
* @param Document $doc
* @param Output $output
* @param string $nameString
* @param Output $output
* @param string $nameString
*/
private function createTopic(Document $doc, Output $output, string $nameString): void
{
Expand Down Expand Up @@ -235,7 +250,7 @@ private function editTopic(DocTopic $topic): void

/**
* @param Document $man
* @param string $nameString
* @param string $nameString
*/
private function listTopicInfo(Document $man, string $nameString): void
{
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Command/LinuxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlag\Validator\EnumValidator;

/**
* class LinuxCommand
Expand Down Expand Up @@ -54,5 +53,6 @@ protected function configFlags(FlagsParser $fs): void
protected function execute(Input $input, Output $output)
{
// TODO: Implement execute() method.
$output->info('TODO');
}
}
2 changes: 2 additions & 0 deletions app/Console/Command/ToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\SubCmd\GitxCmd\GitEmojiCmd;
use Inhere\Kite\Console\SubCmd\OpenCmd;
use Inhere\Kite\Console\SubCmd\ToolCmd\BatchCommand;
use Inhere\Kite\Console\SubCmd\ToolCmd\CatCommand;
Expand Down Expand Up @@ -49,6 +50,7 @@ protected function subCommands(): array
CatCommand::class,
FindCommand::class,
SearchCommand::class,
GitEmojiCmd::class,
];
}

Expand Down
181 changes: 1 addition & 180 deletions app/Console/Component/Clipboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,191 +2,12 @@

namespace Inhere\Kite\Console\Component;

use Toolkit\Stdlib\Obj\AbstractObj;
use Toolkit\Stdlib\OS;
use Toolkit\Sys\Exec;
use function addslashes;
use function file_put_contents;
use function tempnam;

/**
* Class Clipboard
*
* @package Inhere\Kite\Helper
*/
class Clipboard extends AbstractObj
class Clipboard extends \Toolkit\Sys\Tool\Clipboard
{
public const WRITER_ON_MAC = 'pbcopy';
public const WRITER_ON_WIN = 'clip';
public const WRITER_ON_LINUX = 'xsel';

public const READER_ON_MAC = 'pbpaste';
public const READER_ON_WIN = 'clip';
public const READER_ON_LINUX = 'xclip';

/**
* @var string
*/
private string $writerApp;

/**
* @var string
*/
private string $readerApp;

/**
* @return string
*/
public static function readAll(): string
{
return (new self())->read();
}

/**
* @param string $text
*
* @return bool
*/
public static function writeString(string $text): bool
{
return (new self())->write($text);
}

public function __construct()
{
parent::__construct();

$this->writerApp = $this->getWriterByOS();
$this->readerApp = $this->getReaderByOS();
}

/**
* @param string $contents
* @param bool $addSlashes
*
* @return bool
*/
public function write(string $contents, bool $addSlashes = false): bool
{
$program = $this->writerApp;
if (!$program) {
return false;
}

// $contents = trim($contents);
if ($addSlashes) {
$contents = addslashes($contents);
}

// $contents = str_replace("\n", " \\\n", $contents);
$multiLine = str_contains($contents, "\n");

// linux:
// # Copy input to clipboard
// echo -n "$input" | xclip -selection c
// Mac:
// echo hello | pbcopy
// pbcopy < tempfile.txt
if ($multiLine) {
$file = tempnam(OS::tempDir(), "tmp_");

// File::write($contents, $file);
file_put_contents($file, $contents);
$command = "$program < $file";
} else {
$command = "echo $contents | $program";
}

$result = Exec::auto($command);
return (int)$result['status'] === 0;
}

/**
* @return string
*/
public function read(): string
{
$program = $this->readerApp;
if (!$program) {
return '';
}

$result = Exec::auto($program);

return $result['output'];
}

/**
* @param string $file
*
* @return bool
*/
public function readToFile(string $file): bool
{
$program = $this->readerApp;
if (!$program) {
return false;
}

// Mac: pbpaste >> tasklist.txt
$result = Exec::auto("$program >> $file");

return (int)$result['status'] === 0;
}

/**
* @return string
*/
protected function getWriterByOS(): string
{
if (OS::isWindows()) {
return self::WRITER_ON_WIN;
}

if (OS::isMac()) {
return self::WRITER_ON_MAC;
}

if (OS::isLinux()) {
return self::WRITER_ON_LINUX;
}

return '';
}

/**
* @return string
*/
protected function getReaderByOS(): string
{
if (OS::isWindows()) {
return self::READER_ON_WIN;
}

if (OS::isMac()) {
return self::READER_ON_MAC;
}

if (OS::isLinux()) {
return self::READER_ON_LINUX;
}

return '';
}

/**
* @return string
*/
public function getReaderApp(): string
{
return $this->readerApp;
}

/**
* @return string
*/
public function getWriterApp(): string
{
return $this->writerApp;
}
}
28 changes: 25 additions & 3 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
use function is_scalar;
use function is_string;
use function json_decode;
use function preg_replace;
use function str_contains;
use function str_replace;
use function str_starts_with;
use function stripslashes;
use function trim;
use const JSON_THROW_ON_ERROR;

Expand Down Expand Up @@ -151,7 +151,7 @@ private function autoReadJSON(string $source): void
*/
public function loadCommand(FlagsParser $fs, Output $output): void
{
$ext = '.json';
$ext = '.json';
$json = ContentsAutoReader::readFrom($fs->getArg('source'), [
'suffix' => $ext,
]);
Expand Down Expand Up @@ -273,6 +273,7 @@ public function searchCommand(FlagsParser $fs, Output $output): void
*
* @options
* --uq, --unquote bool;unquote input string before format.
* --simple bool; simple split to multi line, not use json_decode
*
* @arguments
* json The json text line. allow: @load, @clipboard, @stdin
Expand All @@ -287,8 +288,29 @@ public function prettyCommand(FlagsParser $fs, Output $output): void
'loadedFile' => $this->dumpfile,
]);

if ($fs->getOpt('simple')) {
$output->writeRaw(str_replace(',', ",\n", $json));
return;
}

if ($fs->getOpt('unquote')) {
$json = stripslashes($json);
$regexMap = [
'#"\{"#' => '{"',
'#:"\[#' => ':[',
'#"\[\{"#' => '[{"',
'#\}\]"#' => '}]',
'#\}"#' => '}',
'#\]"#' => ']',
'#\\\\+#' => '',
];
$output->aList($regexMap, 'regex map', [
'sepChar' => ' => ',
]);

foreach ($regexMap as $pattern => $replace) {
$json = preg_replace($pattern, $replace, $json);
}
// $output->writeRaw(str_replace(',', ",\n", $json));
}

// $data = json_decode($json, true);
Expand Down
Loading

0 comments on commit 7d44c41

Please sign in to comment.