Repository navigation
Expand file tree
/
Copy pathrenderer.js
More file actions
70 lines (63 loc) · 2.14 KB
/
Copy pathrenderer.js
File metadata and controls
70 lines (63 loc) · 2.14 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
const term = new Terminal();
const commandHistory = [];
let historyIndex = -1;
let commandBuffer = '';
let currentDir = '';
// Get user directory on load
window.onload = function () {
fetch('/user-home-dir')
.then(response => response.json())
.then(data => {
currentDir = data.homeDir;
term.write(`${currentDir} $ `);
});
};
async function executeCommand(command) {
try {
const response = await fetch('/execute-command', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ command, currentDir })
});
const result = await response.json();
currentDir = result.currentDir || currentDir;
term.write(`\r\n${result.output}`);
term.write(`\r\n${currentDir} $ `);
} catch (error) {
console.error('Command execution error:', error);
term.write(`\r\nError: ${error.message}\r\n${currentDir} $ `);
}
}
function handleArrowNavigation(key) {
if (key === '\x1B[A') { // Up arrow
if (historyIndex > 0) historyIndex--;
} else if (key === '\x1B[B') { // Down arrow
if (historyIndex < commandHistory.length - 1) historyIndex++;
else historyIndex = commandHistory.length;
}
term.write('\x1B[2K\r$ ');
term.write(commandHistory[historyIndex] || '');
commandBuffer = commandHistory[historyIndex] || '';
}
term.open(document.getElementById('terminal'));
term.write(`${currentDir} $ `);
term.onData(data => {
if (data === '\r') { // Enter key
if (commandBuffer.trim()) {
commandHistory.push(commandBuffer.trim());
historyIndex = commandHistory.length;
executeCommand(commandBuffer.trim());
commandBuffer = '';
}
} else if (data === '\u007F') { // Backspace
if (commandBuffer.length > 0) {
commandBuffer = commandBuffer.slice(0, -1);
term.write('\b \b');
}
} else if (data === '\x1B[A' || data === '\x1B[B') { // Arrow navigation
handleArrowNavigation(data);
} else {
commandBuffer += data;
term.write(data);
}
});