All files / player-terminal/src/cli music-select-navigation.ts

78.57% Statements 55/70
77.31% Branches 75/97
100% Functions 7/7
77.94% Lines 53/68

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                                              8x     8x 8x   8x     8x     8x 3x   5x 3x   2x     2x     2x     2x 1x   1x 1x                           5x 1x   4x 2x   2x       3x     3x 3x 3x 3x               8x 8x 8x     8x     8x 8x 8x 8x                   5x     5x 5x 16x 2x     3x 18x 18x 2x     1x             4x 1x   3x 4x 1x   2x 1x   1x 1x        
import type readline from 'node:readline';
 
export type ResultScreenAction = 'enter' | 'escape' | 'ctrl-c' | 'replay';
export type MusicSelectPageDirection = 'up' | 'down';
export type MusicSelectDifficultyFilter = 1 | 2 | 3 | 4 | 5;
export type MusicSelectNavigationAction =
  | 'move-up'
  | 'move-down'
  | 'page-up'
  | 'page-down'
  | 'first'
  | 'last'
  | 'confirm'
  | 'toggle-auto'
  | 'increase-high-speed'
  | 'decrease-high-speed'
  | 'escape'
  | 'ctrl-c';
 
export function resolveMusicSelectNavigationAction(
  chunk: string | undefined,
  key: readline.Key,
): MusicSelectNavigationAction | undefined {
  Iif (key.sequence === '\u0003') {
    return 'ctrl-c';
  }
  const lowerChunk = typeof chunk === 'string' ? chunk.toLowerCase() : '';
  const keyName = key.name?.toLowerCase();
 
  Iif (keyName === 'up' || lowerChunk === 'k') {
    return 'move-up';
  }
  Iif (keyName === 'down' || lowerChunk === 'j') {
    return 'move-down';
  }
  if (keyName === 'left' || keyName === 'pageup' || lowerChunk === 'h' || (key.ctrl && keyName === 'b')) {
    return 'page-up';
  }
  if (keyName === 'right' || keyName === 'pagedown' || lowerChunk === 'l' || (key.ctrl && keyName === 'f')) {
    return 'page-down';
  }
  Iif (keyName === 'home' || lowerChunk === 'g') {
    return 'first';
  }
  Iif (keyName === 'end' || chunk === 'G') {
    return 'last';
  }
  Iif (keyName === 'return' || keyName === 'enter') {
    return 'confirm';
  }
  if (chunk === 's') {
    return 'increase-high-speed';
  }
  Eif (chunk === 'S') {
    return 'decrease-high-speed';
  }
  if (lowerChunk === 'a') {
    return 'toggle-auto';
  }
  if (keyName === 'escape' || key.sequence === '\u001b') {
    return 'escape';
  }
  return undefined;
}
 
export function resolveMusicSelectDifficultyFilter(
  chunk: string | undefined,
): MusicSelectDifficultyFilter | null | undefined {
  if (chunk === '0') {
    return null;
  }
  if (chunk === '1' || chunk === '2' || chunk === '3' || chunk === '4' || chunk === '5') {
    return Number.parseInt(chunk, 10) as MusicSelectDifficultyFilter;
  }
  return undefined;
}
 
export function resolveCircularSelectableIndex(current: number, delta: number, length: number): number {
  Iif (!Number.isFinite(length) || length <= 0) {
    return 0;
  }
  const safeCurrent = Number.isFinite(current) ? Math.floor(current) : 0;
  const safeDelta = Number.isFinite(delta) ? Math.floor(delta) : 0;
  const next = (safeCurrent + safeDelta) % length;
  return next >= 0 ? next : next + length;
}
 
export function resolveVisibleEntryRange(
  selectedIndex: number,
  entryCount: number,
  listRows: number,
): { start: number; end: number } {
  const safeEntryCount = Number.isFinite(entryCount) ? Math.max(0, Math.floor(entryCount)) : 0;
  const safeListRows = Number.isFinite(listRows) ? Math.max(1, Math.floor(listRows)) : 1;
  Iif (safeEntryCount <= 0) {
    return { start: 0, end: 0 };
  }
  const safeSelectedIndex = Number.isFinite(selectedIndex)
    ? Math.max(0, Math.min(safeEntryCount - 1, Math.floor(selectedIndex)))
    : 0;
  const page = Math.floor(safeSelectedIndex / safeListRows);
  const start = page * safeListRows;
  const end = Math.min(safeEntryCount, start + safeListRows);
  return { start, end };
}
 
export function resolvePageSelectableIndex(
  selectableIndexes: number[],
  selectedIndex: number,
  entryCount: number,
  listRows: number,
  direction: MusicSelectPageDirection,
): number {
  Iif (selectableIndexes.length === 0) {
    return 0;
  }
  const { start, end } = resolveVisibleEntryRange(selectedIndex, entryCount, listRows);
  if (direction === 'down') {
    const next = selectableIndexes.find((index) => index >= end);
    return typeof next === 'number' ? next : selectableIndexes[0]!;
  }
 
  for (let index = selectableIndexes.length - 1; index >= 0; index -= 1) {
    const candidate = selectableIndexes[index]!;
    if (candidate < start) {
      return candidate;
    }
  }
  return selectableIndexes[selectableIndexes.length - 1]!;
}
 
export function resolveResultScreenActionFromKey(
  chunk: string | undefined,
  key: readline.Key,
): ResultScreenAction | undefined {
  if (key.sequence === '\u0003') {
    return 'ctrl-c';
  }
  const keyName = key.name?.toLowerCase();
  if (keyName === 'return' || keyName === 'enter') {
    return 'enter';
  }
  if (keyName === 'escape' || key.sequence === '\u001b') {
    return 'enter';
  }
  Eif (typeof chunk === 'string' && chunk.toLowerCase() === 'r') {
    return 'replay';
  }
  return undefined;
}