aboutsummaryrefslogtreecommitdiffstats
path: root/webapp/src/app/pages/targets/terminals/terminal.component.ts
blob: 0478a08abc9ea56f5609c89769db243db6a82d0a (plain)
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
/**
* @license
* Copyright (C) 2018 "IoT.bzh"
* Author Sebastien Douheret <sebastien@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Component, ElementRef, ViewChild, Input, Output, HostListener, EventEmitter, AfterViewInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import { Terminal } from 'xterm';
import * as fit from 'xterm/lib/addons/fit/fit';

export interface ITerminalFont {
  fontFamily: string;
  fontSize: string;
  lineHeight: number;
  charWidth: number;
  charHeight: number;
}


@Component({
  selector: 'xds-terminal',
  styles: [],
  template: `
    <div #terminalContainer></div>
  `,
})
export class TerminalComponent implements AfterViewInit {

  private _xterm: Terminal;
  private _initDone: boolean;

  @ViewChild('terminalContainer') termContainer: ElementRef;

  @Output() stdin = new EventEmitter<any>();
  @Output() resize = new EventEmitter<{ cols: number, rows: number }>();


  constructor() {
    this._initDone = false;
    Terminal.applyAddon(fit);

    this._xterm = new Terminal({
      cursorBlink: true,
      // useStyle: true,
      scrollback: 1000,
      rows: 24,
      cols: 80,
    });
  }

  // getting the nativeElement only possible after view init
  ngAfterViewInit() {

    // this now finds the #terminal element
    this._xterm.open(this.termContainer.nativeElement);

    // the number of rows will determine the size of the terminal screen
    (<any>this._xterm).fit();

    // Bind input key
    this._xterm.on('data', (data) => {
      // console.log(data.charCodeAt(0));
      this.stdin.emit(this._sanitizeInput(data));
      return false;
    });

    this._initDone = true;
  }

  @Input('stdout')
  set writeData(data) {
    if (this._initDone && data !== undefined) {
      this._xterm.write(data);
    }
  }

  @Input('disable')
  set disable(value: boolean) {
    if (!this._initDone) {
      return;
    }

    this._xterm.setOption('disableStdin', value);

    if (value) {
      this._xterm.blur();
    } else {
      this._xterm.focus();
    }
    this._resize();
  }

  @HostListener('window:resize', ['$event'])
  onWindowResize(event) {
    this._resize();
  }

  /*** Private functions ***/

  private _sanitizeInput(d) {
    // TODO sanitize ?
    return d;
  }

  private _resize() {
    const geom = fit.proposeGeometry(this._xterm);

    // console.log('DEBUG  cols ' + String(geom.cols) + ' rows ' + String(geom.rows));

    if (geom.cols < 0 || geom.cols > 2000 || geom.rows < 0 || geom.rows > 2000) {
      return;
    }

    // Update xterm size
    this._xterm.resize(geom.cols, geom.rows);

    // Send resize event to update remote terminal
    this.resize.emit({ cols: geom.cols, rows: geom.rows });
  }

}