mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
section 4: Phase 4: Prompt and multi-select dialog helpers
This commit is contained in:
1 parent
a99e69f653
commit
87e55372c0
2 files changed
+225
-4
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import type { Context } from '@opencode/plugin/tui/context'
|
||||
import type { Context, DialogPromptOptions } from '@opencode/plugin/tui/context'
|
||||
|
||||
const DONE = -1
|
||||
|
||||
export function confirmDialog(context: Context, props: { title: string; message: string }): Promise<boolean> {
|
||||
return context.ui.dialog.confirm(props).then((value) => value === true)
|
||||
@@ -11,3 +13,61 @@ export function selectDialog<Value>(
|
||||
): Promise<Value | undefined> {
|
||||
return context.ui.dialog.select({ title, options })
|
||||
}
|
||||
|
||||
export function promptDialog(context: Context, props: DialogPromptOptions): Promise<string | undefined> {
|
||||
return context.ui.dialog.prompt(props).then((value) => {
|
||||
const trimmed = value?.trim()
|
||||
return trimmed ? trimmed : undefined
|
||||
})
|
||||
}
|
||||
|
||||
export async function multiSelectDialog<Value>(
|
||||
context: Context,
|
||||
props: {
|
||||
title: string
|
||||
options: { title: string; description?: string; value: Value }[]
|
||||
min: number
|
||||
max: number
|
||||
},
|
||||
): Promise<Value[] | undefined> {
|
||||
const selected = new Set<number>()
|
||||
let current: number | undefined
|
||||
|
||||
for (;;) {
|
||||
const options = [
|
||||
{
|
||||
title: `Done (${selected.size} selected)`,
|
||||
value: DONE,
|
||||
disabled: selected.size < props.min,
|
||||
},
|
||||
...props.options.map((option, index) => ({
|
||||
title: `${selected.has(index) ? '[x]' : '[ ]'} ${option.title}`,
|
||||
description: option.description,
|
||||
value: index,
|
||||
disabled: !selected.has(index) && selected.size >= props.max,
|
||||
})),
|
||||
]
|
||||
|
||||
const choice = await context.ui.dialog.select({ title: props.title, options, current })
|
||||
|
||||
if (choice === undefined) return undefined
|
||||
if (choice === DONE) {
|
||||
return props.options.filter((_, index) => selected.has(index)).map((option) => option.value)
|
||||
}
|
||||
|
||||
current = choice
|
||||
if (selected.has(choice)) {
|
||||
selected.delete(choice)
|
||||
} else {
|
||||
selected.add(choice)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function slashArgument(input: string | undefined, name: string): string {
|
||||
const trimmed = (input ?? '').trim()
|
||||
const prefix = `/${name}`
|
||||
if (trimmed === prefix) return ''
|
||||
if (trimmed.startsWith(`${prefix} `)) return trimmed.slice(prefix.length).trim()
|
||||
return trimmed
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import type { Context } from '@opencode/plugin/tui/context'
|
||||
import { confirmDialog, selectDialog } from '../src/tui-dialogs.js'
|
||||
import { confirmDialog, multiSelectDialog, promptDialog, selectDialog, slashArgument } from '../src/tui-dialogs.js'
|
||||
|
||||
function createFakeContext() {
|
||||
const confirm = vi.fn()
|
||||
const select = vi.fn()
|
||||
const prompt = vi.fn()
|
||||
const context = {
|
||||
ui: { dialog: { confirm, select } },
|
||||
ui: { dialog: { confirm, select, prompt } },
|
||||
} as unknown as Context
|
||||
return { context, confirm, select }
|
||||
return { context, confirm, select, prompt }
|
||||
}
|
||||
|
||||
describe('confirmDialog', () => {
|
||||
@@ -55,3 +56,163 @@ describe('selectDialog', () => {
|
||||
expect(await selectDialog(context, 'Pick', [{ title: 'X', value: 42 }])).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('promptDialog', () => {
|
||||
it('forwards the options and resolves the trimmed value', async () => {
|
||||
const { context, prompt } = createFakeContext()
|
||||
prompt.mockResolvedValue(' fix it ')
|
||||
|
||||
const props = { title: 'Goal', description: 'What do you want?', placeholder: 'Describe it', value: 'seed' }
|
||||
|
||||
expect(await promptDialog(context, props)).toBe('fix it')
|
||||
expect(prompt).toHaveBeenCalledWith(props)
|
||||
})
|
||||
|
||||
it('resolves undefined when the dialog is dismissed', async () => {
|
||||
const { context, prompt } = createFakeContext()
|
||||
prompt.mockResolvedValue(undefined)
|
||||
|
||||
expect(await promptDialog(context, { title: 'Goal' })).toBeUndefined()
|
||||
})
|
||||
|
||||
it('resolves undefined when the value is blank', async () => {
|
||||
const { context, prompt } = createFakeContext()
|
||||
prompt.mockResolvedValue(' ')
|
||||
|
||||
expect(await promptDialog(context, { title: 'Goal' })).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('multiSelectDialog', () => {
|
||||
const options = [
|
||||
{ title: 'A', value: 'a' },
|
||||
{ title: 'B', value: 'b' },
|
||||
{ title: 'C', value: 'c' },
|
||||
]
|
||||
|
||||
it('resolves selected values in option order', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(0).mockResolvedValueOnce(1).mockResolvedValueOnce(-1)
|
||||
|
||||
expect(await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })).toEqual(['a', 'b'])
|
||||
expect(select).toHaveBeenCalledTimes(3)
|
||||
})
|
||||
|
||||
it('disables the done entry while fewer than min are selected', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(0).mockResolvedValueOnce(-1)
|
||||
|
||||
await multiSelectDialog(context, { title: 'Pick', options, min: 2, max: 3 })
|
||||
|
||||
expect(select.mock.calls[0][0].options[0]).toEqual({ title: 'Done (0 selected)', value: -1, disabled: true })
|
||||
expect(select.mock.calls[1][0].options[0]).toEqual({ title: 'Done (1 selected)', value: -1, disabled: true })
|
||||
})
|
||||
|
||||
it('enables the done entry once min are selected', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(0).mockResolvedValueOnce(-1)
|
||||
|
||||
await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })
|
||||
|
||||
expect(select.mock.calls[1][0].options[0]).toEqual({ title: 'Done (1 selected)', value: -1, disabled: false })
|
||||
})
|
||||
|
||||
it('disables unselected options once max are selected', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(0).mockResolvedValueOnce(-1)
|
||||
|
||||
await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 1 })
|
||||
|
||||
const second = select.mock.calls[1][0].options
|
||||
expect(second[1]).toMatchObject({ title: '[x] A', value: 0, disabled: false })
|
||||
expect(second[2]).toMatchObject({ title: '[ ] B', value: 1, disabled: true })
|
||||
expect(second[3]).toMatchObject({ title: '[ ] C', value: 2, disabled: true })
|
||||
})
|
||||
|
||||
it('marks selected options with an x marker', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(1).mockResolvedValueOnce(-1)
|
||||
|
||||
await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })
|
||||
|
||||
const first = select.mock.calls[0][0].options
|
||||
expect(first[1]).toMatchObject({ title: '[ ] A' })
|
||||
expect(first[2]).toMatchObject({ title: '[ ] B' })
|
||||
const second = select.mock.calls[1][0].options
|
||||
expect(second[1]).toMatchObject({ title: '[ ] A' })
|
||||
expect(second[2]).toMatchObject({ title: '[x] B' })
|
||||
})
|
||||
|
||||
it('keeps the cursor on the last toggled entry', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(2).mockResolvedValueOnce(0).mockResolvedValueOnce(-1)
|
||||
|
||||
await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })
|
||||
|
||||
expect(select.mock.calls[0][0].current).toBeUndefined()
|
||||
expect(select.mock.calls[1][0].current).toBe(2)
|
||||
expect(select.mock.calls[2][0].current).toBe(0)
|
||||
})
|
||||
|
||||
it('toggles a selected option off', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(0).mockResolvedValueOnce(1).mockResolvedValueOnce(0).mockResolvedValueOnce(-1)
|
||||
|
||||
expect(await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })).toEqual(['b'])
|
||||
})
|
||||
|
||||
it('resolves undefined when dismissed before any selection', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(undefined)
|
||||
|
||||
expect(await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })).toBeUndefined()
|
||||
})
|
||||
|
||||
it('resolves undefined when dismissed after a selection', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(0).mockResolvedValueOnce(undefined)
|
||||
|
||||
expect(await multiSelectDialog(context, { title: 'Pick', options, min: 1, max: 3 })).toBeUndefined()
|
||||
})
|
||||
|
||||
it('passes option descriptions through', async () => {
|
||||
const { context, select } = createFakeContext()
|
||||
select.mockResolvedValueOnce(-1)
|
||||
|
||||
await multiSelectDialog(context, {
|
||||
title: 'Pick',
|
||||
options: [{ title: 'A', description: 'first', value: 'a' }],
|
||||
min: 0,
|
||||
max: 1,
|
||||
})
|
||||
|
||||
expect(select.mock.calls[0][0].options[1]).toEqual({
|
||||
title: '[ ] A',
|
||||
description: 'first',
|
||||
value: 0,
|
||||
disabled: false,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('slashArgument', () => {
|
||||
it('resolves an empty string for undefined input', () => {
|
||||
expect(slashArgument(undefined, 'goal')).toBe('')
|
||||
})
|
||||
|
||||
it('strips a leading slash token and trims the rest', () => {
|
||||
expect(slashArgument('/goal fix it ', 'goal')).toBe('fix it')
|
||||
})
|
||||
|
||||
it('returns the trimmed argument when no slash token is present', () => {
|
||||
expect(slashArgument(' fix it ', 'goal')).toBe('fix it')
|
||||
})
|
||||
|
||||
it('only strips a whole-token match', () => {
|
||||
expect(slashArgument('/goalie x', 'goal')).toBe('/goalie x')
|
||||
})
|
||||
|
||||
it('resolves an empty string when only the slash token is present', () => {
|
||||
expect(slashArgument('/goal', 'goal')).toBe('')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user