Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
fix prompt styles
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 committed Nov 30, 2023
1 parent ca45e4f commit 90f55ed
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 46 deletions.
2 changes: 1 addition & 1 deletion examples/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const textPrompt = Prompt.text({
})

const togglePrompt = Prompt.toggle({
message: "Can you confirm?",
message: "Yes or no?",
active: "yes",
inactive: "no"
})
Expand Down
8 changes: 4 additions & 4 deletions src/internal/prompt/confirm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ const renderNextFrame = (
const terminal = yield* _(Terminal.Terminal)
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(prevState, options, terminal.columns)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyan)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.black)
const confirmAnnotation = Ansi.black
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyanBright)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.blackBright)
const confirmAnnotation = Ansi.blackBright
// Marking these explicitly as present with `!` because they always will be
// and there is really no value in adding a `DeepRequired` type helper just
// for these internal cases
Expand Down Expand Up @@ -94,7 +94,7 @@ const renderSubmission = (
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(Option.some(nextState), options, terminal.columns)
const leadingSymbol = Doc.annotate(figures.tick, Ansi.green)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.black)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.blackBright)
const confirmMessage = value ? options.label.confirm : options.label.deny
const confirm = Doc.text(confirmMessage)
const promptMsg = renderOutput(confirm, leadingSymbol, trailingSymbol, options)
Expand Down
8 changes: 4 additions & 4 deletions src/internal/prompt/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const renderParts = (nextState: State, submitted: boolean = false) =>
(doc, part, currentIndex) => {
const partDoc = Doc.text(part.toString())
if (currentIndex === nextState.cursor && !submitted) {
const annotation = Ansi.combine(Ansi.underlined, Ansi.cyan)
const annotation = Ansi.combine(Ansi.underlined, Ansi.cyanBright)
return Doc.cat(doc, Doc.annotate(partDoc, annotation))
}
return Doc.cat(doc, partDoc)
Expand Down Expand Up @@ -112,8 +112,8 @@ const renderNextFrame = (
const terminal = yield* _(Terminal.Terminal)
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(prevState, options, terminal.columns)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyan)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.black)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyanBright)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.blackBright)
const parts = renderParts(nextState)
const promptMsg = renderOutput(leadingSymbol, trailingSymbol, parts, options)
const errorMsg = renderError(nextState, figures.pointerSmall)
Expand All @@ -133,7 +133,7 @@ const renderSubmission = (nextState: State, options: Required<Prompt.Prompt.Date
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(Option.some(nextState), options, terminal.columns)
const leadingSymbol = Doc.annotate(figures.tick, Ansi.green)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.black)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.blackBright)
const parts = renderParts(nextState, true)
const promptMsg = renderOutput(leadingSymbol, trailingSymbol, parts, options)
return pipe(
Expand Down
21 changes: 11 additions & 10 deletions src/internal/prompt/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ const renderClearScreen = (
return Doc.cat(clearError, Doc.cat(clearOutput, resetCurrentLine))
}

const renderInput = (nextState: State): Doc.AnsiDoc => {
const renderInput = (nextState: State, submitted: boolean): Doc.AnsiDoc => {
const annotation = Option.match(nextState.error, {
onNone: () => Ansi.combine(Ansi.underlined, Ansi.green),
onNone: () => Ansi.combine(Ansi.underlined, Ansi.cyanBright),
onSome: () => Ansi.red
})
const value = nextState.value === "" ? Doc.empty : Doc.text(`${nextState.value}`)
return Doc.annotate(value, annotation)
return submitted ? value : Doc.annotate(value, annotation)
}

const renderError = (nextState: State, pointer: Doc.AnsiDoc): Doc.AnsiDoc =>
Expand Down Expand Up @@ -89,12 +89,13 @@ const renderOutput = (
nextState: State,
leadingSymbol: Doc.AnsiDoc,
trailingSymbol: Doc.AnsiDoc,
options: Required<Prompt.Prompt.IntegerOptions>
options: Required<Prompt.Prompt.IntegerOptions>,
submitted: boolean = false
): Doc.AnsiDoc => {
const annotateLine = (line: string): Doc.AnsiDoc => Doc.annotate(Doc.text(line), Ansi.bold)
const prefix = Doc.cat(leadingSymbol, Doc.space)
return ReadonlyArray.match(options.message.split(/\r?\n/), {
onEmpty: () => Doc.hsep([prefix, trailingSymbol, renderInput(nextState)]),
onEmpty: () => Doc.hsep([prefix, trailingSymbol, renderInput(nextState, submitted)]),
onNonEmpty: (promptLines) => {
const lines = ReadonlyArray.map(promptLines, (line) => annotateLine(line))
return pipe(
Expand All @@ -103,7 +104,7 @@ const renderOutput = (
Doc.cat(Doc.space),
Doc.cat(trailingSymbol),
Doc.cat(Doc.space),
Doc.cat(renderInput(nextState))
Doc.cat(renderInput(nextState, submitted))
)
}
})
Expand All @@ -117,8 +118,8 @@ const renderNextFrame = (
Effect.gen(function*(_) {
const terminal = yield* _(Terminal.Terminal)
const figures = yield* _(InternalAnsiUtils.figures)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyan)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.black)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyanBright)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.blackBright)
const clearScreen = renderClearScreen(prevState, options, terminal.columns)
const errorMsg = renderError(nextState, figures.pointerSmall)
const promptMsg = renderOutput(nextState, leadingSymbol, trailingSymbol, options)
Expand All @@ -140,8 +141,8 @@ const renderSubmission = (
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(Option.some(nextState), options, terminal.columns)
const leadingSymbol = Doc.annotate(figures.tick, Ansi.green)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.black)
const promptMsg = renderOutput(nextState, leadingSymbol, trailingSymbol, options)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.blackBright)
const promptMsg = renderOutput(nextState, leadingSymbol, trailingSymbol, options, true)
return pipe(
clearScreen,
Doc.cat(promptMsg),
Expand Down
20 changes: 9 additions & 11 deletions src/internal/prompt/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ const renderChoicePrefix = <A>(
prefix = figures.arrowDown
}
if (choices[currentIndex].disabled) {
const annotation = Ansi.combine(Ansi.bold, Ansi.black)
const annotation = Ansi.combine(Ansi.bold, Ansi.blackBright)
return nextState.cursor === currentIndex
? pipe(figures.pointer, Doc.annotate(annotation), Doc.cat(prefix))
: pipe(prefix, Doc.cat(Doc.space))
}
return nextState.cursor === currentIndex
? pipe(figures.pointer, Doc.annotate(Ansi.green), Doc.cat(prefix))
? pipe(figures.pointer, Doc.annotate(Ansi.cyanBright), Doc.cat(prefix))
: pipe(prefix, Doc.cat(Doc.space))
}

Expand All @@ -60,15 +60,13 @@ const renderChoiceTitle = <A>(
isSelected: boolean
): Doc.AnsiDoc => {
const title = Doc.text(choice.title)
const disabledAnnotation = Ansi.combine(Ansi.strikethrough, Ansi.black)
const selectedAnnotaion = Ansi.combine(Ansi.underlined, Ansi.green)
if (isSelected) {
return choice.disabled
? Doc.annotate(title, disabledAnnotation)
: Doc.annotate(title, selectedAnnotaion)
? Doc.annotate(title, Ansi.combine(Ansi.underlined, Ansi.blackBright))
: Doc.annotate(title, Ansi.combine(Ansi.underlined, Ansi.cyanBright))
}
return choice.disabled
? Doc.annotate(title, disabledAnnotation)
? Doc.annotate(title, Ansi.combine(Ansi.strikethrough, Ansi.blackBright))
: title
}

Expand All @@ -81,7 +79,7 @@ const renderChoiceDescription = <A>(
Doc.char("-"),
Doc.cat(Doc.space),
Doc.cat(Doc.text(choice.description)),
Doc.annotate(Ansi.black)
Doc.annotate(Ansi.blackBright)
)
}
return Doc.empty
Expand Down Expand Up @@ -136,8 +134,8 @@ const renderNextFrame = <A>(
const figures = yield* _(InternalAnsiUtils.figures)
const choices = renderChoices(nextState, options, figures)
const clearScreen = renderClearScreen(prevState, options, terminal.columns)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyan)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.black)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyanBright)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.blackBright)
const promptMsg = renderOutput(leadingSymbol, trailingSymbol, options)
return pipe(
clearScreen,
Expand All @@ -159,7 +157,7 @@ const renderSubmission = <A>(
const selected = Doc.text(options.choices[state.cursor].title)
const clearScreen = renderClearScreen(Option.some(state), options, terminal.columns)
const leadingSymbol = Doc.annotate(figures.tick, Ansi.green)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.black)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.blackBright)
const promptMsg = renderOutput(leadingSymbol, trailingSymbol, options)
return pipe(
clearScreen,
Expand Down
19 changes: 10 additions & 9 deletions src/internal/prompt/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ const renderClearScreen = (
const renderInput = (
nextState: State,
options: Required<Prompt.Prompt.TextOptions>,
submitted: boolean = false
submitted: boolean
): Doc.AnsiDoc => {
const annotation = Option.match(nextState.error, {
onNone: () => submitted ? Ansi.white : Ansi.combine(Ansi.underlined, Ansi.green),
onNone: () => submitted ? Ansi.white : Ansi.combine(Ansi.underlined, Ansi.cyanBright),
onSome: () => Ansi.red
})
switch (options.type) {
Expand Down Expand Up @@ -103,7 +103,8 @@ const renderOutput = (
nextState: State,
leadingSymbol: Doc.AnsiDoc,
trailingSymbol: Doc.AnsiDoc,
options: Required<Prompt.Prompt.TextOptions>
options: Required<Prompt.Prompt.TextOptions>,
submitted: boolean = false
): Doc.AnsiDoc => {
const annotateLine = (line: string): Doc.AnsiDoc => pipe(Doc.text(line), Doc.annotate(Ansi.bold))
const promptLines = options.message.split(/\r?\n/)
Expand All @@ -116,10 +117,10 @@ const renderOutput = (
Doc.cat(Doc.space),
Doc.cat(trailingSymbol),
Doc.cat(Doc.space),
Doc.cat(renderInput(nextState, options))
Doc.cat(renderInput(nextState, options, submitted))
)
}
return Doc.hsep([prefix, trailingSymbol, renderInput(nextState, options)])
return Doc.hsep([prefix, trailingSymbol, renderInput(nextState, options, submitted)])
}

const renderNextFrame = (
Expand All @@ -131,8 +132,8 @@ const renderNextFrame = (
const terminal = yield* _(Terminal.Terminal)
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(prevState, options, terminal.columns)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyan)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.black)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyanBright)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.blackBright)
const promptMsg = renderOutput(nextState, leadingSymbol, trailingSymbol, options)
const errorMsg = renderError(nextState, figures.pointerSmall)
return pipe(
Expand All @@ -154,8 +155,8 @@ const renderSubmission = (
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(Option.some(nextState), options, terminal.columns)
const leadingSymbol = Doc.annotate(figures.tick, Ansi.green)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.black)
const promptMsg = renderOutput(nextState, leadingSymbol, trailingSymbol, options)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.blackBright)
const promptMsg = renderOutput(nextState, leadingSymbol, trailingSymbol, options, true)
return pipe(
clearScreen,
Doc.cat(promptMsg),
Expand Down
18 changes: 11 additions & 7 deletions src/internal/prompt/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ const renderClearScreen = (
return Doc.cat(clearOutput, clearPrompt)
}

const renderToggle = (value: boolean, options: Required<Prompt.Prompt.ToggleOptions>) => {
const separator = pipe(Doc.char("/"), Doc.annotate(Ansi.black))
const selectedAnnotation = Ansi.combine(Ansi.underlined, Ansi.cyan)
const renderToggle = (
value: boolean,
options: Required<Prompt.Prompt.ToggleOptions>,
submitted: boolean = false
) => {
const separator = pipe(Doc.char("/"), Doc.annotate(Ansi.blackBright))
const selectedAnnotation = Ansi.combine(Ansi.underlined, submitted ? Ansi.white : Ansi.cyanBright)
const inactive = value
? Doc.text(options.inactive)
: Doc.annotate(Doc.text(options.inactive), selectedAnnotation)
Expand Down Expand Up @@ -74,8 +78,8 @@ const renderNextFrame = (
const terminal = yield* _(Terminal.Terminal)
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(prevState, options, terminal.columns)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyan)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.black)
const leadingSymbol = Doc.annotate(Doc.text("?"), Ansi.cyanBright)
const trailingSymbol = Doc.annotate(figures.pointerSmall, Ansi.blackBright)
const toggle = renderToggle(nextState.value, options)
const promptMsg = renderOutput(toggle, leadingSymbol, trailingSymbol, options)
return pipe(
Expand All @@ -97,8 +101,8 @@ const renderSubmission = (
const figures = yield* _(InternalAnsiUtils.figures)
const clearScreen = renderClearScreen(Option.some(nextState), options, terminal.columns)
const leadingSymbol = Doc.annotate(figures.tick, Ansi.green)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.black)
const toggle = renderToggle(value, options)
const trailingSymbol = Doc.annotate(figures.ellipsis, Ansi.blackBright)
const toggle = renderToggle(value, options, true)
const promptMsg = renderOutput(toggle, leadingSymbol, trailingSymbol, options)
return pipe(
clearScreen,
Expand Down

0 comments on commit 90f55ed

Please sign in to comment.