import chalk from "chalk";
import { Box, Text, useInput, render } from "ink";
import SelectInput from "ink-select-input";
import TextInput from "ink-text-input";
import * as React from "react";
import { useState } from "react";
import { logger } from "./logger";
type ConfirmProps = {
text: string;
onConfirm: (answer: boolean) => void;
};
function Confirm(props: ConfirmProps) {
useInput((input: string, key) => {
if (input === "y" || key.return === true) {
props.onConfirm(true);
} else if (input === "n") {
props.onConfirm(false);
} else {
logger.warn("Unrecognised input:", input);
}
});
return (
{props.text} ({chalk.bold("y")}/n)
);
}
export function confirm(text: string): Promise {
return new Promise((resolve) => {
const { unmount } = render(
{
unmount();
resolve(answer);
}}
/>
);
});
}
type PromptProps = {
text: string;
defaultValue?: string;
type?: "text" | "password";
onSubmit: (text: string) => void;
};
function Prompt(props: PromptProps) {
const [value, setValue] = useState(props.defaultValue || "");
return (
{props.text}
);
}
export async function prompt(
text: string,
type: "text" | "password" = "text",
defaultValue?: string
): Promise {
return new Promise((resolve) => {
const { unmount } = render(
{
unmount();
resolve(inputText);
}}
/>
);
});
}
type SelectOption = {
value: string;
label: string;
};
type SelectProps = {
text: string;
options: SelectOption[];
initialIndex: number;
onSelect: (value: string) => void;
};
function Select(props: SelectProps) {
return (
{props.text}
{
props.onSelect(selected.value);
}}
/>
);
}
export function select(
text: string,
options: SelectOption[],
initialIndex: number
): Promise {
return new Promise((resolve) => {
const { unmount } = render(