Skip to content

Commit

Permalink
feat(prompts): add few prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
renejfc committed Mar 25, 2024
1 parent 8510ff4 commit 83a2869
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 23 deletions.
21 changes: 0 additions & 21 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +0,0 @@
import { Confirm, MultiSelect, Password, Select, SelectOption } from "~/lib/prompts/"

// const p = await <Text message="Test" />
// const p2 = await <Password message="Password" />
// const p3 = await <Confirm message="R u sure?" />

// const p4 = await (
// <Select message="Choose">
// <SelectOption value="1" hint="One" />
// <SelectOption value={{ key: "val" }} label="Empty" />
// <SelectOption value="3" label="Three" hint="Woops" />
// </Select>
// )

// const p5 = (
// <MultiSelect message="Choose" required>
// <SelectOption value="1" hint="One" />
// <SelectOption value="2" label="Two" hint="Test" />
// <SelectOption value="3" label="Three" hint="Woops" />
// </MultiSelect>
// )
6 changes: 6 additions & 0 deletions src/lib/prompts/cancel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import c from "picocolors"
import { S } from "./symbols"

export function Cancel({ message = "" }: { message?: string }) {
process.stdout.write(`${c.gray(S.BAR_END)} ${c.red(message)}\n\n`)
}
5 changes: 5 additions & 0 deletions src/lib/prompts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export * from "./confirm"
export * from "./select"
export * from "./select-option"
export * from "./multi-select"
export * from "./note"
export * from "./cancel"
export * from "./intro"
export * from "./outro"
export * from "./log"
6 changes: 6 additions & 0 deletions src/lib/prompts/intro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import c from "picocolors"
import { S } from "./symbols"

export function Intro({ title = "" }: { title?: string }) {
process.stdout.write(`${c.gray(S.BAR_START)} ${title}\n`)
}
43 changes: 43 additions & 0 deletions src/lib/prompts/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import c from "picocolors"
import { S } from "./symbols"
import type { LogMessageOptions } from "./types"

function Message({ message, symbol }: LogMessageOptions = {}) {
const parts = [`${c.gray(S.BAR)}`]

if (message) {
const [firstLine, ...lines] = message.split("\n")
parts.push(`${symbol} ${firstLine}`, ...lines.map(ln => `${c.gray(S.BAR)} ${ln}`))
}

process.stdout.write(`${parts.join("\n")}\n`)
}

function Info({ message }: { message: string }) {
Message({ message, symbol: c.blue(S.INFO) })
}

function Success({ message }: { message: string }) {
Message({ message, symbol: c.green(S.SUCCESS) })
}

function Step({ message }: { message: string }) {
Message({ message, symbol: c.green(S.STEP_SUBMIT) })
}

function Warning({ message }: { message: string }) {
Message({ message, symbol: c.yellow(S.WARN) })
}

function error({ message }: { message: string }) {
Message({ message, symbol: c.red(S.ERROR) })
}

export const Log = {
Message,
Info,
Success,
Step,
Warning,
Error: error,
}
39 changes: 39 additions & 0 deletions src/lib/prompts/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import c from "picocolors"
import { S } from "./symbols"

const strip = (str: string) => str.replace(ansiRegex(), "")

export function Note({ message = "", title ="" }: { message?: string; title?: string }) {
const lines = `\n${message}\n`.split("\n")
const titleLen = strip(title).length

const len =
Math.max(
lines.reduce((sum, ln) => {
const line = strip(ln)
return line.length > sum ? line.length : sum
}, 0),
titleLen
) + 2

const msg = lines
.map(ln => `${c.gray(S.BAR)} ${c.dim(ln)}${" ".repeat(len - strip(ln).length)}${c.gray(S.BAR)}`)
.join("\n")

process.stdout.write(
`${c.gray(S.BAR)}\n${c.green(S.STEP_SUBMIT)} ${c.reset(title)} ${c.gray(
S.BAR_H.repeat(Math.max(len - titleLen - 1, 1)) + S.CORNER_TOP_RIGHT
)}\n${msg}\n${c.gray(S.CONNECT_LEFT + S.BAR_H.repeat(len + 2) + S.CORNER_BOTTOM_RIGHT)}\n`
)
}

// Adapted from https://github.com/chalk/ansi-regex
// @see LICENSE
function ansiRegex() {
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
].join("|")

return new RegExp(pattern, "g")
}
6 changes: 6 additions & 0 deletions src/lib/prompts/outro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import c from "picocolors"
import { S } from "./symbols"

export function Outro({ message = "" }: { message?: string }) {
process.stdout.write(`${c.gray(S.BAR)}\n${c.gray(S.BAR_END)} ${message}\n\n`)
}
4 changes: 2 additions & 2 deletions src/lib/prompts/select-option.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Option } from "./types";
import type { Option } from "./types"

export function SelectOption<Value>({ value, label, hint }: Option<Value>) {
return { value, label, hint }
}
}
5 changes: 5 additions & 0 deletions src/lib/prompts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ export type MultiSelectOptions<Value> = {
required?: boolean
cursorAt?: Value
}

export type LogMessageOptions = {
message?: string
symbol?: string
}

0 comments on commit 83a2869

Please sign in to comment.