-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
terminal.jsx
311 lines (278 loc) · 13 KB
/
terminal.jsx
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import cockpit from "cockpit";
import 'cockpit-dark-theme'; // once per page
import '../lib/patternfly/patternfly-5-cockpit.scss';
import React from "react";
import { createRoot } from "react-dom/client";
import { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect/index.js";
import { NumberInput } from "@patternfly/react-core/dist/esm/components/NumberInput/index.js";
import { Toolbar, ToolbarContent, ToolbarGroup, ToolbarItem } from "@patternfly/react-core/dist/esm/components/Toolbar/index.js";
import { Alert, AlertActionCloseButton, AlertActionLink } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
import { fsinfo } from "cockpit/fsinfo";
import { Button } from '@patternfly/react-core';
import "./terminal.scss";
import { Terminal } from "cockpit-components-terminal.jsx";
const _ = cockpit.gettext;
(function() {
cockpit.translate();
/*
* A terminal component for the cockpit user.
*
* Uses the Terminal component from base1 internally, but adds a header
* with title and Reset button.
*
* Spawns the user's shell in the user's home directory.
*/
class UserTerminal extends React.Component {
createChannel(user, dir) {
const ch = cockpit.channel({
payload: "stream",
spawn: [user.shell || "/bin/bash"],
environ: [
"TERM=xterm-256color",
],
directory: dir || user.home || "/",
pty: true,
binary: true,
});
ch.addEventListener("ready", (_, msg) => this.setState({ pid: msg.pid }));
ch.addEventListener("close", () => this.setState({ pid: null }));
return ch;
}
constructor(props) {
super(props);
let theme = localStorage.getItem('terminal:theme');
let size = localStorage.getItem('terminal:font-size');
// HACK: Try to read the configuration from localStorage, if it does not exists fall back
// to the old configuration stored in a browser's cookie. After enough time has been
// passed this can be dropped.
if (theme === null || theme === "") {
theme = document.cookie.replace(/(?:(?:^|.*;\s*)theme_cookie\s*=\s*([^;]*).*$)|^.*$/, "$1");
if (theme !== "") {
localStorage.setItem('terminal:theme', theme);
this.invalidateCookie("theme_cookie");
}
}
if (size === null || size === "") {
size = document.cookie.replace(/(?:(?:^|.*;\s*)size_cookie\s*=\s*([^;]*).*$)|^.*$/, "$1");
if (size !== "") {
localStorage.setItem('terminal:font-size', size);
this.invalidateCookie("size_cookie");
}
}
this.state = {
title: 'Terminal',
theme: theme || "black-theme",
size: parseInt(size) || 16,
changePathBusy: false,
pathError: null,
pid: null,
};
this.onTitleChanged = this.onTitleChanged.bind(this);
this.onResetClick = this.onResetClick.bind(this);
this.onThemeChanged = this.onThemeChanged.bind(this);
this.onPlus = this.onPlus.bind(this);
this.onMinus = this.onMinus.bind(this);
this.forceChangeDirectory = this.forceChangeDirectory.bind(this);
this.onNavigate = this.onNavigate.bind(this);
this.dismiss = this.dismiss.bind(this);
this.terminalRef = React.createRef();
this.resetButtonRef = React.createRef();
this.minSize = 6;
this.maxSize = 40;
}
async componentDidMount() {
cockpit.addEventListener("locationchanged", this.onNavigate);
let dir;
if (cockpit.location.options.path) {
const validPath = await this.readyPath();
if (validPath) {
dir = cockpit.location.options.path;
}
}
const user = await cockpit.user();
this.setState({ user, channel: this.createChannel(user, dir) });
}
componentWillUnmount() {
cockpit.removeEventListener("locationchanged", this.onNavigate);
}
onTitleChanged(title) {
this.setState({ title });
}
invalidateCookie(key) {
const cookie = key + "=''" +
"; path=/; Max-Age=0;";
document.cookie = cookie;
}
forceChangeDirectory() {
this.setState(prevState => ({
channel: this.createChannel(prevState.user, cockpit.location.options.path),
changePathBusy: false,
}));
}
dismiss() {
this.setState({
pathError: null,
changePathBusy: false,
});
cockpit.location.replace("");
}
async onNavigate() {
// Clear old path errors
this.setState({
pathError: null,
changePathBusy: false,
});
// If there's no path to change to, then we're done here
if (!cockpit.location.options.path) {
return;
}
const changeNow = await this.readyPath();
if (changeNow) {
this.setState(prevState => ({ channel: this.createChannel(prevState.user, cockpit.location.options.path) }));
}
}
async readyPath() {
// Check if path we're changing to exists
try {
const info = await fsinfo(String(cockpit.location.options.path), ['type']);
if (info.type !== "dir") {
this.setState({ pathError: cockpit.format(_("$0 is not a directory"), cockpit.location.options.path) });
return false;
}
} catch (err) {
this.setState({ pathError: cockpit.format(_("$0 does not exist"), cockpit.location.options.path) });
return false;
}
if (this.state.pid !== null) {
// Check if current shell has a process running in it, ie it's busy
const command = "grep -qr '^PPid:[[:space:]]*" + this.state.pid + "$' /proc/*/status";
try {
await cockpit.script(command, [], { err: "message" });
this.setState({ changePathBusy: true });
return false;
} catch {
return true;
}
}
return true;
}
onPlus() {
this.setState((state, _) => {
localStorage.setItem('terminal:font-size', state.size + 1);
return { size: state.size + 1 };
});
}
onMinus() {
this.setState((state, _) => {
localStorage.setItem('terminal:font-size', state.size - 1);
return { size: state.size - 1 };
});
}
onThemeChanged(_, value) {
this.setState({ theme: value });
localStorage.setItem('terminal:theme', value);
}
onResetClick(event) {
if (event.button !== 0)
return;
if (!this.state.channel.valid && this.state.user)
this.setState(prevState => ({ channel: this.createChannel(prevState.user) }));
else
this.terminalRef.current.reset();
// don't focus the button, but keep it on the terminal
this.resetButtonRef.current.blur();
this.terminalRef.current.focus();
}
render() {
const terminal = this.state.channel
? <Terminal ref={this.terminalRef}
channel={this.state.channel}
theme={this.state.theme}
fontSize={this.state.size}
parentId="the-terminal"
onTitleChanged={this.onTitleChanged} />
: <span>Loading...</span>;
return (
<div className="console-ct-container">
<div className="terminal-group">
<tt className="terminal-title">{this.state.title}</tt>
<Toolbar id="toolbar">
<ToolbarContent>
<ToolbarGroup>
<ToolbarItem variant="label" id="size-select">
{_("Font size")}
</ToolbarItem>
<ToolbarItem>
<NumberInput
className="font-size"
value={this.state.size}
min={this.minSize}
max={this.maxSize}
onMinus={this.onMinus}
onPlus={this.onPlus}
inputAriaLabel={_("Font size")}
minusBtnAriaLabel={_("Decrease by one")}
plusBtnAriaLabel={_("Increase by one")}
widthChars={2}
/>
</ToolbarItem>
</ToolbarGroup>
<ToolbarGroup>
<ToolbarItem variant="label" id="theme-select">
{_("Appearance")}
</ToolbarItem>
<ToolbarItem>
<FormSelect onChange={this.onThemeChanged}
aria-labelledby="theme-select"
value={this.state.theme}>
<FormSelectOption value='black-theme' label={_("Black")} />
<FormSelectOption value='dark-theme' label={_("Dark")} />
<FormSelectOption value='light-theme' label={_("Light")} />
<FormSelectOption value='white-theme' label={_("White")} />
</FormSelect>
</ToolbarItem>
</ToolbarGroup>
<ToolbarItem>
<button ref={this.resetButtonRef}
className="pf-v5-c-button pf-m-secondary terminal-reset"
onClick={this.onResetClick}>{_("Reset")}</button>
</ToolbarItem>
</ToolbarContent>
</Toolbar>
</div>
<div className="ct-terminal-dir-alert">
{this.state.pathError && <Alert isInline
title={_("Unable to open directory")}
variant="warning"
actionClose={<AlertActionCloseButton onClose={this.dismiss} />}>
<p>{_(this.state.pathError)}</p>
</Alert>
}
{this.state.changePathBusy && <Alert isInline
title={_("Running process prevents directory change")}
variant="danger"
actionClose={<AlertActionCloseButton onClose={() =>
this.setState({ changePathBusy: false })} />}
actionLinks={
<>
<Button variant="danger" size="sm" onClick={this.forceChangeDirectory}>{_("Change directory")}</Button>
<AlertActionLink onClick={this.dismiss}>{_("Cancel")}</AlertActionLink>
</>
}>
{_("Changing the directory will forcefully stop the currently running process. The process can also be stopped manually in the terminal before continuing.")}
</Alert>
}
</div>
<div className={"terminal-body " + this.state.theme} id="the-terminal">
{terminal}
</div>
</div>
);
}
}
UserTerminal.displayName = "UserTerminal";
const root = createRoot(document.getElementById('terminal'));
root.render(<UserTerminal />);
/* And show the body */
document.body.removeAttribute("hidden");
}());