diff --git a/src/lib/promptsmith/select-options.ts b/src/lib/promptsmith/select-options.ts index 965a1bd..4b756fd 100644 --- a/src/lib/promptsmith/select-options.ts +++ b/src/lib/promptsmith/select-options.ts @@ -6,8 +6,13 @@ import { type PainterTask, task as TASK, } from '../painter/lifecycle' -import { type AnsiBufferKey } from '../util/ansi' -import { DoEither, type Either, right } from '../util/basic/either' +import { + type AnsiBufferKey, + ansiText, + emptyBytes, + writeAnsiU, +} from '../util/ansi' +import { DoEither, type Either, left, right } from '../util/basic/either' import { some } from '../util/basic/option' import type { Tag } from '../util/basic/utility' @@ -16,9 +21,10 @@ interface SelectOptions { keydown: Array exit: Array accept: Array - selector: string + selector: {selected: string, default: string} + selected: number options: Array - header: {text: string, format: Array} + header?: {text: string, format: Array} format: { text: { default: Array, @@ -43,22 +49,36 @@ const EVENT: Record Event)> = { type RenderText = (x: SelectOptions & Tag<'PainterState'>) => Promise> -// const renderText: RenderText = x => -// -// const selectOptions: PainterEffect = x => -// DoEither(right(x)).start() -// .bind('') - -const test = async () => { - const myPromise = new Promise((resolve, reject) => { - setTimeout(() => { - resolve('First') - }, 300) - }) - - console.log('Second') - console.log(await myPromise) - return some(await myPromise + 'Third') +const renderText: RenderText = async (x) => { + try { + return right( + await writeAnsiU([ + ...(!x.header ? [emptyBytes] + : [...x.header.format, ansiText(x.header.text)]), + ...x.options.flatMap(i => + (x.options[x.selected] === i) + ? [ + ...x.format.selector.defaultSelected, + ansiText(x.selector.selected), + 'RESET' as AnsiBufferKey, + ...x.format.text.defaultSelected, + ansiText(i), + 'RESET' as AnsiBufferKey, + ] : [ + ...x.format.selector.default, + ansiText(x.selector.default), + 'RESET' as AnsiBufferKey, + ...x.format.text.default, + ansiText(i), + 'RESET' as AnsiBufferKey, + ] + ), + ]), + ) + } catch (e) { + return left(e as Error) + } } -console.log(await test()) +const selectOptions: PainterEffect = x => { +}