All files / player-terminal/src/tui layout.ts

89.58% Statements 43/48
69.69% Branches 23/33
100% Functions 10/10
89.13% Lines 41/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1135x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x                               82x       56x                 56x 56x 56x       1x       111x       111x 110x     1x 1x 1x 1x             1x             1x 1x       1x       1x     1x 1x           116x 116x     116x       116x 116x     116x 116x 174x   116x 116x    
export const DEFAULT_LANE_WIDTH = 3;
export const WIDE_SCRATCH_LANE_WIDTH: number = DEFAULT_LANE_WIDTH * 2;
export const DEFAULT_GRID_ROWS = 14;
export const MIN_GRID_ROWS = 4;
export const BASE_TUI_RESERVED_LINES = 18;
export const SPLIT_PANEL_INNER_WIDTH = 5;
export const BGA_LANE_GAP = 3;
export const MIN_BGA_ASCII_WIDTH = 8;
export const MIN_BGA_ASCII_HEIGHT = 6;
export const DEFAULT_TERMINAL_COLUMNS = 120;
export const PLAY_PROGRESS_INDICATOR_SIDE_WIDTH = 1;
 
interface TUILayoutOptions {
  showLaneChannels?: boolean;
  hasRandomPatternSummary?: boolean;
  hasAudioDebugLine?: boolean;
}
 
interface TuiBGADisplaySizeOptions extends TUILayoutOptions {
  laneWidths: number[];
  splitAfterIndex: number;
  columns?: number;
  rows?: number;
}
 
export function resolveLaneWidths(lanes: ReadonlyArray<{ isScratch?: boolean }>): number[] {
  return lanes.map((lane) => (lane.isScratch ? WIDE_SCRATCH_LANE_WIDTH : DEFAULT_LANE_WIDTH));
}
 
export function calculateTUIReservedLineCount(options: TUILayoutOptions): number {
  return (
    BASE_TUI_RESERVED_LINES +
    (options.showLaneChannels ? 1 : 0) +
    (options.hasRandomPatternSummary ? 1 : 0) +
    (options.hasAudioDebugLine ? 1 : 0)
  );
}
 
export function calculateTUIGridRowCount(rows: number | undefined, options: TUILayoutOptions): number {
  const reservedLineCount = calculateTUIReservedLineCount(options);
  const terminalRows = rows ?? DEFAULT_GRID_ROWS + reservedLineCount;
  return Math.max(MIN_GRID_ROWS, terminalRows - reservedLineCount);
}
 
export function calculateTUILaneLinesCount(rowCount: number, showLaneChannels = false): number {
  return rowCount + 3 + (showLaneChannels ? 1 : 0);
}
 
export function calculateLaneBlockVisibleWidth(laneWidths: number[], splitAfterIndex: number): number {
  Iif (laneWidths.length <= 0) {
    return calculateLaneSectionVisibleWidth([DEFAULT_LANE_WIDTH]);
  }
 
  if (splitAfterIndex < 0 || splitAfterIndex >= laneWidths.length - 1) {
    return calculateLaneSectionVisibleWidth(laneWidths);
  }
 
  const left = laneWidths.slice(0, splitAfterIndex + 1);
  const right = laneWidths.slice(splitAfterIndex + 1);
  const splitPanelWidth = SPLIT_PANEL_INNER_WIDTH + 2;
  return Math.max(
    1,
    calculateLaneSectionVisibleWidth(left) + splitPanelWidth + calculateLaneSectionVisibleWidth(right),
  );
}
 
export function estimateBgaANSIDisplaySize(options: TuiBGADisplaySizeOptions): { width: number; height: number } {
  const width = Math.max(
    MIN_BGA_ASCII_WIDTH,
    (options.columns ?? DEFAULT_TERMINAL_COLUMNS) -
      calculateLaneBlockVisibleWidth(options.laneWidths, options.splitAfterIndex) -
      calculateProgressIndicatorVisibleWidth(options.laneWidths, options.splitAfterIndex) -
      BGA_LANE_GAP,
  );
  const rowCount = calculateTUIGridRowCount(options.rows, options);
  const height = Math.max(
    MIN_BGA_ASCII_HEIGHT,
    calculateTUILaneLinesCount(rowCount, Boolean(options.showLaneChannels)),
  );
  return { width, height };
}
 
function calculateProgressIndicatorVisibleWidth(laneWidths: number[], splitAfterIndex: number): number {
  Iif (laneWidths.length <= 0) {
    return 0;
  }
  Eif (splitAfterIndex < 0 || splitAfterIndex >= laneWidths.length - 1) {
    return PLAY_PROGRESS_INDICATOR_SIDE_WIDTH;
  }
  return PLAY_PROGRESS_INDICATOR_SIDE_WIDTH * 2;
}
 
export function calculateLaneSectionVisibleWidth(sectionLaneWidths: number[]): number {
  const innerWidth = calculateLaneSectionInnerVisibleWidth(sectionLaneWidths);
  Iif (innerWidth <= 0) {
    return 0;
  }
  return innerWidth + 2;
}
 
function calculateLaneSectionInnerVisibleWidth(sectionLaneWidths: number[]): number {
  const laneCount = Math.max(0, sectionLaneWidths.length);
  Iif (laneCount <= 0) {
    return 0;
  }
  let width = 0;
  for (let index = 0; index < laneCount; index += 1) {
    width += sectionLaneWidths[index] ?? DEFAULT_LANE_WIDTH;
  }
  width += Math.max(0, laneCount - 1);
  return width;
}