"use strict"; /** * Human-mode display helpers for field options — make select-choice * constraints (choices, colors, per-choice visibilityFilter) readable * without `--json` post-processing. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.formatFieldDetail = exports.choiceRows = exports.describeChoiceCondition = exports.summarizeFieldOptions = void 0; const CONDITION_MAX_LEN = 90; /** Summarize a field's options for the `field list --with-options` column. */ function summarizeFieldOptions(field) { const options = field.options; const choices = options?.choices; if (Array.isArray(choices) && choices.length) { const conditional = choices.filter((c) => c && typeof c === 'object' && c.visibilityFilter).length; return conditional ? `${choices.length} choices · ${conditional} conditional` : `${choices.length} choices`; } if (!options || typeof options !== 'object') return '—'; const type = field.type; if (type === 'rating') { return [ `max=${options.max ?? 5}`, typeof options.icon === 'string' ? `icon=${options.icon}` : '', ] .filter(Boolean) .join(' '); } const formatting = options.formatting; if (type === 'number' && formatting) { return `precision=${formatting.precision ?? 0}`; } if ((type === 'date' || type === 'createdTime' || type === 'lastModifiedTime') && formatting) { return [formatting.date, formatting.time && formatting.time !== 'None' ? formatting.time : ''] .filter(Boolean) .join(' '); } if (typeof options.expression === 'string') { const e = options.expression; return `expr: ${e.length > 40 ? e.slice(0, 39) + '…' : e}`; } return '—'; } exports.summarizeFieldOptions = summarizeFieldOptions; function formatValue(value) { if (value === null || value === undefined) return ''; if (Array.isArray(value)) return value.map((v) => String(v)).join('|'); if (typeof value === 'object') return JSON.stringify(value); return String(value); } /** One-line human summary of a choice's visibilityFilter; `(always)` when absent. */ function describeChoiceCondition(vf, fieldNames) { if (!vf || typeof vf !== 'object') return '(always)'; const { conjunction, filterSet } = vf; if (!Array.isArray(filterSet) || !filterSet.length) return '(always)'; const joiner = conjunction === 'or' ? ' OR ' : ' AND '; const rules = filterSet.map(({ fieldId, operator, value }) => { const label = (fieldId && fieldNames?.[fieldId]) || fieldId || '?'; const v = formatValue(value); return v ? `${label} ${operator ?? '?'} ${v}` : `${label} ${operator ?? '?'}`; }); let text = ''; let kept = 0; for (const rule of rules) { const candidate = text ? text + joiner + rule : rule; if (candidate.length > CONDITION_MAX_LEN && kept > 0) break; text = candidate; kept++; } const omitted = rules.length - kept; return omitted > 0 ? `${text}… +${omitted} more` : text; } exports.describeChoiceCondition = describeChoiceCondition; /** Choices of a select field as display rows; `[]` when the field has none. */ function choiceRows(field, fieldNames) { const choices = field.options?.choices; if (!Array.isArray(choices)) return []; return choices .filter((c) => !!c && typeof c === 'object') .map((c) => ({ name: String(c.name ?? ''), color: typeof c.color === 'string' ? c.color : '', when: describeChoiceCondition(c.visibilityFilter, fieldNames), })); } exports.choiceRows = choiceRows; function kvLine(key, value, keyWidth) { const val = typeof value === 'object' && value !== null ? JSON.stringify(value) : String(value ?? ''); return `${key.padEnd(keyWidth)} ${val}`; } /** Full human-mode rendering for `field get`: kv lines, with select choices * expanded into a per-choice condition listing instead of one JSON blob. */ function formatFieldDetail(field, fieldNames) { const lines = []; const options = field.options; const choices = options?.choices; const restEntries = Object.entries({ ...field }); const keyWidth = Math.max(...restEntries.map(([k]) => k.length), 'defaultValue'.length); for (const [k, v] of restEntries) { if (k === 'options') continue; lines.push(kvLine(k, v, keyWidth)); } if (Array.isArray(choices) && choices.length) { const conditional = choices.filter((c) => c && typeof c === 'object' && c.visibilityFilter).length; lines.push(''); lines.push(`choices (${choices.length} · ${conditional} conditional):`); for (const row of choiceRows(field, fieldNames)) { const color = row.color ? ` [${row.color}]` : ''; lines.push(` ${row.name}${color}`); lines.push(` when: ${row.when}`); } const otherOpts = Object.fromEntries(Object.entries(options ?? {}).filter(([k]) => k !== 'choices')); if (Object.keys(otherOpts).length) { lines.push(''); lines.push(kvLine('options (other)', otherOpts, keyWidth)); } } else if (options && Object.keys(options).length) { lines.push(kvLine('options', options, keyWidth)); } else { lines.push(kvLine('options', undefined, keyWidth)); } return lines.join('\n'); } exports.formatFieldDetail = formatFieldDetail;