import blessed from 'blessed'; import { state } from '../state.js'; const STATUS_SHORT = { open: 'open ', in_progress: 'in_prog', blocked: 'blocked', deferred: 'deferrd', closed: 'closed ', }; const PRIORITY_COLOR = ['red', 'yellow', 'green', 'cyan', 'gray']; const STATUS_COLOR = { open: 'white', in_progress: 'cyan', blocked: 'red', deferred: 'yellow', closed: 'gray', }; const TYPE_BADGE = { epic: '{magenta-fg}E{/}', task: '{blue-fg}T{/}', chore: '{gray-fg}C{/}', bug: '{red-fg}B{/}', story: '{cyan-fg}S{/}', feature: '{green-fg}F{/}', }; function t(color, text) { return `{${color}-fg}${text}{/}`; } function formatRow(bead, depth = 0, isLast = false, guides = [], idWidth = 14) { const id = bead.id.padEnd(idWidth); const p = `P${bead.priority ?? 2}`; // Effective status mirrors how `bd blocked` derives the Blocked tab: // an open bead whose blockers aren't all closed reads as "blocked", // not "open". The raw `bd list` rows only carry the stored status. const stat = (bead.status === 'open' && state.blockedIds.has(bead.id)) ? 'blocked' : (bead.status || 'open'); const s = (STATUS_SHORT[stat] || stat.slice(0, 7)).padEnd(7); const badge = TYPE_BADGE[bead.issue_type] ?? t('gray', '?'); const prefix = depth > 0 ? t('gray', ' ' + guides.map((g) => (g ? '│ ' : ' ')).join('') + (isLast ? '└ ' : '├ ')) : ' '; const title = bead.title || ''; const idCol = t('gray', id); const pCol = t(PRIORITY_COLOR[bead.priority ?? 2] ?? 'white', p); const sCol = t(STATUS_COLOR[stat] ?? 'white', s); return `${idCol} ${pCol} ${sCol} ${badge}${prefix}${title}`; } export function createList(screen) { return blessed.list({ parent: screen, top: 1, left: 0, width: '40%', bottom: 1, border: { type: 'line' }, label: ' Beads ', tags: true, keys: true, vi: true, mouse: true, scrollbar: { ch: ' ', track: { bg: 'black' }, style: { bg: 'cyan' } }, style: { selected: { bg: 'blue', fg: 'white', bold: true }, border: { fg: 'gray' }, label: { fg: 'white', bold: true }, }, }); } export function renderList(list) { const total = state.listOrder.length; // In paged ("load more") mode only the first visibleCount rows are rendered; // everything else renders the full list. const shown = state.visibleCount > 0 && state.visibleCount < total ? state.visibleCount : total; const ids = state.listOrder.slice(0, shown); // Pad the id column to the longest id in the whole list (not just the shown // page) so every row's columns line up and stay put as more rows load. const idWidth = state.listOrder.reduce((w, id) => Math.max(w, id.length), 0); const items = ids.map((id) => { const b = state.beadsById.get(id); const { depth = 0, isLast = false, guides = [] } = state.treeMeta.get(id) ?? {}; return b ? formatRow(b, depth, isLast, guides, idWidth) : t('gray', id); }); const selIdx = state.selectedId ? Math.max(0, ids.indexOf(state.selectedId)) : 0; const label = shown < total ? ` Beads ${t('gray', `(showing ${shown} of ${total})`)} ` : ` Beads ${t('gray', `(${total})`)} `; list.setLabel(label); list.setItems(items); if (ids.length > 0) list.select(Math.min(selIdx, ids.length - 1)); }