mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +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
|
||||
}
|
||||
Reference in new issue
Block a user