- 새 src/commands/prompt.ts — PromptSpec · renderPrompt(키워드 물음은 「또는」 없음) · matchOption(initget 줄임) · 세션 기억 · OPT · SELECT_OBJECTS - 새 inputController/prompt-input.ts — routeTyped 사다리(점 · 옵션 묻는 중 명령 이름 거절 · 식 · 단위 거절 · 투명 ') · 알림 줄 · 덧물음 · cancelActiveCommand · undoCommand - 새 tools/factories/end-command.ts — final(END) 갈고리 → 같은 틱 선택 도구 · state.ts 갈고리 · LASTPOINT - sequence-tool v2 — ROUTE · id · prompt · when · next · options · keyword · loopFrom · escCommits · pickfirst · 확정 뒤 기본 끝 · Esc 늘 끝 - 흠 — N1 ②③ · N5 · N6 · N11 · N18(앞글자 실행 없앰) · H1(Ctrl · Alt 글자 안 쌓음 · Ctrl+C · X · Shift+C · Shift+V · 1) · 우클릭 = 쉴 때 반복 · SE 뺌 · ED 더함 - 시험 — cad-drive 도우미 · prompt-frame · prompt-contract · 옛 시험 손질(keyboard-chain · ime-sink · command-catalog · input-prefs-cad · eraser 녹화) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QfHKQs7J3hQELKMWQMUJEy
180 lines
6.9 KiB
TypeScript
180 lines
6.9 KiB
TypeScript
/**
|
|
* 물음 꼴(337 설계 2-3 · 2-4 · 2-9) — 오토캐드 명령행 한 줄 「글 또는 [이름(키)/…] <기본>:」 ·
|
|
* 옵션 맞추기(initget 줄임 규칙) · 세션 기억(앞에 쓴 값이 다음 <기본>).
|
|
* 도구는 상태 `meta: { kind, prompt }` 로 물음을 선언하고 `OPTION.<key>` 사건으로 옵션을 받는다.
|
|
* 이 파일은 상태(state.ts)를 읽지 않음 — 기억은 탭 수명 Map 하나.
|
|
*/
|
|
import type { StepKind } from '../tools/tool.types';
|
|
|
|
export type XY = { x: number; y: number };
|
|
|
|
/** 옵션 하나 — 그림 「label(kw)」 · 사건 `OPTION.<key>` · 맞추기는 kw · en */
|
|
export interface OptionSpec {
|
|
key: string;
|
|
kw: string;
|
|
en: string;
|
|
label: string;
|
|
hidden?: true;
|
|
}
|
|
|
|
export type DefaultSpec =
|
|
| { kind: 'value'; remember: string; initial: number | XY | null; format?: 'len' | 'int' | 'ang' }
|
|
/** <변위> · <전체(T)>(label 로 바꿈) — 빈 Enter = `OPTION.<key>` */
|
|
| { kind: 'option'; key: string; label?: string }
|
|
/** <첫 번째 점을 변위로 사용> · <종료> · <객체 선택> — 빈 Enter = `ENTER` */
|
|
| { kind: 'enter'; label: string };
|
|
|
|
export interface PromptSpec {
|
|
/** '다음 점 지정' — ' 또는 [..]' · ' <..>' · ':' 는 renderPrompt 가 붙임 */
|
|
text: string;
|
|
/** 넣은 옵션(숨김 포함) */
|
|
options?: OptionSpec[];
|
|
/** 오토캐드엔 있고 우리엔 없음 — 치면 안내 · 안 그림 */
|
|
absent?: OptionSpec[];
|
|
default?: DefaultSpec;
|
|
number?: { int?: true; min?: number; max?: number; positive?: true };
|
|
/** 거절 글 바꿈(없으면 물음 종류 기본 글) */
|
|
reject?: string;
|
|
/** ↑↓ 최근 값 묶음(없으면 text) */
|
|
history?: string;
|
|
}
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: 도구마다 context 꼴이 다름
|
|
export type PromptLike = PromptSpec | ((context: any) => PromptSpec);
|
|
|
|
/** 상태 meta 꼴 — `instructions` 는 옛 꼴(전환 중) */
|
|
export interface PromptMeta {
|
|
kind?: StepKind;
|
|
prompt?: PromptLike;
|
|
instructions?: string;
|
|
/** 옛 꼴 — 점 물음이 받는 옵션 글자(대문자) */
|
|
options?: string[];
|
|
}
|
|
|
|
/** 함수 꼴 물음을 지금 context 로 풂 */
|
|
export const resolvePrompt = (prompt: PromptLike | undefined, context: unknown): PromptSpec | null =>
|
|
prompt ? (typeof prompt === 'function' ? prompt(context) : prompt) : null;
|
|
|
|
// ---- 세션 기억(설계 2-9) ----
|
|
|
|
const memory = new Map<string, unknown>();
|
|
let lastPicked: string[] = [];
|
|
let lastPointValue: XY | null = null;
|
|
|
|
export const remember = (key: string, value: unknown): void => {
|
|
memory.set(key, value);
|
|
};
|
|
export const recall = <T = unknown>(key: string): T | undefined => memory.get(key) as T | undefined;
|
|
/** 고르기 이전(P) — 명령이 고른 것을 쓸 때 */
|
|
export const rememberSelection = (ids: string[]): void => {
|
|
lastPicked = [...ids];
|
|
};
|
|
export const previousSelection = (): string[] => lastPicked;
|
|
/** 마지막 점(LASTPOINT) — `@` 의 기준 */
|
|
export const rememberPoint = (point: XY): void => {
|
|
lastPointValue = { x: point.x, y: point.y };
|
|
};
|
|
export const lastPoint = (): XY | null => lastPointValue;
|
|
|
|
// ---- 그림(설계 2-3) ----
|
|
|
|
const isXY = (value: unknown): value is XY =>
|
|
typeof value === 'object' &&
|
|
value !== null &&
|
|
typeof (value as XY).x === 'number' &&
|
|
typeof (value as XY).y === 'number';
|
|
|
|
/** 기본 값 — 기억 우선 · 없으면 처음 값 */
|
|
export function defaultValue(spec: DefaultSpec & { kind: 'value' }): number | XY | null {
|
|
const remembered = recall<number | XY>(spec.remember);
|
|
return remembered ?? spec.initial;
|
|
}
|
|
|
|
const formatNumber = (value: number, format: 'len' | 'int' | 'ang' = 'len'): string => {
|
|
if (format === 'int') return String(Math.round(value));
|
|
if (format === 'ang') return String(Number(value.toFixed(4)));
|
|
return value.toFixed(4); // LUPREC 4
|
|
};
|
|
|
|
function renderDefault(prompt: PromptSpec): string | null {
|
|
const spec = prompt.default;
|
|
if (!spec) return null;
|
|
if (spec.kind === 'enter') return spec.label;
|
|
if (spec.kind === 'option') {
|
|
return spec.label ?? prompt.options?.find((option) => option.key === spec.key)?.label ?? null;
|
|
}
|
|
const value = defaultValue(spec);
|
|
if (value === null) return null;
|
|
if (isXY(value)) return `${value.x.toFixed(4)}, ${value.y.toFixed(4)}`;
|
|
return formatNumber(value, spec.format);
|
|
}
|
|
|
|
/** 「다음 점 지정 또는 [닫기(C)/명령 취소(U)] <10.0000>:」 · 키워드 물음은 「또는」 없이 「옵션 입력 [..] <I>:」 */
|
|
export function renderPrompt(prompt: PromptSpec, kind?: StepKind): string {
|
|
const shown = (prompt.options ?? []).filter((option) => !option.hidden);
|
|
const joiner = kind === 'keyword' ? ' ' : ' 또는 ';
|
|
const options = shown.length
|
|
? `${joiner}[${shown.map((option) => `${option.label}(${option.kw})`).join('/')}]`
|
|
: '';
|
|
const fallback = renderDefault(prompt);
|
|
return `${prompt.text}${options}${fallback === null ? '' : ` <${fallback}>`}:`;
|
|
}
|
|
|
|
// ---- 옵션 맞추기(설계 2-4) ----
|
|
|
|
/** 친 글 → 맞추기 꼴 — 빈칸 뗌 · 대문자 · 앞 `_` 하나 뗌 */
|
|
export const normalizeKeyword = (text: string): string => text.trim().toUpperCase().replace(/^_/, '');
|
|
|
|
const matches = (text: string, option: OptionSpec): boolean =>
|
|
text === option.kw || text === option.en || (option.en.startsWith(text) && text.startsWith(option.kw));
|
|
|
|
export type OptionMatch = { option: OptionSpec } | { absent: OptionSpec } | null;
|
|
|
|
/** 친 글이 맞는 옵션 — kw 딱 맞음 우선 · 숨김도 · 없는 옵션(absent)은 안내용 */
|
|
export function matchOption(text: string, prompt: PromptSpec | null | undefined): OptionMatch {
|
|
if (!prompt) return null;
|
|
const typed = normalizeKeyword(text);
|
|
if (!typed) return null;
|
|
const pick = (list: OptionSpec[] | undefined) => {
|
|
const hits = (list ?? []).filter((option) => matches(typed, option));
|
|
return hits.find((option) => option.kw === typed) ?? hits[0];
|
|
};
|
|
const option = pick(prompt.options);
|
|
if (option) return { option };
|
|
const absent = pick(prompt.absent);
|
|
return absent ? { absent } : null;
|
|
}
|
|
|
|
// ---- 공용 옵션 ----
|
|
|
|
const opt = (key: string, kw: string, label: string, en = key): OptionSpec => ({
|
|
key,
|
|
kw,
|
|
en,
|
|
label,
|
|
});
|
|
const hidden = (option: OptionSpec): OptionSpec => ({ ...option, hidden: true });
|
|
|
|
/** 여러 명령이 같이 쓰는 옵션 — 글자가 다른 명령(종료(X) 등)은 그 도구가 따로 만듦 */
|
|
export const OPT = {
|
|
UNDO: opt('UNDO', 'U', '명령 취소'),
|
|
CLOSE: opt('CLOSE', 'C', '닫기'),
|
|
EXIT: opt('EXIT', 'E', '종료'),
|
|
MULTIPLE: opt('MULTIPLE', 'M', '다중'),
|
|
YES: opt('YES', 'Y', '예'),
|
|
NO: opt('NO', 'N', '아니오'),
|
|
DISPLACEMENT: opt('DISPLACEMENT', 'D', '변위'),
|
|
COPY: opt('COPY', 'C', '복사'),
|
|
REFERENCE: opt('REFERENCE', 'R', '참조'),
|
|
TEXT: opt('TEXT', 'T', '문자'),
|
|
ALL: hidden(opt('ALL', 'ALL', '전체')),
|
|
LAST: hidden(opt('LAST', 'L', '최종')),
|
|
PREVIOUS: hidden(opt('PREVIOUS', 'P', '이전')),
|
|
} as const;
|
|
|
|
/** 고르기 물음 공용(설계 2-15) — 숨은 옵션 ALL · L · P 의 처리는 선택 도구 몫 */
|
|
export const SELECT_OBJECTS: PromptSpec = {
|
|
text: '객체 선택',
|
|
options: [OPT.ALL, OPT.LAST, OPT.PREVIOUS],
|
|
};
|