controller and vsorc data viewers done
[VSoRC/.git] / node_modules / xterm-addon-fit / src / FitAddon.ts
1 /**
2  * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
6 import { Terminal, ITerminalAddon } from 'xterm';
7
8 interface ITerminalDimensions {
9   /**
10    * The number of rows in the terminal.
11    */
12   rows: number;
13
14   /**
15    * The number of columns in the terminal.
16    */
17   cols: number;
18 }
19
20 const MINIMUM_COLS = 2;
21 const MINIMUM_ROWS = 1;
22
23 export class FitAddon implements ITerminalAddon {
24   private _terminal: Terminal | undefined;
25
26   constructor() {}
27
28   public activate(terminal: Terminal): void {
29     this._terminal = terminal;
30   }
31
32   public dispose(): void {}
33
34   public fit(): void {
35     const dims = this.proposeDimensions();
36     if (!dims || !this._terminal) {
37       return;
38     }
39
40     // TODO: Remove reliance on private API
41     const core = (<any>this._terminal)._core;
42
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);
47     }
48   }
49
50   public proposeDimensions(): ITerminalDimensions | undefined {
51     if (!this._terminal) {
52       return undefined;
53     }
54
55     if (!this._terminal.element || !this._terminal.element.parentElement) {
56       return undefined;
57     }
58
59     // TODO: Remove reliance on private API
60     const core = (<any>this._terminal)._core;
61
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'))
71     };
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;
76     const geometry = {
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))
79     };
80     return geometry;
81   }
82 }