askeroo
Version:
A modern CLI prompt library with flow control, history navigation, and conditional prompts
76 lines • 2.95 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
* Example: Creating a custom prompt using askeroo/core imports
*
* Demonstrates:
* - Importing from askeroo/core (cleaner than relative paths)
* - Using Prompt State Context for external state updates
* - Creating a custom prompt with createPrompt
* - Using the simplified usePromptData hook
*/
import { useEffect } from "react";
import { Box, Text } from "ink";
import { ask } from "../src/index.js";
import { createPrompt, usePromptState, getPromptStateNotifier, } from "../src/core.js";
// Global store for our custom prompt
const customStore = {
messages: [],
};
// API to add messages externally
export function addMessage(message) {
customStore.messages.push(message);
// Notify prompts to update
const notifyChange = getPromptStateNotifier();
if (notifyChange) {
notifyChange();
}
}
export const messageBoard = createPrompt({
type: "messageBoard",
component: ({ node, options, events }) => {
// Subscribe to prompt state updates
const { revision } = usePromptState();
// Read messages during render (prevents flicker)
const messages = options.maxMessages
? customStore.messages.slice(-options.maxMessages)
: customStore.messages;
// Force re-render when revision changes
void revision;
// Auto-submit (setTimeout is automatically applied with 100ms default delay)
useEffect(() => {
if (node.state === "active" && events.onSubmit) {
events.onSubmit({ type: "auto" });
}
}, [node.state, events.onSubmit]);
if (messages.length === 0) {
return _jsx(Text, { color: "gray", children: "No messages yet..." });
}
return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", padding: 1, children: [_jsx(Text, { bold: true, color: "cyan", children: options.label }), messages.map((msg, idx) => (_jsxs(Text, { color: "green", children: ["\u2022 ", msg] }, idx)))] }));
},
});
// Demo flow
const flow = async () => {
// Show initial empty message board
await messageBoard({ label: "📋 Message Board", maxMessages: 5 });
// Add some messages externally
addMessage("System initialized");
await new Promise((resolve) => setTimeout(resolve, 500));
addMessage("Loading configuration");
await new Promise((resolve) => setTimeout(resolve, 500));
addMessage("Ready!");
// Show updated message board
await messageBoard({ label: "📋 Message Board", maxMessages: 5 });
return "✅ Custom plugin with askeroo/core imports works!";
};
(async () => {
try {
const result = await ask(flow);
console.log("\n" + result);
}
catch (error) {
console.error("❌ Error:", error);
process.exit(1);
}
})();
//# sourceMappingURL=custom-plugin-with-core-imports.js.map