p-sw e222b9f7a2 feat(cli): improve search command and user interaction
The search command in the CLI is enhanced to recognize search queries as command arguments. An improved interaction with the search box is also introduced, including key event handlers for up and down arrows and  escape key, allowing users to navigate more intuitively. keyboard-based selection of suggestions is now implemented and the helper message is updated to match these interaction changes.
2024-06-07 21:54:34 +09:00

36 lines
1.0 KiB
TypeScript

import {Command, Args} from '@oclif/core'
import {render} from 'ink'
import {SearchBox} from '../components/SearchBox.js'
import {getAvailableComponentNames, getRegistry} from '../helpers/registry.js'
import React from 'react'
export default class Search extends Command {
static override args = {
query: Args.string({description: 'search query'}),
}
static override description = 'Search components.'
static override examples = ['<%= config.bin %> <%= command.id %>']
public async run(): Promise<void> {
const {args} = await this.parse(Search)
const registryResult = await getRegistry()
if (!registryResult.ok) {
this.error(registryResult.message)
}
const registry = registryResult.registry
const componentNames = await getAvailableComponentNames(registry)
await render(
<SearchBox
components={componentNames}
initialQuery={args.query}
helper={'Press ESC to quit'}
onKeyDown={(_, k, app) => k.escape && app.exit()}
/>,
).waitUntilExit()
}
}