Commands: add dynamic arg menus
This commit is contained in:
committed by
Peter Steinberger
parent
7e1e7ba2d8
commit
74bc5bfd7c
66
src/auto-reply/commands-args.ts
Normal file
66
src/auto-reply/commands-args.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import type { CommandArgValues } from "./commands-registry.types.js";
|
||||
|
||||
export type CommandArgsFormatter = (values: CommandArgValues) => string | undefined;
|
||||
|
||||
function normalizeArgValue(value: unknown): string | undefined {
|
||||
if (value == null) return undefined;
|
||||
const text = typeof value === "string" ? value.trim() : String(value).trim();
|
||||
return text ? text : undefined;
|
||||
}
|
||||
|
||||
const formatConfigArgs: CommandArgsFormatter = (values) => {
|
||||
const action = normalizeArgValue(values.action)?.toLowerCase();
|
||||
const path = normalizeArgValue(values.path);
|
||||
const value = normalizeArgValue(values.value);
|
||||
if (!action) return undefined;
|
||||
if (action === "show" || action === "get") {
|
||||
return path ? `${action} ${path}` : action;
|
||||
}
|
||||
if (action === "unset") {
|
||||
return path ? `${action} ${path}` : action;
|
||||
}
|
||||
if (action === "set") {
|
||||
if (!path) return action;
|
||||
if (!value) return `${action} ${path}`;
|
||||
return `${action} ${path}=${value}`;
|
||||
}
|
||||
return action;
|
||||
};
|
||||
|
||||
const formatDebugArgs: CommandArgsFormatter = (values) => {
|
||||
const action = normalizeArgValue(values.action)?.toLowerCase();
|
||||
const path = normalizeArgValue(values.path);
|
||||
const value = normalizeArgValue(values.value);
|
||||
if (!action) return undefined;
|
||||
if (action === "show" || action === "reset") {
|
||||
return action;
|
||||
}
|
||||
if (action === "unset") {
|
||||
return path ? `${action} ${path}` : action;
|
||||
}
|
||||
if (action === "set") {
|
||||
if (!path) return action;
|
||||
if (!value) return `${action} ${path}`;
|
||||
return `${action} ${path}=${value}`;
|
||||
}
|
||||
return action;
|
||||
};
|
||||
|
||||
const formatQueueArgs: CommandArgsFormatter = (values) => {
|
||||
const mode = normalizeArgValue(values.mode);
|
||||
const debounce = normalizeArgValue(values.debounce);
|
||||
const cap = normalizeArgValue(values.cap);
|
||||
const drop = normalizeArgValue(values.drop);
|
||||
const parts: string[] = [];
|
||||
if (mode) parts.push(mode);
|
||||
if (debounce) parts.push(`debounce:${debounce}`);
|
||||
if (cap) parts.push(`cap:${cap}`);
|
||||
if (drop) parts.push(`drop:${drop}`);
|
||||
return parts.length > 0 ? parts.join(" ") : undefined;
|
||||
};
|
||||
|
||||
export const COMMAND_ARG_FORMATTERS: Record<string, CommandArgsFormatter> = {
|
||||
config: formatConfigArgs,
|
||||
debug: formatDebugArgs,
|
||||
queue: formatQueueArgs,
|
||||
};
|
||||
Reference in New Issue
Block a user