Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.6 KB
Raw Blame
/**
 * How far a viewer's terminal moves its cursor for one code point: the width the phone's and the desktop's
 * emulator give it, which is what a row written to them has to fit. Both vendor the same xterm.dart
 * (mobile|desktop/third_party/xterm): `Buffer.writeChar` advances one column for EVERY code point it is handed,
 * a combining mark, a joiner and a variation selector included, and a second one for a character its Unicode 11
 * table calls wide. So this is not wcwidth: a mark that a terminal would put on its base character takes a column
 * of its own there. Ported from `BMP_WIDE` and `HIGH_WIDE` in third_party/xterm/lib/src/utils/unicode_v11.dart;
 * keep the two in step if that table changes.
 *
 * tmux measures with the system's wcwidth instead, and the two disagree (newer emoji, marks), which is why a
 * keyframe's rows are placed with autowrap off (tmuxStream.ts). A row built to this measure never runs past the
 * viewer's edge: xterm.dart does not clip a row that does, it carries the rest onto a row of its own.
 */

/** Inclusive ranges of wide code points, ascending, as start, end pairs. */
const WIDE = [
  0x1100, 0x115F, 0x231A, 0x231B, 0x2329, 0x232A, 0x23E9, 0x23EC, 0x23F0, 0x23F0, 0x23F3, 0x23F3, 0x25FD,
  0x25FE, 0x2614, 0x2615, 0x2648, 0x2653, 0x267F, 0x267F, 0x2693, 0x2693, 0x26A1, 0x26A1, 0x26AA, 0x26AB,
  0x26BD, 0x26BE, 0x26C4, 0x26C5, 0x26CE, 0x26CE, 0x26D4, 0x26D4, 0x26EA, 0x26EA, 0x26F2, 0x26F3, 0x26F5,
  0x26F5, 0x26FA, 0x26FA, 0x26FD, 0x26FD, 0x2705, 0x2705, 0x270A, 0x270B, 0x2728, 0x2728, 0x274C, 0x274C,
  0x274E, 0x274E, 0x2753, 0x2755, 0x2757, 0x2757, 0x2795, 0x2797, 0x27B0, 0x27B0, 0x27BF, 0x27BF, 0x2B1B,
  0x2B1C, 0x2B50, 0x2B50, 0x2B55, 0x2B55, 0x2E80, 0x2E99, 0x2E9B, 0x2EF3, 0x2F00, 0x2FD5, 0x2FF0, 0x2FFB,
  0x3000, 0x3029, 0x302E, 0x303E, 0x3041, 0x3096, 0x309B, 0x30FF, 0x3105, 0x312F, 0x3131, 0x318E, 0x3190,
  0x31BA, 0x31C0, 0x31E3, 0x31F0, 0x321E, 0x3220, 0x3247, 0x3250, 0x4DBF, 0x4E00, 0xA48C, 0xA490, 0xA4C6,
  0xA960, 0xA97C, 0xAC00, 0xD7A3, 0xF900, 0xFAFF, 0xFE10, 0xFE19, 0xFE30, 0xFE52, 0xFE54, 0xFE66, 0xFE68,
  0xFE6B, 0xFF01, 0xFF60, 0xFFE0, 0xFFE6,
  0x16FE0, 0x16FE3, 0x17000, 0x187F7, 0x18800, 0x18AF2, 0x1B000, 0x1B11E, 0x1B150, 0x1B152, 0x1B164, 0x1B167,
  0x1B170, 0x1B2FB, 0x1F004, 0x1F004, 0x1F0CF, 0x1F0CF, 0x1F18E, 0x1F18E, 0x1F191, 0x1F19A, 0x1F200, 0x1F202,
  0x1F210, 0x1F23B, 0x1F240, 0x1F248, 0x1F250, 0x1F251, 0x1F260, 0x1F265, 0x1F300, 0x1F320, 0x1F32D, 0x1F335,
  0x1F337, 0x1F37C, 0x1F37E, 0x1F393, 0x1F3A0, 0x1F3CA, 0x1F3CF, 0x1F3D3, 0x1F3E0, 0x1F3F0, 0x1F3F4, 0x1F3F4,
  0x1F3F8, 0x1F43E, 0x1F440, 0x1F440, 0x1F442, 0x1F4FC, 0x1F4FF, 0x1F53D, 0x1F54B, 0x1F54E, 0x1F550, 0x1F567,
  0x1F57A, 0x1F57A, 0x1F595, 0x1F596, 0x1F5A4, 0x1F5A4, 0x1F5FB, 0x1F64F, 0x1F680, 0x1F6C5, 0x1F6CC, 0x1F6CC,
  0x1F6D0, 0x1F6D2, 0x1F6D5, 0x1F6D5, 0x1F6EB, 0x1F6EC, 0x1F6F4, 0x1F6FA, 0x1F7E0, 0x1F7EB, 0x1F90D, 0x1F971,
  0x1F973, 0x1F976, 0x1F97A, 0x1F9A2, 0x1F9A5, 0x1F9AA, 0x1F9AE, 0x1F9CA, 0x1F9CD, 0x1F9FF, 0x1FA70, 0x1FA73,
  0x1FA78, 0x1FA7A, 0x1FA80, 0x1FA82, 0x1FA90, 0x1FA95, 0x20000, 0x2FFFD, 0x30000, 0x3FFFD
]

function isWide(codePoint: number): boolean {
  if (codePoint < WIDE[0] || codePoint > WIDE[WIDE.length - 1]) return false
  let low = 0
  let high = WIDE.length / 2 - 1
  while (low <= high) {
    const middle = (low + high) >> 1
    if (codePoint > WIDE[middle * 2 + 1]) low = middle + 1
    else if (codePoint < WIDE[middle * 2]) high = middle - 1
    else return true
  }
  return false
}

/** The columns a viewer's cursor moves for `codePoint`: 2 for a wide one, else 1. Controls are not measured. */
export function cellAdvance(codePoint: number): 1 | 2 {
  return isWide(codePoint) ? 2 : 1
}