All files / player-terminal/src/cli selection-format.ts

95.13% Statements 137/144
84.02% Branches 121/144
100% Functions 21/21
95.1% Lines 136/143

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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328    4x 4x                                                                 8x 8x 8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x     8x   8x 8x   8x                       4x 4x 4x 4x 4x 4x 4x 4x       8x 1x   7x 1x 1x 1x 1x 1x 1x 1x 1x     6x 8x 8x         8x 8x         8x 8x 8x       14x 3x   11x       77x 77x       17x 1x   16x 16x 13x   3x     3x 1x   2x 1x   1x               24x 5x   19x 19x 3x   16x       16x     16x 16x 14x 1x   13x     13x 8x   5x 4x   1x 1x     2x       2x 2x       21x 5x 5x   16x 1x   15x 2x   13x 13x       14x 14x 3x   11x       6x       14x     14x       14x                           14x 42x 14x 13x   1x       42x 42x       171x 155x   16x 4x   12x 12x 12x 232x 232x 12x   220x 220x   12x       77x 77x 77x       488x 488x 3614x   488x       3614x 3614x     3614x                 2x   3612x       3612x 3553x   59x                                  
import type { BeMusicPlayLevel } from '@be-music/chart-source';
 
const PLAY_LEVEL_HEADER = 'PLEVEL';
const PLAY_LEVEL_TEXT_MAX_WIDTH = 4;
 
export interface SelectionDisplayEntry {
  kind: 'random' | 'group' | 'chart';
  label?: string;
  fileLabel?: string;
  totalNotes?: number;
  player?: number;
  difficulty?: number;
  /** Where an estimated `difficulty` comes from: the chart's names or its note count; absent for the chart's own. */
  difficultyBasis?: 'names' | 'notes';
  rank?: number;
  rankLabel?: string;
  playLevel?: BeMusicPlayLevel;
  bpmInitial?: number;
  bpmMin?: number;
  bpmMax?: number;
}
 
export interface SelectionColumnLayout {
  fileWidth: number;
  playerWidth: number;
  difficultyWidth: number;
  rankWidth: number;
  playLevelWidth: number;
  bpmWidth: number;
  notesWidth: number;
}
 
export function createSelectionColumnLayout(
  itemLabelWidth: number,
  entries: readonly SelectionDisplayEntry[],
): SelectionColumnLayout {
  let playerWidth = 'PLAYER'.length;
  let difficultyWidth = 'DIFF'.length;
  let rankWidth = 'RANK'.length;
  let playLevelWidth = PLAY_LEVEL_HEADER.length;
  let bpmWidth = 'BPM'.length;
  let notesWidth = 'NOTES'.length;
  for (const entry of entries) {
    Iif (entry.kind !== 'chart') {
      continue;
    }
    playerWidth = Math.max(playerWidth, formatPlayerLabel(entry.player).length);
    difficultyWidth = Math.max(difficultyWidth, formatDifficultyLabel(entry.difficulty, entry.difficultyBasis).length);
    rankWidth = Math.max(rankWidth, formatRankCellValue(entry).length);
    playLevelWidth = Math.max(playLevelWidth, measureDisplayWidth(formatPlayLevelCellValue(entry.playLevel)));
    bpmWidth = Math.max(bpmWidth, formatBPMLabel(entry.bpmMin, entry.bpmInitial, entry.bpmMax).length);
    notesWidth = Math.max(notesWidth, formatTotalNotesLabel(entry.totalNotes).length);
  }
 
  const spacingWidth = 12;
  const fixedMetaWidth =
    playerWidth + difficultyWidth + rankWidth + playLevelWidth + bpmWidth + notesWidth + spacingWidth;
  const fileWidth = Math.max(1, itemLabelWidth - fixedMetaWidth);
 
  return {
    fileWidth,
    playerWidth,
    difficultyWidth,
    rankWidth,
    playLevelWidth,
    bpmWidth,
    notesWidth,
  };
}
 
export function formatSelectionColumnHeader(layout: SelectionColumnLayout): string {
  const file = formatColumnCell('File', layout.fileWidth);
  const player = formatColumnCell('PLAYER', layout.playerWidth, 'right');
  const difficulty = formatColumnCell('DIFF', layout.difficultyWidth, 'right');
  const rank = formatColumnCell('RANK', layout.rankWidth, 'right');
  const playLevel = formatColumnCell(PLAY_LEVEL_HEADER, layout.playLevelWidth, 'left');
  const bpm = formatColumnCell('BPM', layout.bpmWidth, 'right');
  const notes = formatColumnCell('NOTES', layout.notesWidth, 'right');
  return `${file}  ${player}  ${difficulty}  ${rank}  ${playLevel}  ${bpm}  ${notes}`;
}
 
