From 055d4cc1d9e957eecea17338bb24d1fb7c9fbf8b Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Tue, 30 Jan 2024 13:56:31 -0300 Subject: [PATCH] chore: update help text --- src/commands/autocomplete/index.ts | 118 +++++++++++++++-------- test/commands/autocomplete/index.test.ts | 35 +------ 2 files changed, 78 insertions(+), 75 deletions(-) diff --git a/src/commands/autocomplete/index.ts b/src/commands/autocomplete/index.ts index fa73074d..72a95cb8 100644 --- a/src/commands/autocomplete/index.ts +++ b/src/commands/autocomplete/index.ts @@ -5,30 +5,6 @@ import {EOL} from 'node:os' import {AutocompleteBase} from '../../base.js' import Create from './create.js' -const noteFromShell = (shell: string) => { - switch (shell) { - case 'zsh': { - return `After sourcing, you can run \`${chalk.cyan( - '$ compaudit -D', - )}\` to ensure no permissions conflicts are present` - } - - case 'bash': { - return 'If your terminal starts as a login shell you may need to print the init script into ~/.bash_profile or ~/.profile.' - } - - case 'powershell': { - return `Use the \`MenuComplete\` mode to get matching completions printed below the command line:\n${chalk.cyan( - 'Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete', - )}` - } - - default: { - return '' - } - } -} - export default class Index extends AutocompleteBase { static args = { shell: Args.string({ @@ -67,32 +43,90 @@ export default class Index extends AutocompleteBase { ux.action.stop() if (!flags['refresh-cache']) { - const {bin} = this.config - const tabStr = shell === 'bash' ? '' : '' + this.printShellInstructions(shell) + } + } + + private printShellInstructions(shell: string): void { + const binUpcase = this.cliBinEnvVar + const shellUpcase = shell.toUpperCase() + const tabStr = shell === 'bash' ? '' : '' + const scriptCommand = `${this.config.bin} autocomplete${this.config.topicSeparator}script ${shell}` + + let instructions = ` +Setup Instructions for ${this.config.bin.toUpperCase()} CLI Autocomplete --- +============================================== +` + + switch (shell) { + case 'bash': { + instructions += ` +1) Run this command (starting with "printf") in your terminal window: - const instructions = - shell === 'powershell' - ? `New-Item -Type Directory -Path (Split-Path -Parent $PROFILE) -ErrorAction SilentlyContinue -Add-Content -Path $PROFILE -Value (Invoke-Expression -Command "${bin} autocomplete${this.config.topicSeparator}script ${shell}"); .$PROFILE` - : `$ printf "eval $(${bin} autocomplete${this.config.topicSeparator}script ${shell})" >> ~/.${shell}rc; source ~/.${shell}rc` + ${chalk.cyan(`$ printf "eval $(${scriptCommand})" >> ~/.bashrc; source ~/.bashrc`)} - const note = noteFromShell(shell) + The previous command adds the ${chalk.cyan(`${binUpcase}_AC_${shellUpcase}_SETUP_PATH`)} environment variable to your Bash config file and then sources the file. - this.log(` -${chalk.bold(`Setup Instructions for ${bin.toUpperCase()} CLI Autocomplete ---`)} + NOTE: If you’ve configured your terminal to start as a login shell, you may need to modify the command so it updates either the ~/.bash_profile or ~/.profile file. For example: -1) Add the autocomplete ${shell === 'powershell' ? 'file' : 'env var'} to your ${shell} profile and source it + ${chalk.cyan(`$ printf "eval $(${scriptCommand}) >> ~/.bash_profile; source ~/.bash_profile`)} + + Or: -${chalk.cyan(instructions)} + ${chalk.cyan(`$ printf "eval $(${scriptCommand})" >> ~/.profile; source ~/.profile`)} -${chalk.bold('NOTE')}: ${note} +2) Start using autocomplete: -2) Test it out, e.g.: -${chalk.cyan(`$ ${bin} ${tabStr}`)} # Command completion -${chalk.cyan(`$ ${bin} command --${tabStr}`)} # Flag completion + ${chalk.cyan(`$ sf ${tabStr}`)} # Command completion + ${chalk.cyan(`$ sf command --${tabStr}`)} # Flag completion + ` + break + } -Enjoy! -`) + case 'zsh': { + instructions += ` +1) Run this command (starting with "printf") in your terminal window: + + ${chalk.cyan(`$ printf "eval $(${scriptCommand})" >> ~/.zshrc; source ~/.zshrc`)} + + The previous command adds the ${chalk.cyan(`${binUpcase}_AC_${shellUpcase}_SETUP_PATH`)} environment variable to your zsh config file and then sources the file. + +2) (Optional) Run this command to ensure that you have no permissions conflicts: + + ${chalk.cyan('$ compaudit -D')} + +3) Start using autocomplete: + + ${chalk.cyan(`$ sf ${tabStr}`)} # Command completion + ${chalk.cyan(`$ sf command --${tabStr}`)} # Flag completion + ` + break + } + + case 'powershell': { + instructions += ` +1) Run these two cmdlets in your PowerShell window in the order shown: + + ${chalk.cyan(`New-Item -Type Directory -Path (Split-Path -Parent $PROFILE) -ErrorAction SilentlyContinue + Add-Content -Path $PROFILE -Value (Invoke-Expression -Command "${scriptCommand}"); .$PROFILE`)} + +2) (Optional) If you want matching completions printed below the command line, run this cmdlet: + + ${chalk.cyan('Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete')} + +3) Start using autocomplete: + + ${chalk.cyan(`$ sf ${tabStr}`)} # Command completion + ${chalk.cyan(`$ sf command --${tabStr}`)} # Flag completion + ` + break + } } + + instructions += ` + Every time you enter ${tabStr}, the autocomplete feature displays a list of commands (or flags if you type --), along with their summaries. Enter a letter and then ${tabStr} again to narrow down the list until you end up with the complete command that you want to execute. + + Enjoy!` + this.log(instructions) } } diff --git a/test/commands/autocomplete/index.test.ts b/test/commands/autocomplete/index.test.ts index 1b3c4335..01b281b2 100644 --- a/test/commands/autocomplete/index.test.ts +++ b/test/commands/autocomplete/index.test.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-useless-escape */ import {expect, test} from '@oclif/test' // autocomplete will throw error on windows ci @@ -9,43 +8,13 @@ skipWindows('autocomplete index', () => { .stdout() .command(['autocomplete', 'bash']) .it('provides bash instructions', (ctx) => { - expect(ctx.stdout).to.contain(` -Setup Instructions for OCLIF-EXAMPLE CLI Autocomplete --- - -1) Add the autocomplete env var to your bash profile and source it - -$ printf \"eval $(oclif-example autocomplete:script bash)\" >> ~/.bashrc; source ~/.bashrc - -NOTE: If your terminal starts as a login shell you may need to print the init script into ~/.bash_profile or ~/.profile. - -2) Test it out, e.g.: -$ oclif-example # Command completion -$ oclif-example command -- # Flag completion - -Enjoy! - -`) + expect(ctx.stdout).to.contain(`Setup Instructions for OCLIF-EXAMPLE CLI Autocomplete ---`) }) test .stdout() .command(['autocomplete', 'zsh']) .it('provides zsh instructions', (ctx) => { - expect(ctx.stdout).to.contain(` -Setup Instructions for OCLIF-EXAMPLE CLI Autocomplete --- - -1) Add the autocomplete env var to your zsh profile and source it - -$ printf \"eval $(oclif-example autocomplete:script zsh)\" >> ~/.zshrc; source ~/.zshrc - -NOTE: After sourcing, you can run \`$ compaudit -D\` to ensure no permissions conflicts are present - -2) Test it out, e.g.: -$ oclif-example # Command completion -$ oclif-example command -- # Flag completion - -Enjoy! - -`) + expect(ctx.stdout).to.contain(`Setup Instructions for OCLIF-EXAMPLE CLI Autocomplete ---`) }) })