Mari's guided journal software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

80 lines
3.3 KiB

import capitalize from "capitalize";
import {YamlPromptFunction} from "../../schemata/YAMLPrompt.js";
import chalk from "chalk";
import {InquireFunction} from "../Inquire.js";
import {EmpathyGroup, EmpathyGroupJTD} from "../../datatypes/EmpathyGroup.js";
import {EditorQuestion, ExpandQuestion, InputQuestion, MultiTextInputQuestion} from "inquirer";
export interface EmpathyGroupPromptOptions {
default?: EmpathyGroup
listName: string
}
export interface EmpathyGroupPromptDependencies {
inquire: InquireFunction<ExpandQuestion, typeof PROMPT|typeof AUTO|typeof YAML> & InquireFunction<InputQuestion, string> & InquireFunction<MultiTextInputQuestion, string[]> & InquireFunction<EditorQuestion, string>
promptYaml: YamlPromptFunction
}
export function empathyGroupPrompt(deps: EmpathyGroupPromptDependencies): (opts: EmpathyGroupPromptOptions) => Promise<EmpathyGroup | null> {
return (opts) => promptForEmpathyGroup(opts, deps)
}
const PROMPT = "Prompt"
const AUTO = "Auto"
const YAML = "YAML"
export async function promptForEmpathyGroup(opts: EmpathyGroupPromptOptions, deps: EmpathyGroupPromptDependencies): Promise<EmpathyGroup | null> {
const {listName, default: {header = undefined, items = []} = {}} = opts
const {inquire, promptYaml} = deps
const mode = await inquire({
type: "expand",
message: `How would you like to ${header ? "edit" : "create"} this ${listName} group?`,
choices: [
{key: "p", name: "Be prompted and use the interactive process", value: PROMPT},
{key: "t", name: "Auto-process a plain-text list pasted from another source", value: AUTO},
{key: "y", name: `Directly ${header ? "edit" : "write"} YAML`, value: YAML},
]
})
switch (mode) {
case AUTO:
const text: string = await inquire({
type: "editor",
message: "Enter the header on the first line, and each item on separate lines:",
default: (header ? [header, ...items] : ["", ...items]).join("\n")
})
const trimmed = text.trim()
if (trimmed === "") {
return null
}
const [parsedHeader, ...parsedItems] = trimmed.split("\n").map((line) => line.trim()).filter((line) => line !== "")
return {
header: capitalize.words(parsedHeader, false),
items: parsedItems.map((item) => item.toLocaleLowerCase())
}
case YAML:
return await promptYaml({
schema: EmpathyGroupJTD,
currentValue: opts.default,
name: `this ${listName} group`,
}) || null
case PROMPT:
default:
const newHeader = await inquire({
type: "input",
message: `What should this ${listName} group be called?${typeof header === "string" ? chalk.dim(" (leave blank to delete this group)") : ""}`,
default: header,
})
if (newHeader === "") {
return null
}
const newItems = await inquire({
type: "multitext",
message: `What ${listName} should the ${newHeader} group contain?`,
default: items,
})
return {
header: newHeader,
items: newItems,
}
}
}