-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
131 lines (106 loc) · 4.19 KB
/
scripts.js
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
let buffer = []; // Buffer to store commands and outputs
let keySymbols = ""; // Buffer to hold key symbols as they are typed
document.addEventListener("DOMContentLoaded", () => {
const commandInput = document.getElementById("commandInput");
const bufferDiv = document.getElementById("buffer");
// Event listener for keydown
commandInput.addEventListener("keydown", (event) => {
handleKeyPress(event, bufferDiv);
});
});
// Handle single key press
function handleKeyPress(event, bufferDiv) {
const commandInput = document.getElementById("commandInput");
// Append the key symbol unless it's Enter
if (event.key !== "Enter") {
keySymbols += event.key;
} else {
event.preventDefault(); // Prevent default behavior (new line)
parseAndProcessCommand(keySymbols.trim(), bufferDiv); // Parse and process entered command
keySymbols = ""; // Reset key symbol buffer
commandInput.value = ""; // Clear input field
}
// Display the captured symbols in real-time
displayKeySymbols(bufferDiv);
}
// Display the currently typed key symbols in real-time
function displayKeySymbols(bufferDiv) {
const livePrompt = document.getElementById("livePrompt");
if (!livePrompt) {
// Create a live prompt display if not already present
const liveDisplay = document.createElement("div");
liveDisplay.id = "livePrompt";
liveDisplay.classList.add("prompt");
bufferDiv.appendChild(liveDisplay);
}
// Update the live prompt with current key symbols
document.getElementById("livePrompt").textContent = `Typing: ${keySymbols}`;
}
// Parse and process the command
function parseAndProcessCommand(input, bufferDiv) {
if (!input) return; // Do nothing if input is empty
// Add the command to the buffer
buffer.push({ type: "input", content: input });
// Parse and interpret the command
try {
const parsedCommand = parseCommand(input);
const output = executeCommand(parsedCommand);
buffer.push({ type: "output", content: output });
} catch (error) {
buffer.push({ type: "error", content: `Error: ${error.message}` });
}
// Clear live typing area
const livePrompt = document.getElementById("livePrompt");
if (livePrompt) livePrompt.remove();
// Update the buffer display
updateBufferDisplay(bufferDiv);
}
// Parse the command for structure
function parseCommand(command) {
const validCommandPattern = /^(exec|call)\s+[\w\.\(\)]+/g;
const matches = command.match(validCommandPattern);
if (!matches) {
throw new Error("Invalid command format. Use 'exec' or 'call' followed by valid syntax.");
}
// Split the command into parts for detailed parsing
const parsed = matches.map((cmd) => {
const parts = cmd.split(/\s+/);
return { action: parts[0], content: parts.slice(1).join(" ") };
});
return parsed;
}
// Execute parsed commands
function executeCommand(parsedCommand) {
const results = [];
parsedCommand.forEach((cmd, index) => {
if (cmd.action === "exec") {
results.push(`EXEC: Executing ${cmd.content}`);
} else if (cmd.action === "call") {
results.push(`CALL: Calling ${cmd.content}`);
} else {
throw new Error(`Unknown action: ${cmd.action}`);
}
});
return results.join("\n");
}
// Update the buffer display
function updateBufferDisplay(bufferDiv) {
bufferDiv.innerHTML = ""; // Clear the buffer display
buffer.forEach((entry) => {
const div = document.createElement("div");
div.classList.add("prompt");
if (entry.type === "input") {
div.textContent = `Command: ${entry.content}`;
div.style.backgroundColor = "#563a42";
} else if (entry.type === "output") {
div.textContent = `Output:\n${entry.content}`;
div.style.backgroundColor = "#4a3b3f";
} else if (entry.type === "error") {
div.textContent = entry.content;
div.style.backgroundColor = "#702f3a";
}
bufferDiv.appendChild(div);
});
// Scroll to the bottom of the buffer
bufferDiv.scrollTop = bufferDiv.scrollHeight;
}