This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appTerminal.ts
220 lines (177 loc) · 5.18 KB
/
appTerminal.ts
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { Terminal } from 'xterm';
import * as pty from 'node-pty';
import { IPty } from 'node-pty';
import { FitAddon } from 'xterm-addon-fit';
import { WebLinksAddon } from 'xterm-addon-web-links';
import { LigaturesAddon } from 'xterm-addon-ligatures';
import { Unicode11Addon } from 'xterm-addon-unicode11';
import Options, { IOptions, ITheme } from '@/options/options';
import AppWatcher from '@/app/appWatcher';
import { remote, clipboard } from 'electron';
export default class AppTerminal {
// The xterm instance
protected xterm: Terminal;
// The ptyProcess instance
private ptyProcess: IPty;
// The fitAddon instance - needed to call fit()
private fitAddon: FitAddon | undefined;
constructor(id: number) {
const options: IOptions = Options.get().getOptions();
// Build the instances
this.xterm = this.buildTerminal(options);
this.ptyProcess = this.buildPty(options.bash);
// Apply themes and addons
this.applyTheme(options.theme);
this.summonTerminal(id);
this.applyAddons();
// Listeners
this.xterm.onResize((data: {cols: number, rows: number}) => this.onResize(data));
this.xterm.onData((data: string) => this.onData(data));
this.xterm.onSelectionChange(() => clipboard.writeText(this.xterm.getSelection(), 'selection'));
this.ptyProcess.onData((data: string) => this.onPtyData(data));
this.ptyProcess.onExit(() => this.exit());
window.onresize = () => this.fit();
// Focus and fit the window
this.fit();
this.focus();
new AppWatcher(this);
}
/**
* Build the terminal instance with the settings
*
* @param IOptions
* @return Terminal
*/
private buildTerminal(options: IOptions): Terminal {
return new Terminal({
cursorBlink: options.cursor.blink,
cursorStyle: options.cursor.style,
fontSize: options.font.size,
fontFamily: options.font.family,
fastScrollModifier: options.fastScrollModifier,
});
}
/**
* Build the pty instance with the good
* cols and rows from xterm
*
* @param bash
* @return IPty
*/
private buildPty(bash: string): IPty {
return pty.spawn(bash, [], {
name: 'xterm-256color',
cols: this.xterm.cols,
rows: this.xterm.rows,
});
}
/**
* Summon the terminal to the element
* in the DOM with the ID #terminal
*
* @param number
* @return void
*/
private summonTerminal(id: number): void {
this.xterm.open(<HTMLElement>document.getElementById(`terminal-${id}`));
}
/**
* Apply the current theme colors
* to the xterm instance
*
* @return void
*/
public applyTheme(theme: ITheme): void {
this.xterm.setOption('theme', theme);
}
/**
* Apply the addons to the xterm instance
*
* @return void
*/
private applyAddons(): void {
this.xterm.loadAddon(this.fitAddon = new FitAddon());
this.xterm.loadAddon(new WebLinksAddon());
this.xterm.loadAddon(new LigaturesAddon());
this.xterm.loadAddon(new Unicode11Addon());
this.xterm.unicode.activeVersion = '11';
}
/**
* Set a option to the xterm instance
*
* @param string
* @param any
* @return void
*/
public setOption(option: string, value: any): void {
this.xterm.setOption(option, value);
}
/**
* Fit xterm thanks to fitAddon
*
* @return void
*/
public fit(): void {
// @ts-ignore
this.fitAddon.fit();
}
/**
* Focus the xterm instance
*
* @return void
*/
public focus(): void {
this.xterm.focus();
}
/**
* Resize the pty process to the specified
* cols and rows
*
* @param data
* @return void
*/
private onResize(data: {cols: number, rows: number}): void {
this.ptyProcess.resize(
Math.max(data ? data.cols : this.xterm.cols, 1),
Math.max(data ? data.rows : this.xterm.rows, 1));
}
/**
* Write the data in the pty instance
*
* @param data
* @return void
*/
public onData(data: string): void {
this.ptyProcess.write(data);
}
/**
* Write the data in the xterm instance
*
* @param data
* @return void
*/
private onPtyData(data: string): void {
this.xterm.write(data);
}
/**
* Called when we exit the process
*
* @return void
*/
public exit(): void {
remote.getCurrentWindow().webContents.send('shortcuts', 'pane:close');
}
/**
* Called when the terminal instance
* is destroyed
*
* @return void
*/
public onDestroy(): void {
this.xterm.dispose();
this.ptyProcess.kill();
}
}
export interface ITerminal {
index: number;
}