{"version":3,"file":"statusLine.cjs","names":[],"sources":["../../../src/web/lib/statusLine.ts"],"sourcesContent":["export interface Selection {\n  /** Empty when the session runs without a profile, which is a valid choice. */\n  profile: string;\n  preset: string;\n  domains: string[];\n  /** True while a preset switch is journaled but not yet applied. */\n  pending: boolean;\n}\n\nexport const emptySelection: Selection = { profile: '', preset: '', domains: [], pending: false };\n\ninterface Segment {\n  text: string;\n  rgb: [number, number, number] | null;\n}\n\n// The escape is the point of this parser, so the control character is\n// deliberate; spelled as a Unicode escape to keep it readable.\n// oxlint-disable-next-line no-control-regex\nconst SGR = /\\u001B\\[([0-9;]*)m/gu;\n\n/** Splits an ANSI string into runs of text carrying the truecolor in force. */\nexport function ansiSegments(input: string): Segment[] {\n  const segments: Segment[] = [];\n  let current: [number, number, number] | null = null;\n  let index = 0;\n\n  SGR.lastIndex = 0;\n  for (let match = SGR.exec(input); match !== null; match = SGR.exec(input)) {\n    const text = input.slice(index, match.index);\n    if (text) segments.push({ text, rgb: current });\n    const codes = match[1].split(';').map((code) => Number.parseInt(code, 10));\n    if (codes[0] === 38 && codes[1] === 2) current = [codes[2], codes[3], codes[4]];\n    else if (codes[0] === 39 || codes[0] === 0 || Number.isNaN(codes[0])) current = null;\n    index = match.index + match[0].length;\n  }\n  const tail = input.slice(index);\n  if (tail) segments.push({ text: tail, rgb: current });\n  return segments;\n}\n\nexport function stripAnsi(input: string): string {\n  return input.replace(SGR, '');\n}\n\n// The values po-dark.json binds to `warning` and `accent`. Po ships one\n// theme, so comparing against both and taking the nearer one reports pending\n// accurately, and falls back to \"not pending\" when it cannot tell.\nconst WARNING: [number, number, number] = [236, 190, 123];\nconst ACCENT: [number, number, number] = [81, 175, 239];\n\nfunction distance(a: [number, number, number], b: [number, number, number]): number {\n  return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2;\n}\n\n/**\n * Reads Po's footer status line.\n *\n * The line is the only place the session publishes its selection, so this\n * parses the shape `*profile*:[preset]:domains` structurally and uses the\n * colour of the preset segment solely to tell a pending switch from a settled one.\n */\nexport function parseSelection(statusText: string): Selection {\n  const segments = ansiSegments(statusText);\n  const plain = stripAnsi(statusText).trim();\n  if (!plain) return emptySelection;\n\n  const profileMatch = /\\*([^*]+)\\*/u.exec(plain);\n  const modeMatch = /\\[([^\\]]+)\\]/u.exec(plain);\n\n  const afterMode = modeMatch ? plain.slice(plain.indexOf(modeMatch[0]) + modeMatch[0].length) : '';\n  const domains = afterMode\n    .replace(/^:/u, '')\n    .split(',')\n    .map((entry) => entry.trim())\n    .filter(Boolean);\n\n  const modeSegment = segments.find((segment) => segment.text.includes('['));\n  const pending =\n    modeSegment?.rgb !== undefined && modeSegment?.rgb !== null\n      ? distance(modeSegment.rgb, WARNING) < distance(modeSegment.rgb, ACCENT)\n      : false;\n\n  return {\n    profile: profileMatch ? profileMatch[1] : '',\n    preset: modeMatch ? modeMatch[1] : '',\n    domains,\n    pending,\n  };\n}\n"],"mappings":";AASA,MAAa,iBAA4B;CAAE,SAAS;CAAI,QAAQ;CAAI,SAAS,CAAC;CAAG,SAAS;AAAM;AAUhG,MAAM,MAAM;;AAGZ,SAAgB,aAAa,OAA0B;CACrD,MAAM,WAAsB,CAAC;CAC7B,IAAI,UAA2C;CAC/C,IAAI,QAAQ;CAEZ,IAAI,YAAY;CAChB,KAAK,IAAI,QAAQ,IAAI,KAAK,KAAK,GAAG,UAAU,MAAM,QAAQ,IAAI,KAAK,KAAK,GAAG;EACzE,MAAM,OAAO,MAAM,MAAM,OAAO,MAAM,KAAK;EAC3C,IAAI,MAAM,SAAS,KAAK;GAAE;GAAM,KAAK;EAAQ,CAAC;EAC9C,MAAM,QAAQ,MAAM,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,SAAS,OAAO,SAAS,MAAM,EAAE,CAAC;EACzE,IAAI,MAAM,OAAO,MAAM,MAAM,OAAO,GAAG,UAAU;GAAC,MAAM;GAAI,MAAM;GAAI,MAAM;EAAE;OACzE,IA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