export function formatSelectionEntryLabel(entry: SelectionDisplayEntry, layout: SelectionColumnLayout): string {
  if (entry.kind === 'group') {
    return entry.label ?? '';
  }
  if (entry.kind === 'random') {
    const file = formatColumnCell(entry.label ?? '', layout.fileWidth);
    const player = formatColumnCell('-', layout.playerWidth, 'right');
    const difficulty = formatColumnCell('-', layout.difficultyWidth, 'right');
    const rank = formatColumnCell('-', layout.rankWidth, 'right');
    const playLevel = formatColumnCell('-', layout.playLevelWidth, 'right');
    const bpm = formatColumnCell('-', layout.bpmWidth, 'right');
    const notes = formatColumnCell('-', layout.notesWidth, 'right');
    return `${file}  ${player}  ${difficulty}  ${rank}  ${playLevel}  ${bpm}  ${notes}`;
  }
 
  const file = formatColumnCell(`  ${entry.fileLabel ?? ''}`, layout.fileWidth);
  const player = formatColumnCell(formatPlayerLabel(entry.player), layout.playerWidth, 'right');
  const difficulty = formatColumnCell(
    formatDifficultyLabel(entry.difficulty, entry.difficultyBasis),
    layout.difficultyWidth,
    'right',
  );
  const rank = formatColumnCell(formatRankCellValue(entry), layout.rankWidth, 'right');
  const playLevel = formatColumnCell(
    formatPlayLevelCellValue(entry.playLevel),
    layout.playLevelWidth,
    resolvePlayLevelCellAlign(entry.playLevel),
  );
  const bpm = formatColumnCell(formatBPMLabel(entry.bpmMin, entry.bpmInitial, entry.bpmMax), layout.bpmWidth, 'right');
  const notes = formatColumnCell(formatTotalNotesLabel(entry.totalNotes), layout.notesWidth, 'right');
  return `${file}  ${player}  ${difficulty}  ${rank}  ${playLevel}  ${bpm}  ${notes}`;
}
 
function formatRankCellValue(entry: SelectionDisplayEntry): string {
  if (typeof entry.rankLabel === 'string' && entry.rankLabel.length > 0) {
    return entry.rankLabel;
  }
  return formatRankLabel(entry.rank);
}
 
function formatColumnCell(value: string, width: number, align: 'left' | 'right' = 'left'): string {
  const truncated = truncateForDisplay(value, width);
  return padDisplayWidth(truncated, width, align);
}
 
export function formatPlayerLabel(value: number | undefined): string {
  if (typeof value !== 'number') {
    return '-';
  }
  const normalized = Math.floor(value);
  if (normalized === 1) {
    return 'SINGLE';
  }
  Iif (normalized === 2) {
    return 'COUPLE';
  }
  if (normalized === 3) {
    return 'DOUBLE';
  }
  if (normalized === 4) {
    return 'BATTLE';
  }
  return String(normalized);
}
 
/**
 * A difficulty 1–5 as its number, followed by `?` when it was estimated from the chart's names (`3?`) and `??` when
 * from its note count (`3??`); `-` otherwise.
 */
export function formatDifficultyLabel(value: number | undefined, basis?: 'names' | 'notes'): string {
  if (typeof value !== 'number' || !Number.isFinite(value)) {
    return '-';
  }
  const normalized = Math.trunc(value);
  if (normalized < 1 || normalized > 5) {
    return '-';
  }
  return basis === 'names' ? `${normalized}?` : basis === 'notes' ? `${normalized}??` : String(normalized);
}
 
export function formatRankLabel(value: number | undefined): string {
  Iif (typeof value !== 'number' || !Number.isFinite(value)) {
    return '-';
  }
  const normalized = Math.trunc(value);
  if (normalized === value) {
    if (normalized === 0) {
      return 'VERY HARD';
    }
    Iif (normalized === 1) {
      return 'HARD';
    }
    if (normalized === 2) {
      return 'NORMAL';
    }
    if (normalized === 3) {
      return 'EASY';
    }
    Eif (normalized === 4) {
      return 'VERY EASY';
    }
  }
  return formatRankValue(value);
}
 
function formatRankValue(value: number): string {
  const rounded = Math.round(value * 100) / 100;
  return rounded.toFixed(2).replace(/(?:\.0+|(\.\d+?)0+)$/, '$1');
}
 
