Skip to content

Commit

Permalink
feat: integrate usage guide
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Mar 20, 2021
1 parent 9538d25 commit 2af585c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/commands/completion/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CompletionBase } from '../../base'
import { getInstructionsForBash } from '../../utils/bash'
import { getInstructionsForFish } from '../../utils/fish'
import { getInstructionsForZsh } from '../../utils/zsh'

export default class Completion extends CompletionBase {
static description = 'completion plugin'
Expand All @@ -13,13 +16,23 @@ export default class Completion extends CompletionBase {

async run() {
const { flags } = this.parse(Completion)
const { bin } = this.config
const shell = flags.shell

this.log(
[
`Run the following command to generate completion script for ${shell} shell:`,
`$ ${this.config.bin} completion:generate --shell=${shell}`,
].join('\n')
)
let instructions: string[] = []

if (shell === 'bash') {
instructions = getInstructionsForBash({ bin, shell })
}

if (shell === 'fish') {
instructions = getInstructionsForFish({ bin, shell })
}

if (shell === 'zsh') {
instructions = getInstructionsForZsh({ bin, shell })
}

this.log(instructions.join('\n'))
}
}
33 changes: 33 additions & 0 deletions src/utils/bash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,36 @@ export function generateCompletionScriptForBash({

return scriptParts.join('\n'.repeat(2))
}

export function getInstructionsForBash({
bin,
shell,
}: {
bin: string
shell: string
}): string[] {
const scriptName = bin

const lines = [
`Make sure you have the "bash-completion" package installed on your system.`,
``,
`Running the following command will generate the completion script for ${shell} shell:`,
``,
` $ ${bin} completion:generate --shell=${shell} > ${scriptName}`,
``,
`You need to put that "${scriptName}" file in one of the following directories (depending on your system):`,
``,
`- $XDG_DATA_HOME/bash-completion/completions`,
`- ~/.local/share/bash-completion/completions`,
`- /usr/local/share/bash-completion/completions`,
`- /usr/share/bash-completion/completions`,
``,
`Usually this should work:`,
``,
` $ ${bin} completion:generate --shell=${shell} > ~/.local/share/bash-completion/completions/${scriptName}`,
``,
`Enjoy!`,
]

return lines
}
27 changes: 27 additions & 0 deletions src/utils/fish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,30 @@ export function generateCompletionScriptForFish({

return scriptParts.join('\n'.repeat(2))
}

export function getInstructionsForFish({
bin,
shell,
}: {
bin: string
shell: string
}): string[] {
const scriptName = `${bin}.fish`

const lines = [
`Running the following command will generate the completion script for ${shell} shell:`,
``,
` $ ${bin} completion:generate --shell=${shell} > ${scriptName}`,
``,
`You need to put that "${scriptName}" file in one of the directories present in "$fish_complete_path" environment variable:`,
``,
` $ echo $fish_complete_path`,
``,
`Usually this should work:`,
``,
` $ ${bin} completion:generate --shell=${shell} > ~/.config/fish/completions/${scriptName}`,
``,
`Enjoy!`,
]
return lines
}
27 changes: 27 additions & 0 deletions src/utils/zsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,30 @@ function _${bin}_${command.id} {

return scriptParts.join('\n'.repeat(2))
}

export function getInstructionsForZsh({
bin,
shell,
}: {
bin: string
shell: string
}): string[] {
const scriptName = `_${bin}`

const lines = [
`Running the following command will generate the completion script for ${shell} shell:`,
``,
` $ ${bin} completion:generate --shell=${shell} > ${scriptName}`,
``,
`You need to put that "${scriptName}" file in one of the directories present in "$FPATH" environment variable:`,
``,
` $ echo $FPATH`,
``,
`Usually this should work by automatically find an appropriate directory for you:`,
``,
` $ ${bin} completion:generate --shell=${shell} > "$(echo \${FPATH} | tr ':' '\\n' | grep site-functions | head -n1)/${scriptName}"`,
``,
`Enjoy!`,
]
return lines
}

0 comments on commit 2af585c

Please sign in to comment.