Skip to content
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 4 commits into from Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions completion.zsh.hbs
@@ -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[@]\}"))
Copy link
Member

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.

Copy link
Contributor Author

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.

IFS=$si
_describe 'values' reply
}
compdef _{{app_name}}_yargs_completions {{app_name}}
###-end-{{app_name}}-completions-###
4 changes: 3 additions & 1 deletion lib/completion.js
Expand Up @@ -79,8 +79,10 @@ module.exports = function completion (yargs, usage, command) {

// generate the completion script to add to your .bashrc.
self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
let zshShell = process.env.SHELL && process.env.SHELL.indexOf('zsh') !== -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this could be const and not let.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.


let script = fs.readFileSync(
path.resolve(__dirname, '../completion.sh.hbs'),
path.resolve(__dirname, zshShell ? '../completion.zsh.hbs' : '../completion.sh.hbs'),
'utf-8'
)
const name = path.basename($0)
Expand Down
13 changes: 12 additions & 1 deletion test/completion.js
Expand Up @@ -196,10 +196,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'
Copy link
Member

Choose a reason for hiding this comment

The 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/)
})

Expand Down