export function formatPlayLevelLabel(value: BeMusicPlayLevel | undefined): string {
  if (typeof value === 'string') {
    const normalized = value.trim();
    return normalized.length > 0 ? normalized : '-';
  }
  if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
    return '-';
  }
  if (value === 0) {
    return '?';
  }
  const rounded = Math.round(value * 100) / 100;
  return rounded.toFixed(2).replace(/(?:\.0+|(\.\d+?)0+)$/, '$1');
}
 
function formatPlayLevelCellValue(value: BeMusicPlayLevel | undefined): string {
  const label = formatPlayLevelLabel(value);
  if (typeof value === 'string') {
    return truncateForDisplay(label, PLAY_LEVEL_TEXT_MAX_WIDTH);
  }
  return label;
}
 
function resolvePlayLevelCellAlign(value: BeMusicPlayLevel | undefined): 'left' | 'right' {
  return typeof value === 'number' ? 'right' : 'left';
}
 
function formatTotalNotesLabel(value: number | undefined): string {
  Iif (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
    return '-';
  }
  return String(Math.floor(value));
}
 
function formatBPMLabel(min: number | undefined, initial: number | undefined, max: number | undefined): string {
  Iif (
    typeof min !== 'number' ||
    typeof initial !== 'number' ||
    typeof max !== 'number' ||
    !Number.isFinite(min) ||
    !Number.isFinite(initial) ||
    !Number.isFinite(max) ||
    min <= 0 ||
    initial <= 0 ||
    max <= 0
  ) {
    return '-';
  }
 
  const values = [formatBPMValue(min), formatBPMValue(initial), formatBPMValue(max)];
  const collapsed = values.filter((value, index) => index === 0 || value !== values[index - 1]);
  if (collapsed.length <= 1) {
    return collapsed[0] ?? '-';
  }
  return collapsed.join('-');
}
 
function formatBPMValue(value: number): string {
  const rounded = Math.round(value * 100) / 100;
  return rounded.toFixed(2).replace(/(?:\.0+|(\.\d+?)0+)$/, '$1');
}
 
export function truncateForDisplay(value: string, width: number): string {
  if (measureDisplayWidth(value) <= width) {
    return value;
  }
  if (width <= 1) {
    return width <= 0 ? '' : '…'.slice(0, width);
  }
  let result = '';
  let consumedWidth = 0;
  for (const char of value) {
    const charWidth = measureDisplayWidth(char);
    if (consumedWidth + charWidth > width - 1) {
      break;
    }
    result += char;
    consumedWidth += charWidth;
  }
  return `${result}…`;
}
 
function padDisplayWidth(value: string, width: number, align: 'left' | 'right'): string {
  const paddingWidth = Math.max(0, width - measureDisplayWidth(value));
  const padding = ' '.repeat(paddingWidth);
  return align === 'right' ? `${padding}${value}` : `${value}${padding}`;
}
 
function measureDisplayWidth(value: string): number {
  let width = 0;
  for (const char of value) {
    width += measureCharacterDisplayWidth(char);
  }
  return width;
}
 
function measureCharacterDisplayWidth(char: string): number {
  const codePoint = char.codePointAt(0);
  Iif (codePoint === undefined) {
    return 0;
  }
  if (
    codePoint === 0 ||
    codePoint < 32 ||
    (codePoint >= 0x7f && codePoint < 0xa0) ||
    codePoint === 0x200d ||
    codePoint === 0xfe0e ||
    codePoint === 0xfe0f ||
    /\p{Mark}/u.test(char)
  ) {
    return 0;
  }
  return isFullWidthCodePoint(codePoint) ? 2 : 1;
}
 
function isFullWidthCodePoint(codePoint: number): boolean {
  if (codePoint < 0x1100) {
    return false;
  }
  return (
    codePoint <= 0x115f ||
    codePoint === 0x2329 ||
    codePoint === 0x232a ||
    (codePoint >= 0x2e80 && codePoint <= 0xa4cf && codePoint !== 0x303f) ||
    (codePoint >= 0xac00 && codePoint <= 0xd7a3) ||
    (codePoint >= 0xf900 && codePoint <= 0xfaff) ||
    (codePoint >= 0xfe10 && codePoint <= 0xfe19) ||
    (codePoint >= 0xfe30 && codePoint <= 0xfe6f) ||
    (codePoint >= 0xff00 && codePoint <= 0xff60) ||
    (codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
    (codePoint >= 0x1f300 && codePoint <= 0x1f64f) ||
    (codePoint >= 0x1f680 && codePoint <= 0x1f6ff) ||
    (codePoint >= 0x1f900 && codePoint <= 0x1f9ff) ||
    (codePoint >= 0x20000 && codePoint <= 0x3fffd)
  );
}