@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
132 lines • 5.9 kB
JavaScript
/**
* Line builders for the ask-user question overlay. Kept separate from the
* component so the layout rules stay under the 250-LOC sibling limit; the
* component owns focus, key decoding and the inline Inputs.
*/
import { CURSOR_MARKER } from "@earendil-works/pi-tui";
import { theme } from "../theme/theme.js";
import { COMMENT_LABEL, OWN_ANSWER_LABEL } from "./ask-user-question-state.js";
import { rawKeyHint } from "./keybinding-hints.js";
export function renderTitle(countdownLabel) {
const suffix = countdownLabel === "" ? "" : theme.fg("muted", ` · ${countdownLabel}`);
return theme.fg("accent", theme.bold("Ask user")) + suffix;
}
export function renderTabLabels(state) {
const tabs = state.request.questions.map((question, index) => {
const answered = state.isAnswered(question.id) ? theme.fg("success", " ✓") : "";
const label = `${question.header}${answered}`;
return index === state.activeTabIndex
? theme.fg("accent", theme.bold(`→ ${label}`))
: theme.fg("muted", ` ${label}`);
});
const submit = state.activeTabIndex === state.request.questions.length
? theme.fg("accent", theme.bold("→ Submit"))
: theme.fg("muted", " Submit");
return [...tabs, submit];
}
export function renderTabBar(state) {
return renderTabLabels(state).join(" ");
}
export function renderQuestionLine(question) {
return theme.fg("text", question.question);
}
/**
* `focused` places the hardware cursor at the end of the own-answer row while the option list has focus: a printable
* key there opens the own answer, so an IME draws its composing text where the answer goes instead of the footer.
*/
export function renderQuestionList(state, focused) {
const lines = [];
const active = state.activeQuestion;
active.options.forEach((option, index) => {
const highlighted = index === state.highlightIndex && state.focus === "options";
const marker = state.isSelected(active.id, option.label) ? theme.fg("success", " ✓") : "";
const prefix = highlighted ? theme.fg("accent", "→ ") : " ";
lines.push(`${prefix}${index + 1}. ${theme.fg("text", option.label)}${marker}`);
if (option.description) {
lines.push(theme.fg("muted", ` ${option.description}`));
}
});
const ownText = state.textFor(active.id);
const ownSuffix = ownText !== undefined ? theme.fg("text", `: ${ownText}`) : "";
const ownHighlighted = state.highlightIndex === state.ownAnswerRowIndex && state.focus === "options";
const ownPrefix = ownHighlighted ? theme.fg("accent", "→ ") : " ";
const cursor = focused && state.focus === "options" ? CURSOR_MARKER : "";
lines.push(`${ownPrefix}${theme.fg("muted", OWN_ANSWER_LABEL)}${ownSuffix}${cursor}`);
return lines;
}
export function renderSubmitSummary(state) {
return state.request.questions.map((question, index) => {
const highlighted = state.focus === "submit" && state.submitRowIndex === index;
const prefix = highlighted ? theme.fg("accent", "→ ") : " ";
const answer = state.answers()[question.id];
if (!answer)
return `${prefix}${theme.fg("warning", `${question.header}: unanswered`)}`;
const value = answer.selected.length > 0 ? answer.selected.join(", ") : (answer.text ?? "");
return `${prefix}${question.header}: ${value}`;
});
}
export function renderOwnAnswerLabel() {
return theme.fg("muted", "Your answer (enter to save, ↑↓ back to options, esc to discard)");
}
export function renderCommentLabel() {
return theme.fg("muted", COMMENT_LABEL);
}
export function renderNotice(notice) {
return notice === undefined ? "" : theme.fg("warning", `! ${notice}`);
}
export function renderSubmitLine(state) {
const answered = state.answeredCount();
const total = state.request.questions.length;
if (state.focus === "submit") {
return theme.fg("accent", theme.bold(`Submit (${answered}/${total} answered)`));
}
const hint = state.focus === "options" && state.activeQuestion.multiSelect
? " - Enter toggles; Tab to Submit"
: " - Enter advances";
return theme.fg("accent", theme.bold(`Submit (${answered}/${total} answered)`)) + theme.fg("muted", hint);
}
export function renderHintsLine(state) {
if (state.focus === "submit") {
if (!state.isCommentFocused) {
return (rawKeyHint("enter", "edit answer") +
" " +
rawKeyHint("↑↓", "move") +
" " +
rawKeyHint("tab", "next question") +
" " +
rawKeyHint("esc", "back"));
}
return (rawKeyHint("enter", "submit") +
" " +
rawKeyHint("↑", "review answers") +
" " +
rawKeyHint("shift+tab", "back") +
" " +
rawKeyHint("tab", "next question") +
" " +
rawKeyHint("esc", "back"));
}
if (state.focus === "own-answer") {
return (rawKeyHint("enter", "save and next") +
" " +
rawKeyHint("↑↓", "back to options") +
" " +
rawKeyHint("tab", "next question") +
" " +
rawKeyHint("esc", "discard"));
}
return (rawKeyHint("↑↓", "move") +
" " +
rawKeyHint("1-9", "select") +
" " +
rawKeyHint("space", state.activeQuestion.multiSelect ? "toggle" : "select") +
" " +
rawKeyHint("enter", state.activeQuestion.multiSelect ? "toggle" : "next") +
" " +
rawKeyHint("tab", state.activeQuestion.multiSelect ? "next / Submit" : "next question") +
" " +
rawKeyHint("c", "comment") +
" " +
rawKeyHint("esc", "cancel"));
}
//# sourceMappingURL=ask-user-question-render.js.map