Skip to content

Commit

Permalink
feat: ux improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jun 20, 2024
1 parent 7ebe253 commit 87e5969
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/commands/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import {Command, loadHelpClass, toConfiguredId, toStandardizedId} from '@oclif/core'
import {Command, loadHelpClass, toConfiguredId, toStandardizedId, ux} from '@oclif/core'
import autocomplete from 'inquirer-autocomplete-standalone'
import readline from 'node:readline'

export default class Search extends Command {
public static description = 'Once you select a command, hit enter and it will show the help for that command.'
public static summary = 'Search for a command.'

public async run(): Promise<unknown> {
this.log(`Interactively search the catalog of ${this.config.bin} commands.
Use ${ux.colorize('bold', '↑')} and ${ux.colorize('bold', '↓')} keys or type to search. Press ${ux.colorize('bold', 'ESC')} to exit.
`)
const commandChoices = this.config.commands
.filter((c) => !c.hidden && !c.aliases.includes(c.id))
.sort((a, b) => a.id.localeCompare(b.id))
Expand All @@ -25,15 +29,32 @@ export default class Search extends Command {
}
})

const command = await autocomplete<string>({
const commandPromise = autocomplete<string>({
emptyText: 'Nothing found!',
message: 'Search for a command',
pageSize: 10,
pageSize: Math.floor(process.stdout.rows < 20 ? process.stdout.rows / 2 : 10),
async source(input) {
return input ? commandChoices.filter((c) => c.name.includes(input)) : commandChoices
},
})

readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
process.stdin.on('keypress', (_, key) => {
if (key.name === 'escape') {
commandPromise.cancel()
}
})

const command = await commandPromise
.catch((error) => {
if (error.message === 'Prompt was canceled') return
throw error
})
.then((result) => result)

if (!command) return

const Help = await loadHelpClass(this.config)
const help = new Help(this.config, this.config.pjson.oclif.helpOptions ?? this.config.pjson.helpOptions)
return help.showHelp([toStandardizedId(command, this.config)])
Expand Down

0 comments on commit 87e5969

Please sign in to comment.