-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenderer.js
More file actions
255 lines (224 loc) · 7.53 KB
/
renderer.js
File metadata and controls
255 lines (224 loc) · 7.53 KB
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
// Renderer process script
const { ipcRenderer } = require("electron");
const { Terminal } = require("@xterm/xterm");
const { FitAddon } = require("@xterm/addon-fit");
// Terminal variables
let config;
let terminals = {}; // Object of terminals keyed by hostname
let fitAddons = {}; // Object of fit addons keyed by hostname
let lastPasteTime = 0;
const PASTE_DEBOUNCE_MS = 100;
// Listen for F12 to toggle dev tools
window.addEventListener("keydown", (event) => {
if (event.key === "F12") {
ipcRenderer.send("toggle-dev-tools");
event.preventDefault();
}
});
// Handle window resize
window.addEventListener("resize", () => {
Object.values(fitAddons).forEach((fitAddon) => {
if (fitAddon) {
fitAddon.fit();
}
});
});
// ipcRenderer.on(...) will listen for messages from the backend process
ipcRenderer.on("config", (_event, receivedConfig) => {
config = receivedConfig;
populateHostGroupDropdown();
});
ipcRenderer.on("config-error", (_event, errorMessage) => {
const container = document.getElementById("terminals-container");
if (container) {
container.innerHTML = `<div style="color: red; padding: 20px; font-size: 18px;">Error loading configuration: ${errorMessage}</div>`;
}
});
ipcRenderer.on("initialize-terminals", (_event, hosts) => {
initializeTerminals(hosts);
});
ipcRenderer.on("clear-all-terminals", () => {
clearAllTerminals();
});
ipcRenderer.on("terminal-data", (_event, { hostname, data }) => {
if (terminals[hostname]) {
terminals[hostname].write(data);
terminals[hostname].scrollToBottom();
}
});
// Listen for clear command
ipcRenderer.on("terminal-clear", (_event, { hostname }) => {
if (terminals[hostname]) {
terminals[hostname].clear();
}
});
// Populate host group dropdown
function populateHostGroupDropdown() {
const select = document.getElementById("host-group-select");
select.innerHTML = '<option value="">Select Host Group</option>';
for (const groupName in config.hostGroups) {
const option = document.createElement("option");
option.value = groupName;
option.textContent = groupName;
select.appendChild(option);
}
select.addEventListener("change", (event) => {
const groupName = event.target.value;
if (groupName) {
ipcRenderer.send("switch-host-group", groupName);
}
});
}
// Initialize xterm.js terminals
function initializeTerminals(hosts) {
const container = document.getElementById("terminals-container");
if (!container) {
return;
}
for (const hostname of hosts) {
const { wrapper, terminal, fitAddon } = createSingleTerminal(
hostname,
config,
);
container.appendChild(wrapper);
terminals[hostname] = terminal;
fitAddons[hostname] = fitAddon;
// Focus the first terminal
if (hostname === hosts[0]) {
terminal.focus();
}
}
}
// Create single terminal
function createSingleTerminal(hostname, config) {
// Create wrapper
const wrapper = document.createElement("div");
wrapper.className = "terminal-wrapper";
wrapper.style.flex = `1 1 ${config.terminalMinWidth}px`;
wrapper.style.height = `${config.terminalHeight}px`;
// Create title bar
const titleBar = document.createElement("div");
titleBar.className = "terminal-title-bar";
titleBar.textContent = hostname;
wrapper.appendChild(titleBar);
// Create terminal container
const termContainer = document.createElement("div");
termContainer.className = "terminal-container";
wrapper.appendChild(termContainer);
// Create terminal div
const termDiv = document.createElement("div");
termDiv.className = "terminal";
termDiv.id = `terminal-${hostname}`;
termContainer.appendChild(termDiv);
// Create terminal instance
const terminal = new Terminal({
cursorBlink: true,
cursorStyle: "block",
fontFamily:
'"Cascadia Code", "Fira Code", "Source Code Pro", "Monaco", "Menlo", "Ubuntu Mono", monospace',
fontSize: 14,
fontWeight: "normal",
fontWeightBold: "bold",
lineHeight: 1.2,
letterSpacing: 0,
theme: {
background: "#0c0c0c",
foreground: "#ffffff",
cursor: "#ffffff",
cursorAccent: "#000000",
selection: "rgba(255, 255, 255, 0.3)",
black: "#000000",
red: "#cd3131",
green: "#0dbc79",
yellow: "#e5e510",
blue: "#2472c8",
magenta: "#bc3fbc",
cyan: "#11a8cd",
white: "#e5e5e5",
brightBlack: "#666666",
brightRed: "#f14c4c",
brightGreen: "#23d18b",
brightYellow: "#f5f543",
brightBlue: "#3b8eea",
brightMagenta: "#d670d6",
brightCyan: "#29b8db",
brightWhite: "#ffffff",
},
allowProposedApi: true,
});
// Create fit addon
const fitAddon = new FitAddon();
terminal.loadAddon(fitAddon);
// Open terminal in the container
terminal.open(termDiv);
// Fit terminal to container after layout is complete
requestAnimationFrame(() => {
fitAddon.fit();
});
// Handle copy/paste keyboard shortcuts
terminal.attachCustomKeyEventHandler((event) => {
// Handle copy (Ctrl+C or Cmd+C on Mac)
if ((event.ctrlKey || event.metaKey) && event.key === "c") {
// Only copy if there's a selection, otherwise let Ctrl+C work as interrupt
if (terminal.hasSelection()) {
navigator.clipboard.writeText(terminal.getSelection());
return false; // Prevent default behavior
}
return true; // Allow Ctrl+C to work as interrupt if no selection
}
// Handle paste (Ctrl+V or Cmd+V on Mac)
if ((event.ctrlKey || event.metaKey) && event.key === "v") {
if (Date.now() - lastPasteTime < PASTE_DEBOUNCE_MS) return true;
lastPasteTime = Date.now();
navigator.clipboard.readText().then((text) => {
sendTerminalInput(text, hostname);
});
return true; // Having false here for some reason adds the text twice...
}
return true; // Allow all other keys to work normally
});
// Handle terminal input
terminal.onData((data) => {
sendTerminalInput(data, hostname);
});
// Handle terminal resize
terminal.onResize(({ cols, rows }) => {
ipcRenderer.send("terminal-resize", {
hostname: hostname,
cols,
rows,
});
});
return { wrapper, terminal, fitAddon };
}
// Send terminal input to all hosts or specific host
function sendTerminalInput(data, hostname) {
const sendToAll = document.getElementById("send-to-all").checked;
if (sendToAll) {
for (const host of Object.keys(terminals)) {
ipcRenderer.send("terminal-input", {
hostname: host,
data,
});
}
} else {
ipcRenderer.send("terminal-input", {
hostname,
data,
});
}
}
function clearAllTerminals() {
const container = document.getElementById("terminals-container");
if (container) {
container.innerHTML = "";
}
// Clear terminal objects
Object.values(terminals).forEach((terminal) => {
if (terminal) {
terminal.dispose();
}
});
terminals = {};
fitAddons = {};
}