Skip to content

Commit 1ec5eb1

Browse files
committed
some bug fixed for auto completion
1 parent e30dfbc commit 1ec5eb1

File tree

6 files changed

+119
-50
lines changed

6 files changed

+119
-50
lines changed

auto-completion.bash

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
# ------------------------------------------------------------------------------
3+
# DATE: 2019-01-07 10:16:23
4+
# FILE: auto-completion.bash
5+
# AUTHOR: inhere (https://github.com/inhere)
6+
# VERSION: 0.5.1
7+
# DESCRIPTION: bash shell complete for console app: examples/app
8+
# ------------------------------------------------------------------------------
9+
# usage: source auto-completion.bash
10+
# run 'complete' to see registered complete function.
11+
12+
_complete_for_examples_app () {
13+
local cur prev
14+
commands="demo exam test self-update cor home process phar show interact version help list"
15+
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
16+
}
17+
18+
complete -F _complete_for_examples_app examples/app

auto-completion.zsh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#compdef examples/app
2+
# ------------------------------------------------------------------------------
3+
# DATE: 2019-01-07 10:34:20
4+
# FILE: auto-completion.zsh
5+
# AUTHOR: inhere (https://github.com/inhere)
6+
# VERSION: 0.5.1
7+
# DESCRIPTION: zsh shell complete for console app: examples/app
8+
# ------------------------------------------------------------------------------
9+
# usage: source auto-completion.zsh
10+
11+
_complete_for_examples_app () {
12+
local -a commands
13+
IFS=$'\n'
14+
commands=(
15+
'version:Show application version information'
16+
'help:Show application help information'
17+
'list:List all group and alone commands'
18+
'home:This is a demo command controller. there are some command usage examples(2) [alias\: h]'
19+
'interact:there are some demo commands for use interactive method [alias\: iact]'
20+
'phar:Pack a project directory to phar or unpack phar to directory'
21+
'process:Some simple process to create and use examples [alias\: prc]'
22+
'show:there are some demo commands for show format data'
23+
'cor:a coroutine test command [alias\: coro]'
24+
'demo:this is a demo alone command. but config use configure(), like symfony console\: argument define by position'
25+
'exam:a description message'
26+
'self-update:Update phar package to most recent stable, pre-release or development build. [alias\: selfUpdate]'
27+
'test:this is a test independent command [alias\: t]'
28+
)
29+
30+
_describe 'commands' commands
31+
}
32+
33+
compdef _complete_for_examples_app examples/app

res/templates/auto-completion.zsh.tpl

Lines changed: 0 additions & 29 deletions
This file was deleted.

res/templates/auto-completion.bash.tpl renamed to res/templates/bash-completion.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
# DATE: {{datetime}}
44
# FILE: auto-completion.bash
55
# AUTHOR: inhere (https://github.com/inhere)
6-
# VERSION: 1.0.0
7-
# DESCRIPTION: bash shell complete for cli app: cliapp
6+
# VERSION: {{version}}
7+
# DESCRIPTION: bash shell complete for console app: {{binName}}
88
# ------------------------------------------------------------------------------
99
# usage: source auto-completion.bash
1010
# run 'complete' to see registered complete function.
1111

12-
_complete_for_cliapp () {
12+
_complete_for_{{fmtBinName}} () {
1313
local cur prev
14-
commands= "{{commands}}"
14+
commands="{{commands}}"
1515
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
1616
}
1717

18-
complete -F _complete_for_cliapp examples/app
18+
complete -F _complete_for_{{fmtBinName}} {{binName}}

res/templates/zsh-completion.tpl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#compdef {{binName}}
2+
# ------------------------------------------------------------------------------
3+
# DATE: {{datetime}}
4+
# FILE: auto-completion.zsh
5+
# AUTHOR: inhere (https://github.com/inhere)
6+
# VERSION: {{version}}
7+
# DESCRIPTION: zsh shell complete for console app: {{binName}}
8+
# ------------------------------------------------------------------------------
9+
# usage: source auto-completion.zsh
10+
11+
_complete_for_{{fmtBinName}} () {
12+
local -a commands
13+
IFS=$'\n'
14+
commands=(
15+
{{commands}}
16+
)
17+
18+
_describe 'commands' commands
19+
}
20+
21+
compdef _complete_for_{{fmtBinName}} {{binName}}

src/Traits/ApplicationHelpTrait.php

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Inhere\Console\Traits;
1010

11+
use Inhere\Console\Component\Style\Style;
1112
use Inhere\Console\Face\CommandInterface;
1213
use Inhere\Console\Util\FormatUtil;
1314
use Inhere\Console\Util\Helper;
@@ -197,10 +198,14 @@ public function showCommandList()
197198
}
198199

