New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zsh auto completion #1292
Merged
Merged
zsh auto completion #1292
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f058d06
WIP zsh
trevorlinton 40be95d
Added initial skeleton test for completion script
trevorlinton 48f62fa
Adding tests for zsh, adds description to zsh auto-complete
trevorlinton aab0387
Fixed docs and command name to include zsh as well as bash
trevorlinton File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
###-begin-{{app_name}}-completions-### | ||
# | ||
# yargs command completion script | ||
# | ||
# Installation: {{app_path}} {{completion_command}} >> ~/.zshrc | ||
# or {{app_path}} {{completion_command}} >> ~/.zsh_profile on OSX. | ||
# | ||
_{{app_name}}_yargs_completions() | ||
{ | ||
local reply | ||
local si=$IFS | ||
IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} --get-yargs-completions "$\{words[@]\}")) | ||
IFS=$si | ||
_describe 'values' reply | ||
} | ||
compdef _{{app_name}}_yargs_completions {{app_name}} | ||
###-end-{{app_name}}-completions-### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ describe('Completion', () => { | |
}) | ||
|
||
describe('default completion behavior', () => { | ||
it('it returns a list of commands as completion suggestions', () => { | ||
it('it returns a list of commands as completion suggestions (bash)', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', '']) | ||
.command('foo', 'bar') | ||
.command('apple', 'banana') | ||
|
@@ -26,7 +27,21 @@ describe('Completion', () => { | |
r.logs.should.include('foo') | ||
}) | ||
|
||
it('avoids interruption from command recommendations', () => { | ||
it('it returns a list of commands as completion suggestions (zsh)', () => { | ||
process.env.SHELL = '/bin/zsh' | ||
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', '']) | ||
.command('foo', 'bar') | ||
.command('apple', 'banana') | ||
.completion() | ||
.argv | ||
) | ||
|
||
r.logs.should.include('apple:banana') | ||
r.logs.should.include('foo:bar') | ||
}) | ||
|
||
it('avoids interruption from command recommendations (bash)', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => | ||
yargs(['./completion', '--get-yargs-completions', './completion', 'a']) | ||
.command('apple', 'fruit') | ||
|
@@ -41,7 +56,24 @@ describe('Completion', () => { | |
r.logs.should.include('aardvark') | ||
}) | ||
|
||
it('avoids interruption from default command', () => { | ||
it('avoids interruption from command recommendations (zsh)', () => { | ||
process.env.SHELL = '/bin/zsh' | ||
const r = checkUsage(() => | ||
yargs(['./completion', '--get-yargs-completions', './completion', 'a']) | ||
.command('apple', 'fruit') | ||
.command('aardvark', 'animal') | ||
.recommendCommands() | ||
.completion() | ||
.argv | ||
) | ||
|
||
r.errors.should.deep.equal([]) | ||
r.logs.should.include('apple:fruit') | ||
r.logs.should.include('aardvark:animal') | ||
}) | ||
|
||
it('avoids interruption from default command (bash)', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => | ||
yargs(['./usage', '--get-yargs-completions', './usage', '']) | ||
.usage('$0 [thing]', 'skipped', subYargs => { | ||
|
@@ -57,7 +89,25 @@ describe('Completion', () => { | |
r.logs.should.include('aardvark') | ||
}) | ||
|
||
it('avoids interruption from default command (zsh)', () => { | ||
process.env.SHELL = '/bin/zsh' | ||
const r = checkUsage(() => | ||
yargs(['./usage', '--get-yargs-completions', './usage', '']) | ||
.usage('$0 [thing]', 'skipped', subYargs => { | ||
subYargs.command('aardwolf', 'is a thing according to google') | ||
}) | ||
.command('aardvark', 'animal') | ||
.completion() | ||
.argv | ||
) | ||
|
||
r.errors.should.deep.equal([]) | ||
r.logs.should.not.include('aardwolf') | ||
r.logs.should.include('aardvark:animal') | ||
}) | ||
|
||
it('avoids repeating already included commands', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'apple']) | ||
.command('foo', 'bar') | ||
.command('apple', 'banana') | ||
|
@@ -96,7 +146,8 @@ describe('Completion', () => { | |
r.logs.should.not.include('--foo') | ||
}) | ||
|
||
it('completes options for a command', () => { | ||
it('completes options for a command (bash)', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'foo', '--b']) | ||
.command('foo', 'foo command', subYargs => subYargs.options({ | ||
bar: { | ||
|
@@ -114,7 +165,27 @@ describe('Completion', () => { | |
r.logs.should.include('--help') | ||
}) | ||
|
||
it('completes options for a command (zsh)', () => { | ||
process.env.SHELL = '/bin/zsh' | ||
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'foo', '--b']) | ||
.command('foo', 'foo command', subYargs => subYargs.options({ | ||
bar: { | ||
describe: 'bar option' | ||
} | ||
}) | ||
.help(true) | ||
.version(false)) | ||
.completion() | ||
.argv | ||
) | ||
|
||
r.logs.should.have.length(2) | ||
r.logs.should.include('--bar:bar option') | ||
r.logs.should.include('--help:__yargsString__:Show help') | ||
}) | ||
|
||
it('completes options for the correct command', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'cmd2', '--o']) | ||
.help(false) | ||
.version(false) | ||
|
@@ -141,6 +212,7 @@ describe('Completion', () => { | |
}) | ||
|
||
it('does not complete hidden commands', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'cmd']) | ||
.command('cmd1', 'first command') | ||
.command('cmd2', false) | ||
|
@@ -153,6 +225,7 @@ describe('Completion', () => { | |
}) | ||
|
||
it('does not include possitional arguments', function () { | ||
process.env.SHELL = '/bin/bash' | ||
var r = checkUsage(function () { | ||
return yargs(['./completion', '--get-yargs-completions', 'cmd']) | ||
.command('cmd1 [arg]', 'first command') | ||
|
@@ -167,6 +240,7 @@ describe('Completion', () => { | |
}) | ||
|
||
it('works if command has no options', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'foo', '--b']) | ||
.help(false) | ||
.version(false) | ||
|
@@ -181,6 +255,7 @@ describe('Completion', () => { | |
}) | ||
|
||
it("returns arguments as completion suggestion, if next contains '-'", () => { | ||
process.env.SHELL = '/bin/basg' | ||
const r = checkUsage(() => yargs(['./usage', '--get-yargs-completions', '-f']) | ||
.option('foo', { | ||
describe: 'foo option' | ||
|
@@ -196,10 +271,21 @@ describe('Completion', () => { | |
}) | ||
|
||
describe('generateCompletionScript()', () => { | ||
it('replaces application variable with $0 in script', () => { | ||
it('replaces application variable with $0 in script (bash)', () => { | ||
process.env.SHELL = '/bin/bash' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this approach 👍 |
||
const r = checkUsage(() => yargs([]) | ||
.showCompletionScript(), ['ndm']) | ||
|
||
r.logs[0].should.match(/bashrc/) | ||
r.logs[0].should.match(/ndm --get-yargs-completions/) | ||
}) | ||
|
||
it('replaces application variable with $0 in script (zsh)', () => { | ||
process.env.SHELL = '/bin/zsh' | ||
const r = checkUsage(() => yargs([]) | ||
.showCompletionScript(), ['ndm']) | ||
|
||
r.logs[0].should.match(/zshrc/) | ||
r.logs[0].should.match(/ndm --get-yargs-completions/) | ||
}) | ||
|
||
|
@@ -299,8 +385,9 @@ describe('Completion', () => { | |
}) | ||
}) | ||
|
||
describe('getCompletion()', () => { | ||
describe('getCompletion() - bash', () => { | ||
it('returns default completion to callback', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => { | ||
yargs() | ||
.command('foo', 'bar') | ||
|
@@ -318,8 +405,29 @@ describe('Completion', () => { | |
}) | ||
}) | ||
|
||
describe('getCompletion() - zsh', () => { | ||
it('returns default completion to callback', () => { | ||
process.env.SHELL = '/bin/zsh' | ||
const r = checkUsage(() => { | ||
yargs() | ||
.command('foo', 'bar') | ||
.command('apple', 'banana') | ||
.completion() | ||
.getCompletion([''], (completions) => { | ||
;(completions || []).forEach((completion) => { | ||
console.log(completion) | ||
}) | ||
}) | ||
}) | ||
|
||
r.logs.should.include('apple:banana') | ||
r.logs.should.include('foo:bar') | ||
}) | ||
}) | ||
|
||
// fixes for #177. | ||
it('does not apply validation when --get-yargs-completions is passed in', () => { | ||
it('does not apply validation when --get-yargs-completions is passed in (bash)', () => { | ||
process.env.SHELL = '/bin/bash' | ||
const r = checkUsage(() => { | ||
try { | ||
return yargs(['./completion', '--get-yargs-completions', '--']) | ||
|
@@ -335,4 +443,21 @@ describe('Completion', () => { | |
r.errors.length.should.equal(0) | ||
r.logs.should.include('--foo') | ||
}) | ||
it('does not apply validation when --get-yargs-completions is passed in (zsh)', () => { | ||
process.env.SHELL = '/bin/zsh' | ||
const r = checkUsage(() => { | ||
try { | ||
return yargs(['./completion', '--get-yargs-completions', '--']) | ||
.option('foo', {'describe': 'bar'}) | ||
.completion() | ||
.strict() | ||
.argv | ||
} catch (e) { | ||
console.log(e.message) | ||
} | ||
}) | ||
|
||
r.errors.length.should.equal(0) | ||
r.logs.should.include('--foo:bar') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting, at the end of the day this isn't too far off bash completion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, not to much of a difference really.