askeroo
Version:
A modern CLI prompt library with flow control, history navigation, and conditional prompts
60 lines • 2.58 kB
JavaScript
#!/usr/bin/env node
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { Text, Box } from "ink";
import { createPrompt, ask } from "../src/index.js";
// Custom plugin that's aware of submission types
export const smartPrompt = createPrompt({
type: "smartPrompt",
autoSubmit: false, // Default: manual submission
component: ({ node, options, events }) => {
const [value, setValue] = useState("");
// NOTE: In real implementation, you would get treeManager from context
// For this example, we'll show the concept
const showNavInfo = options.showNavigationInfo ?? true;
// Simulated navigation info (in real use, get from treeManager)
const canGoBack = node.allowBack !== false;
// const previousSubmissionType = treeManager.getPreviousNode()?.submissionType;
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { children: options.label }), _jsxs(Text, { color: "cyan", children: ["Input: ", value] }), showNavInfo && (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: "gray", dimColor: true, children: ["Navigation: ", canGoBack ? "↑ Back enabled" : "↑ Back disabled"] }) })), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: "gray", dimColor: true, children: ["State: ", node.state] }) })] }));
},
});
// Example flow using the custom plugin
const flow = async () => {
console.log("Starting custom plugin example...\n");
// Auto-submitted prompt (user can't go back to this)
const autoValue = await smartPrompt({
label: "Auto-submitted field",
autoSubmit: true,
showNavigationInfo: true
});
// Manual prompt (user can go back)
const manualValue = await smartPrompt({
label: "Manual submission field",
autoSubmit: false, // Explicit
showNavigationInfo: true
});
// Prompt with custom navigation rules
const restrictedValue = await smartPrompt({
label: "No going back from here",
allowBack: false,
showNavigationInfo: true
});
return {
autoValue,
manualValue,
restrictedValue
};
};
// Run the example
(async () => {
try {
const result = await ask(flow);
console.log("\n✅ Custom plugin example completed!");
console.log("Result:", JSON.stringify(result, null, 2));
}
catch (error) {
console.error("❌ Error:", error);
process.exit(1);
}
})();
//# sourceMappingURL=custom-plugin-with-submission-types.js.map