2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
6 import { Terminal, ITerminalAddon } from 'xterm';
8 interface ITerminalDimensions {
10 * The number of rows in the terminal.
15 * The number of columns in the terminal.
20 const MINIMUM_COLS = 2;
21 const MINIMUM_ROWS = 1;
23 export class FitAddon implements ITerminalAddon {
24 private _terminal: Terminal | undefined;
28 public activate(terminal: Terminal): void {
29 this._terminal = terminal;
32 public dispose(): void {}
35 const dims = this.proposeDimensions();
36 if (!dims || !this._terminal) {
40 // TODO: Remove reliance on private API
41 const core = (<any>this._terminal)._core;
43 // Force a full render
44 if (this._terminal.rows !== dims.rows || this._terminal.cols !== dims.cols) {
45 core._renderService.clear();
46 this._terminal.resize(dims.cols, dims.rows);
50 public proposeDimensions(): ITerminalDimensions | undefined {
51 if (!this._terminal) {
55 if (!this._terminal.element || !this._terminal.element.parentElement) {
59 // TODO: Remove reliance on private API
60 const core = (<any>this._terminal)._core;
62 const parentElementStyle = window.getComputedStyle(this._terminal.element.parentElement);
63 const parentElementHeight = parseInt(parentElementStyle.getPropertyValue('height'));
64 const parentElementWidth = Math.max(0, parseInt(parentElementStyle.getPropertyValue('width')));
65 const elementStyle = window.getComputedStyle(this._terminal.element);
66 const elementPadding = {
67 top: parseInt(elementStyle.getPropertyValue('padding-top')),
68 bottom: parseInt(elementStyle.getPropertyValue('padding-bottom')),
69 right: parseInt(elementStyle.getPropertyValue('padding-right')),
70 left: parseInt(elementStyle.getPropertyValue('padding-left'))
72 const elementPaddingVer = elementPadding.top + elementPadding.bottom;
73 const elementPaddingHor = elementPadding.right + elementPadding.left;
74 const availableHeight = parentElementHeight - elementPaddingVer;
75 const availableWidth = parentElementWidth - elementPaddingHor - core.viewport.scrollBarWidth;
77 cols: Math.max(MINIMUM_COLS, Math.floor(availableWidth / core._renderService.dimensions.actualCellWidth)),
78 rows: Math.max(MINIMUM_ROWS, Math.floor(availableHeight / core._renderService.dimensions.actualCellHeight))