Skip to content

Commit

Permalink
enhance: update and optimize the quick jump logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 7, 2022
1 parent 3bb2cc3 commit 7d88fe5
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 13 deletions.
16 changes: 11 additions & 5 deletions app/Console/Controller/JumpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

use Inhere\Console\Controller;
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Jump\JumpShell;
use Inhere\Kite\Lib\Jump\JumpStorage;
use InvalidArgumentException;
use PhpPkg\EasyTpl\SimpleTemplate;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Str;
Expand Down Expand Up @@ -74,15 +74,20 @@ public function listCommand(FlagsParser $fs, Output $output): void
$output->colored('Datafile: ' . $qj->getDatafile(), 'cyan');
$output->println(Str::repeat('=', 60));

$key = $fs->getFirstArg();
$data = $qj->getEngine()->toArray(true);

$opts = [
'ucTitleWords' => false,
];

if ($key && isset($data[$key])) {
$output->aList($data[$key], $key, $opts);
if ($key = $fs->getFirstArg()) {
$val = $qj->getEngine()->get($key);
if ($val === null) {
throw new InvalidArgumentException("invalid data key: $key");
}

$output->aList($val, $key, $opts);
} else {
$data = $qj->getEngine()->toArray(true);
$output->mList($data, $opts);
}
}
Expand Down Expand Up @@ -210,6 +215,7 @@ public function getCommand(FlagsParser $fs, Output $output): void
// vdump($input, $_SERVER['argv']);
$name = $fs->getArg('name');
$dir = $qj->match($name);
// $qj->saveLastMatch($dir);

Kite::logger()->info("jump get directory is: $dir, name: $name");

Expand Down
19 changes: 18 additions & 1 deletion app/Lib/Jump/JumpStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class JumpStorage implements JsonSerializable
public const MATCH_BOTH = 3;

/**
* The json data file
*
* @var string
*/
private string $datafile;
Expand Down Expand Up @@ -380,13 +382,28 @@ public function __toString(): string

/**
* @return string
* @throws \JsonException
*/
public function toString(): string
{
return Json::encode($this->toArray(), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}

/**
* @param string $key
*
* @return string|array|null
*/
public function get(string $key): string|array|null
{
return match ($key) {
'prev', 'prevPath' => $this->prevPath,
'last', 'lastPath' => $this->lastPath,
'name', 'named', 'namedPaths' => $this->namedPaths,
'his', 'history', 'histories' => array_values($this->histories),
default => null,
};
}

/**
* @param bool $clearId
*
Expand Down
2 changes: 1 addition & 1 deletion resource/mkdown-demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Render colored markdown contents on console terminal
**composer**

```bash
composer require phpcom-lab/cli-markdown
composer require phppkg/cli-markdown
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion resource/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ like Yaml, json but simple.
**crontab**

- `dragonmantank/cron-expression` https://github.com/dragonmantank/cron-expression
- `phpcom-lab/hucron` https://github.com/phpcom-lab/hucron
- `phppkg/hucron` https://github.com/phppkg/hucron

**parser&lexer**

Expand Down
12 changes: 12 additions & 0 deletions resource/templates/completion/kite-alias.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
## aliases for kite
# NOTICE: zsh plugin support add aliases
#alias kj="kite jump"
alias kg="kite git"
alias kgit="kite git"
alias kjson="kite json"
alias kplug="kite plugin"
alias kstr="kite string"
alias kgl="kite gitlab"
alias kgh="kite github"
alias cht="kite cheat"
11 changes: 11 additions & 0 deletions resource/templates/go-code-tpl/struct.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// {{ $className }} struct
//
// Gen by:
// {{ $genMark | nl }}
type {{ $className }} struct {
{{ foreach ($fields as $field): }}
// {{= $field->name | camel:true }} {{= $field->comment | nl }}
{{= $field->name | camel:true }} {{= $field->getType($lang) }} `json:"{{= $field->name | snake }}"`
{{ endforeach }}
}
4 changes: 4 additions & 0 deletions resource/templates/php-code-tpl/parts/class-comments.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @author {{= $user ?? 'inhere' | nl}}
* @date {{= date('Y-m-d H:i:s') | nl }}
*/
17 changes: 17 additions & 0 deletions resource/templates/php-code-tpl/req-resp-dto.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

{{ include('parts/class-comments', [
'user' => $user,
'date' => $date
]) }}
class {{ $className | nl }}
{
{{ foreach ($fields as $field): }}

/**
* {{= $field->comment | nl }}
*
* @param {{= $field->getType($lang) | nl }}
*/
public ${{= $field->name | camel }};
{{ endforeach }}
}
16 changes: 11 additions & 5 deletions resource/templates/quick-jump/jump.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
# # set the bind func name is: j
# eval "$(kite jump shell zsh --bind j)"
#
# for DEBUG:
# set -x # start
# set +x # stop
# typeset -ft __jump_chpwd # start for given function
# typeset +ft __jump_chpwd # stop
#
# The following lines are autogenerated:

# change pwd hook
__jump_chpwd() {
# x: is number
# cmd: the command name
# eg: "123 j" or "124 j /path/to/dir"
local lastCmd=$(history -1 | {
# eg:
# ': 1649259804:0;j /path/to'
# => x=':' cmd='1649259804:0;j' args='/path/to'
local midStr=$(tail -n 1 $HOME/.zsh_history | {
read x cmd args
echo "$cmd"
})

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

0 comments on commit 7d88fe5

Please sign in to comment.