|
| 1 | +const terminalElement = document.getElementById('terminal'); |
| 2 | +const outputElement = document.getElementById('output'); |
| 3 | +const inputElement = document.getElementById('command-input'); |
| 4 | + |
| 5 | +const commands = { |
| 6 | + help: "Available commands:\n - help: List all commands\n - projects: List all projects\n - project [name]: Show details of a specific project\n - about: Display personal information\n - contact: Display contact information\n - clear: Clear the terminal\n - ls: List available sections/pages", |
| 7 | + projects: "Projects:\n - project1\n - project2\n - project3", |
| 8 | + about: "About:\n - Name: John Doe\n - Skills: JavaScript, Python, Linux, Software Architecture", |
| 9 | + contact: "Contact:\n - Email: [email protected]\n - LinkedIn: linkedin.com/in/johndoe", |
| 10 | + ls: "Sections:\n - About\n - Projects\n - Contact", |
| 11 | +}; |
| 12 | + |
| 13 | +const projectDetails = { |
| 14 | + project1: "Project1: A web application built with React.", |
| 15 | + project2: "Project2: A machine learning model for predictive analysis.", |
| 16 | + project3: "Project3: A terminal-style portfolio website.", |
| 17 | +}; |
| 18 | + |
| 19 | +let commandHistory = []; |
| 20 | +let historyIndex = -1; |
| 21 | + |
| 22 | +const easterEggArt = ` |
| 23 | + __ |
| 24 | + / \\ |
| 25 | + | | |
| 26 | + \\__/ |
| 27 | + / \\ |
| 28 | + | | |
| 29 | + | | |
| 30 | + \\____/ |
| 31 | + (____) |
| 32 | +`; |
| 33 | + |
| 34 | +commands.easteregg = "Easter Egg discovered! Here's a little surprise:\n" + easterEggArt; |
| 35 | + |
| 36 | +function handleCommand(input) { |
| 37 | + const [command, arg] = input.split(' '); |
| 38 | + |
| 39 | + switch (command) { |
| 40 | + case 'help': |
| 41 | + displayOutput(commands.help); |
| 42 | + break; |
| 43 | + case 'projects': |
| 44 | + displayOutput(commands.projects); |
| 45 | + break; |
| 46 | + case 'project': |
| 47 | + displayOutput(projectDetails[arg] || "Error: Project not found.", !!projectDetails[arg]); |
| 48 | + break; |
| 49 | + case 'about': |
| 50 | + displayOutput(commands.about); |
| 51 | + break; |
| 52 | + case 'contact': |
| 53 | + displayOutput(commands.contact); |
| 54 | + break; |
| 55 | + case 'clear': |
| 56 | + clearTerminal(); |
| 57 | + break; |
| 58 | + case 'ls': |
| 59 | + displayOutput(commands.ls); |
| 60 | + break; |
| 61 | + case 'easteregg': // New Easter Egg command |
| 62 | + displayOutput(commands.easteregg); |
| 63 | + break; |
| 64 | + default: |
| 65 | + displayOutput("Error: Command not recognized. Type 'help' for a list of commands.", false); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +function displayOutput(text, success = true) { |
| 71 | + const timestamp = new Date().toLocaleTimeString(); |
| 72 | + const line = `[${timestamp}] ${text}`; |
| 73 | + outputElement.innerHTML += `<div class="${success ? '' : 'error'}">${line}</div>`; |
| 74 | + scrollToBottom(); |
| 75 | +} |
| 76 | + |
| 77 | +function clearTerminal() { |
| 78 | + outputElement.innerHTML = ''; |
| 79 | +} |
| 80 | + |
| 81 | +function scrollToBottom() { |
| 82 | + outputElement.scrollTop = outputElement.scrollHeight; |
| 83 | +} |
| 84 | + |
| 85 | +function handleKeyDown(event) { |
| 86 | + if (event.key === 'Enter') { |
| 87 | + const input = inputElement.value.trim(); |
| 88 | + if (input) { |
| 89 | + displayOutput(`$ ${input}`); |
| 90 | + commandHistory.push(input); |
| 91 | + historyIndex = commandHistory.length; |
| 92 | + handleCommand(input); |
| 93 | + } |
| 94 | + inputElement.value = ''; |
| 95 | + } else if (event.key === 'ArrowUp') { |
| 96 | + if (historyIndex > 0) { |
| 97 | + historyIndex -= 1; |
| 98 | + inputElement.value = commandHistory[historyIndex]; |
| 99 | + } |
| 100 | + } else if (event.key === 'ArrowDown') { |
| 101 | + if (historyIndex < commandHistory.length - 1) { |
| 102 | + historyIndex += 1; |
| 103 | + inputElement.value = commandHistory[historyIndex]; |
| 104 | + } else { |
| 105 | + inputElement.value = ''; |
| 106 | + historyIndex = commandHistory.length; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Focus the input field whenever the terminal is clicked |
| 112 | +terminalElement.addEventListener('click', () => { |
| 113 | + inputElement.focus(); |
| 114 | +}); |
| 115 | + |
| 116 | +inputElement.addEventListener('keydown', handleKeyDown); |
0 commit comments