199200
/**
200-
* for zsh:
201-
* php bin/app --auto-completion --shell-env zsh
202-
* for bash:
203-
* php bin/app --auto-completion --shell-env bash
201+
* zsh:
202+
* php examples/app --auto-completion --shell-env zsh
203+
* php examples/app --auto-completion --shell-env zsh --gen-file
204+
* php examples/app --auto-completion --shell-env zsh --gen-file stdout
205+
* bash:
206+
* php examples/app --auto-completion --shell-env bash
207+
* php examples/app --auto-completion --shell-env bash --gen-file
208+
* php examples/app --auto-completion --shell-env bash --gen-file stdout
204209
* @param string $shellEnv
205210
* @param array $data
206211
*/
@@ -213,21 +218,21 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
213218

214219
// info
215220
$glue = ' ';
216-
$genFile = $input->getLongOpt('gen-file');
221+
$genFile = (string)$input->getLongOpt('gen-file');
217222
$filename = 'auto-completion.' . $shellEnv;
218-
$tplDir = \dirname(__DIR__, 2) . '/templates';
223+
$tplDir = \dirname(__DIR__, 2) . '/res/templates';
219224

220225
if ($shellEnv === 'bash') {
221-
$tplFile = $tplDir . '/auto-completion.bash.tpl';
222-
$list = \array_merge(
226+
$tplFile = $tplDir . '/bash-completion.tpl';
227+
$list = \array_merge(
223228
$this->getCommandNames(),
224229
$this->getControllerNames(),
225230
$this->getInternalCommands()
226231
);
227232
} else {
228-
$glue = \PHP_EOL;
229-
$list = [];
230-
$tplFile = $tplDir . '/auto-completion.zsh.tpl';
233+
$glue = \PHP_EOL;
234+
$list = [];
235+
$tplFile = $tplDir . '/zsh-completion.tpl';
231236
foreach ($data as $name => $desc) {
232237
$list[] = $name . ':' . \str_replace(':', '\:', $desc);
233238
}
@@ -241,15 +246,36 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
241246
return;
242247
}
243248

249+
if ($shellEnv === 'zsh') {
250+
$commands = "'" . \implode("'\n'", $list) . "'";
251+
$commands = Style::stripColor($commands);
252+
}
253+
244254
// dump at script file
255+
$binName = $input->getBinName();
245256
$tplText = \file_get_contents($tplFile);
246257
$content = \strtr($tplText, [
247-
'{{filename}}' => $filename,
248-
'{{commands}}' => $commands,
249-
'{{binName}}' => $input->getBinName(),
250-
'{{datetime}}' => \date('Y-m-d H:i:s'),
258+
'{{version}}' => $this->getVersion(),
259+
'{{filename}}' => $filename,
260+
'{{commands}}' => $commands,
261+
'{{binName}}' => $binName,
262+
'{{datetime}}' => \date('Y-m-d H:i:s'),
263+
'{{fmtBinName}}' => \str_replace('/', '_', $binName),
251264
]);
252265

253-
\file_put_contents($input->getPwd() . '/' . $filename, $content);
266+
// dump to stdout
267+
if ($genFile === 'stdout') {
268+
\file_put_contents('php://stdout', $content);
269+
return;
270+
}
271+
272+
$targetFile = $input->getPwd() . '/' . $filename;
273+
$output->write(['Target File:', $targetFile, '']);
274+
275+
if (\file_put_contents($targetFile, $content) > 10) {
276+
$output->success("O_O! Generate $filename successful!");
277+
} else {
278+
$output->error("O^O! Generate $filename failure!");
279+
}
254280
}
255281
}

0 commit comments

Comments
 (0)