Skip to content

Commit 1ed1de4

Browse files
committed
Simplify -g option
1 parent d221480 commit 1ed1de4

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ If you don't need any custom completion behaviour:
1717
}
1818
```
1919

20-
3. Run `eval $([your-application] _completion -g [program-name])` in a terminal, where `[program-name]` is the name you want to register bash completion for (this will be the same as `[your-application]` if your application is on your PATH).
20+
3. Run `eval $([program] _completion -g)` in a terminal.
2121
4. Add the above command to your bash profile if you want the completion to apply automatically for new terminal sessions.
2222

23-
The command `_completion -g [program-name]` (`-g` being a shortcut for `--genhook`) generates a few lines of bash that, when run, register your application as a completion handler for `[program-name]`. Completion is handled by running the completion command on your application with no arguments: `[your-application] _completion`.
23+
The `-g` option generates a few lines of bash that, when run, register your application as a completion handler for your program name. Completion is handled by running the completion command on your application with no arguments: `[program] _completion`.
24+
25+
If you need to use completion with an alias, specify the program name to complete for with the `-p` option: `_completion -g -p [alias-name]`.
2426

2527
## Custom completion
2628

src/Stecman/Component/Symfony/Console/BashCompletion/CompletionCommand.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,27 @@ protected function configure()
2121
->setName('_completion')
2222
->setDescription('BASH completion hook.')
2323
->setHelp(<<<END
24-
To enable BASH completion, run: <comment>eval `beam completion --genhook="app-name"`</comment>
24+
To enable BASH completion, run:
25+
26+
<comment>eval `[program] _completion -g`</comment>.
27+
28+
Or for an alias:
29+
30+
<comment>eval `[program] _completion -g -p [alias]`</comment>.
31+
2532
END
2633
)
2734
->addOption(
2835
'genhook',
2936
'g',
37+
InputOption::VALUE_NONE,
38+
'Generate BASH script to use completion with this application.'
39+
)
40+
->addOption(
41+
'program',
42+
'p',
3043
InputOption::VALUE_REQUIRED,
31-
'Generate BASH script to use completion with this application. <comment>Requires program name as value.</comment>'
44+
'Program name to add completion for.'
3245
);
3346
}
3447

@@ -37,8 +50,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
3750
$this->handler = new CompletionHandler( $this->getApplication() );
3851
$handler = $this->handler;
3952

40-
if ($programName = $input->getOption('genhook')) {
41-
$output->write( $handler->generateBashCompletionHook($programName), true );
53+
if ( $input->getOption('genhook') ) {
54+
$output->write( $handler->generateBashCompletionHook($input->getOption('program')), true );
4255
} else {
4356
$handler->configureFromEnvironment();
4457
$output->write($this->runCompletion(), true);

src/Stecman/Component/Symfony/Console/BashCompletion/CompletionHandler.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function configureFromEnvironment()
7070
$this->commandLine = getenv('COMP_LINE');
7171

7272
if ($this->commandLine === false) {
73-
throw new \RuntimeException('Failed to configure from environment; Environment var COMP_LINE not set');
73+
throw new \RuntimeException('Failed to configure from environment; Environment var COMP_LINE not set.');
7474
}
7575

7676
$this->wordIndex = intval(getenv('COMP_CWORD'));
@@ -154,6 +154,9 @@ protected function completeForOptionShortcuts()
154154
*/
155155
protected function completeForOptionValues()
156156
{
157+
158+
fwrite(STDERR, "\n".print_r($this->words, true)."\n\nIndex: $this->wordIndex\n\n");
159+
157160
// if ($this->command && $this->wordIndex > 1) {
158161
// $left = $this->words[$this->wordIndex-1];
159162
//
@@ -330,6 +333,7 @@ protected function mapArgumentsToWords($arguments)
330333
{
331334
$argIndex = 0;
332335
$wordNum = -1;
336+
$prevWord = null;
333337
$argPositions = array();
334338

335339
$words = $this->words;
@@ -349,6 +353,7 @@ protected function mapArgumentsToWords($arguments)
349353
$argPositions[$argsArray[$argIndex]] = $wordNum;
350354
}
351355
$argIndex++;
356+
$prevWord = $word;
352357
}
353358

354359
return $argPositions;
@@ -363,7 +368,7 @@ protected function mapArgumentsToWords($arguments)
363368
protected function filterResults($array)
364369
{
365370
$curWord = $this->words[$this->wordIndex];
366-
return implode(' ',
371+
return implode("\n",
367372
array_filter($array, function($val) use ($curWord) {
368373
return fnmatch($curWord.'*', $val);
369374
})
@@ -379,7 +384,16 @@ public function generateBashCompletionHook($programName)
379384
{
380385
global $argv;
381386
$command = $argv[0];
382-
$funcName = "_{$programName}complete";
387+
388+
if (!$programName) {
389+
$programName = $command;
390+
$funcName = sprintf('_%s_%s_complete',
391+
basename($programName),
392+
substr(md5($command), 0, 12)
393+
);
394+
} else {
395+
$funcName = "_{$programName}complete";
396+
}
383397

384398
return <<<"END"
385399
function $funcName {

0 commit comments

Comments
 (0)