MCPcopy Create free account
hub / github.com/coder/ghostty-web / proposeDimensions

Method proposeDimensions

lib/addons/fit.ts:140–196  ·  view source on GitHub ↗

* Propose dimensions to fit the terminal to its container * * Calculates cols and rows based on: * - Terminal container element dimensions (clientWidth/Height) * - Terminal element padding * - Font metrics (character cell size) * - Scrollbar width reservation * * @returns Pro

()

Source from the content-addressed store, hash-verified

138 * @returns Proposed dimensions or undefined if cannot calculate
139 */
140 public proposeDimensions(): ITerminalDimensions | undefined {
141 // Need terminal and its DOM element
142 if (!this._terminal?.element) {
143 return undefined;
144 }
145
146 // Access terminal internals to get renderer
147 const terminal = this._terminal as any;
148 const renderer = terminal.renderer;
149
150 if (!renderer || typeof renderer.getMetrics !== 'function') {
151 return undefined;
152 }
153
154 // Get font metrics from renderer
155 const metrics = renderer.getMetrics();
156 if (!metrics || metrics.width === 0 || metrics.height === 0) {
157 return undefined;
158 }
159
160 // Get terminal element (container) dimensions
161 // Use clientWidth/clientHeight to get the INSIDE dimensions (excluding padding)
162 const terminalElement = this._terminal.element;
163
164 // Check if we have clientWidth/clientHeight (DOM element required)
165 if (typeof terminalElement.clientWidth === 'undefined') {
166 return undefined;
167 }
168
169 const elementStyle = window.getComputedStyle(terminalElement);
170
171 // Get the actual content area (inside padding)
172 const paddingTop = Number.parseInt(elementStyle.getPropertyValue('padding-top')) || 0;
173 const paddingBottom = Number.parseInt(elementStyle.getPropertyValue('padding-bottom')) || 0;
174 const paddingLeft = Number.parseInt(elementStyle.getPropertyValue('padding-left')) || 0;
175 const paddingRight = Number.parseInt(elementStyle.getPropertyValue('padding-right')) || 0;
176
177 // Use clientWidth/clientHeight which gives us the inside dimensions
178 // This is stable and doesn't grow with content
179 const containerWidth = terminalElement.clientWidth;
180 const containerHeight = terminalElement.clientHeight;
181
182 // Check for invalid dimensions
183 if (containerWidth === 0 || containerHeight === 0) {
184 return undefined;
185 }
186
187 // Calculate available space (subtract padding since clientWidth includes padding)
188 const availableWidth = containerWidth - paddingLeft - paddingRight - DEFAULT_SCROLLBAR_WIDTH;
189 const availableHeight = containerHeight - paddingTop - paddingBottom;
190
191 // Calculate dimensions (enforce minimums)
192 const cols = Math.max(MINIMUM_COLS, Math.floor(availableWidth / metrics.width));
193 const rows = Math.max(MINIMUM_ROWS, Math.floor(availableHeight / metrics.height));
194
195 return { cols, rows };
196 }
197

Callers 2

fitMethod · 0.95
fit.test.tsFile · 0.80

Calls 1

getMetricsMethod · 0.80

Tested by

no test coverage detected