(input: DisplaySizeInput)
| 38 | * @returns Display dimensions in terminal cells |
| 39 | */ |
| 40 | export function calculateDisplaySize(input: DisplaySizeInput): DisplaySize { |
| 41 | const { width, height, availableWidth } = input |
| 42 | |
| 43 | // Calculate max width with padding |
| 44 | const maxWidth = Math.max(1, Math.min(availableWidth - 4, MAX_DISPLAY_WIDTH)) |
| 45 | |
| 46 | // Fallback when dimensions are unknown or invalid |
| 47 | if (!width || !height || width <= 0 || height <= 0) { |
| 48 | const fallbackWidth = Math.max(1, Math.floor(maxWidth * 0.5)) |
| 49 | const fallbackHeight = Math.max(1, Math.floor(fallbackWidth / CELL_ASPECT_RATIO)) |
| 50 | return { width: fallbackWidth, height: fallbackHeight } |
| 51 | } |
| 52 | |
| 53 | const aspectRatio = width / height |
| 54 | |
| 55 | // Calculate natural cell width based on image pixel dimensions |
| 56 | // This prevents tiny images from being blown up too large |
| 57 | const naturalCellWidth = Math.ceil(width / PIXELS_PER_CELL) |
| 58 | |
| 59 | // Use the smaller of natural width and max available width |
| 60 | const displayWidth = Math.max(1, Math.min(naturalCellWidth, maxWidth)) |
| 61 | |
| 62 | // Calculate height preserving aspect ratio, accounting for cell aspect ratio |
| 63 | // Since cells are 2:1, we divide by CELL_ASPECT_RATIO to get proper visual proportions |
| 64 | const displayHeight = Math.max(1, Math.floor(displayWidth / aspectRatio / CELL_ASPECT_RATIO)) |
| 65 | |
| 66 | return { width: displayWidth, height: displayHeight } |
| 67 | } |
no outgoing calls
no test coverage detected