From 1d9bc87f1beb411b9fa17939f33d0c1a70cdcb2d Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 9 Dec 2025 14:28:30 +0000 Subject: [PATCH] Fixing terminal to only boot if required --- assets/js/commands/core.js | 81 ++++++++-------- assets/js/commands/custom.js | 157 ++++++++++++++++--------------- assets/js/commands/navigation.js | 102 ++++++++++---------- assets/js/commands/utility.js | 112 +++++++++++----------- assets/js/init.js | 21 +++-- assets/js/terminal.js | 11 ++- 6 files changed, 250 insertions(+), 234 deletions(-) diff --git a/assets/js/commands/core.js b/assets/js/commands/core.js index 7efae8b..62d0e69 100644 --- a/assets/js/commands/core.js +++ b/assets/js/commands/core.js @@ -1,41 +1,46 @@ // Core Commands Module -// These are essential commands for the terminal +if (window.terminal) { + // Help command + window.terminal.registerCommand("help", "Display available commands", () => { + window.terminal.print("Available commands:", "success"); + window.terminal.print(""); -// Help command -window.terminal.registerCommand("help", "Display available commands", () => { - window.terminal.print("Available commands:", "success"); - window.terminal.print(""); - - const commands = Object.keys(window.terminal.commands).sort(); - commands.forEach((cmd) => { - const desc = window.terminal.commands[cmd].description || "No description"; - window.terminal.print(` ${cmd.padEnd(15)} - ${desc}`); - }); - window.terminal.print(""); -}); - -// Clear command -window.terminal.registerCommand("clear", "Clear the terminal screen", () => { - window.terminal.clear(); -}); - -// Echo command -window.terminal.registerCommand("echo", "Echo text to the terminal", (args) => { - window.terminal.print(args.join(" ")); -}); - -// History command -window.terminal.registerCommand("history", "Show command history", () => { - if (window.terminal.history.length === 0) { - window.terminal.print("No commands in history"); - return; - } - - window.terminal.print("Command history:", "info"); - window.terminal.history - .slice() - .reverse() - .forEach((cmd, idx) => { - window.terminal.print(` ${idx + 1}. ${cmd}`); + const commands = Object.keys(window.terminal.commands).sort(); + commands.forEach((cmd) => { + const desc = + window.terminal.commands[cmd].description || "No description"; + window.terminal.print(` ${cmd.padEnd(15)} - ${desc}`); }); -}); + window.terminal.print(""); + }); + + // Clear command + window.terminal.registerCommand("clear", "Clear the terminal screen", () => { + window.terminal.clear(); + }); + + // Echo command + window.terminal.registerCommand( + "echo", + "Echo text to the terminal", + (args) => { + window.terminal.print(args.join(" ")); + }, + ); + + // History command + window.terminal.registerCommand("history", "Show command history", () => { + if (window.terminal.history.length === 0) { + window.terminal.print("No commands in history"); + return; + } + + window.terminal.print("Command history:", "info"); + window.terminal.history + .slice() + .reverse() + .forEach((cmd, idx) => { + window.terminal.print(` ${idx + 1}. ${cmd}`); + }); + }); +} diff --git a/assets/js/commands/custom.js b/assets/js/commands/custom.js index 68ea937..8984610 100644 --- a/assets/js/commands/custom.js +++ b/assets/js/commands/custom.js @@ -1,90 +1,90 @@ // Custom Commands Module -// Add your custom commands here! -// This file is a template for creating your own commands +if (window.terminal) { + // Example: Weather command (placeholder) + // window.terminal.registerCommand( + // "weather", + // "Show weather information", + // (args) => { + // const city = args.length > 0 ? args.join(" ") : "your location"; + // window.terminal.printInfo(`Fetching weather for ${city}...`); + // window.terminal.print("☀️ Sunny, 72°F"); + // window.terminal.print(""); + // window.terminal.printWarning( + // "Note: This is a placeholder. Integrate with a real weather API!", + // ); + // }, + // ); -// Example: Weather command (placeholder) -// window.terminal.registerCommand( -// "weather", -// "Show weather information", -// (args) => { -// const city = args.length > 0 ? args.join(" ") : "your location"; -// window.terminal.printInfo(`Fetching weather for ${city}...`); -// window.terminal.print("☀️ Sunny, 72°F"); -// window.terminal.print(""); -// window.terminal.printWarning( -// "Note: This is a placeholder. Integrate with a real weather API!", -// ); -// }, -// ); + // Example: Random quote + // window.terminal.registerCommand("quote", "Display a random quote", () => { + // const quotes = [ + // "The only way to do great work is to love what you do. - Steve Jobs", + // "Innovation distinguishes between a leader and a follower. - Steve Jobs", + // "Stay hungry, stay foolish. - Steve Jobs", + // "First, solve the problem. Then, write the code. - John Johnson", + // ]; -// Example: Random quote -// window.terminal.registerCommand("quote", "Display a random quote", () => { -// const quotes = [ -// "The only way to do great work is to love what you do. - Steve Jobs", -// "Innovation distinguishes between a leader and a follower. - Steve Jobs", -// "Stay hungry, stay foolish. - Steve Jobs", -// "First, solve the problem. Then, write the code. - John Johnson", -// ]; + // const randomQuote = quotes[Math.floor(Math.random() * quotes.length)]; + // window.terminal.printSuccess(randomQuote); + // }); -// const randomQuote = quotes[Math.floor(Math.random() * quotes.length)]; -// window.terminal.printSuccess(randomQuote); -// }); + // Example: List command (demonstrates dynamic content) + // window.terminal.registerCommand( + // "list", + // "List items in a collection", + // (args) => { + // if (args.length === 0) { + // window.terminal.printError("Usage: list "); + // window.terminal.print("Available categories: files, users, tasks"); + // return; + // } -// Example: List command (demonstrates dynamic content) -// window.terminal.registerCommand( -// "list", -// "List items in a collection", -// (args) => { -// if (args.length === 0) { -// window.terminal.printError("Usage: list "); -// window.terminal.print("Available categories: files, users, tasks"); -// return; -// } + // const category = args[0].toLowerCase(); -// const category = args[0].toLowerCase(); + // switch (category) { + // case "files": + // window.terminal.print("Files:", "info"); + // window.terminal.print(" 📄 document.txt"); + // window.terminal.print(" 📄 notes.md"); + // window.terminal.print(" 📁 projects/"); + // break; + // case "users": + // window.terminal.print("Users:", "info"); + // window.terminal.print(" 👤 admin"); + // window.terminal.print(" 👤 guest"); + // break; + // case "tasks": + // window.terminal.print("Tasks:", "info"); + // window.terminal.print(" ✓ Complete terminal setup"); + // window.terminal.print(" ☐ Add more commands"); + // window.terminal.print(" ☐ Customize appearance"); + // break; + // default: + // window.terminal.printError(`Unknown category: ${category}`); + // } + // }, + // ); -// switch (category) { -// case "files": -// window.terminal.print("Files:", "info"); -// window.terminal.print(" 📄 document.txt"); -// window.terminal.print(" 📄 notes.md"); -// window.terminal.print(" 📁 projects/"); -// break; -// case "users": -// window.terminal.print("Users:", "info"); -// window.terminal.print(" 👤 admin"); -// window.terminal.print(" 👤 guest"); -// break; -// case "tasks": -// window.terminal.print("Tasks:", "info"); -// window.terminal.print(" ✓ Complete terminal setup"); -// window.terminal.print(" ☐ Add more commands"); -// window.terminal.print(" ☐ Customize appearance"); -// break; -// default: -// window.terminal.printError(`Unknown category: ${category}`); -// } -// }, -// ); + // Example: Color command (demonstrates HTML output) + window.terminal.registerCommand("colors", "Display available colors", () => { + window.terminal.print("Available terminal colors:", "info"); + window.terminal.print(""); + window.terminal.printHTML("● Standard (green)"); + window.terminal.printHTML('● Error (red)'); + window.terminal.printHTML( + '● Success (bright green)', + ); + window.terminal.printHTML('● Info (blue)'); + window.terminal.printHTML( + '● Warning (orange)', + ); + }); -// Example: Color command (demonstrates HTML output) -window.terminal.registerCommand("colors", "Display available colors", () => { - window.terminal.print("Available terminal colors:", "info"); - window.terminal.print(""); - window.terminal.printHTML("● Standard (green)"); - window.terminal.printHTML('● Error (red)'); - window.terminal.printHTML( - '● Success (bright green)', - ); - window.terminal.printHTML('● Info (blue)'); - window.terminal.printHTML('● Warning (orange)'); -}); + // ADD YOUR OWN COMMANDS BELOW THIS LINE + // ======================================== -// ADD YOUR OWN COMMANDS BELOW THIS LINE -// ======================================== - -// Template for new command: -/* + // Template for new command: + /* window.terminal.registerCommand('commandname', 'Command description', (args) => { // args is an array of arguments // Example: if user types "mycommand hello world" @@ -99,3 +99,4 @@ window.terminal.registerCommand('commandname', 'Command description', (args) => window.terminal.printHTML('HTML content'); }); */ +} diff --git a/assets/js/commands/navigation.js b/assets/js/commands/navigation.js index aa98e5d..b8031d4 100644 --- a/assets/js/commands/navigation.js +++ b/assets/js/commands/navigation.js @@ -1,61 +1,61 @@ // Navigation Commands Module -// Commands for navigating to different pages or URLs +if (window.terminal) { + // Navigate to URL command + window.terminal.registerCommand("goto", "Navigate to a URL", (args) => { + if (args.length === 0) { + window.terminal.printError("Usage: goto "); + window.terminal.print("Examples:"); + window.terminal.print(" goto google.com"); + window.terminal.print(" goto https://github.com"); + return; + } -// Navigate to URL command -window.terminal.registerCommand("goto", "Navigate to a URL", (args) => { - if (args.length === 0) { - window.terminal.printError("Usage: goto "); - window.terminal.print("Examples:"); - window.terminal.print(" goto google.com"); - window.terminal.print(" goto https://github.com"); - return; - } + const url = args[0]; + window.terminal.printInfo(`Navigating to ${url}...`); - const url = args[0]; - window.terminal.printInfo(`Navigating to ${url}...`); + setTimeout(() => { + window.location.href = url.startsWith("http") ? url : `https://${url}`; + }, 500); + }); - setTimeout(() => { - window.location.href = url.startsWith("http") ? url : `https://${url}`; - }, 500); -}); + // Open in new tab command + window.terminal.registerCommand("open", "Open URL in new tab", (args) => { + if (args.length === 0) { + window.terminal.printError("Usage: open "); + window.terminal.print("Examples:"); + window.terminal.print(" open google.com"); + window.terminal.print(" open https://github.com"); + return; + } -// Open in new tab command -window.terminal.registerCommand("open", "Open URL in new tab", (args) => { - if (args.length === 0) { - window.terminal.printError("Usage: open "); - window.terminal.print("Examples:"); - window.terminal.print(" open google.com"); - window.terminal.print(" open https://github.com"); - return; - } + const url = args[0]; + window.terminal.printInfo(`Opening ${url} in new tab...`); - const url = args[0]; - window.terminal.printInfo(`Opening ${url} in new tab...`); + const fullUrl = url.startsWith("http") ? url : `https://${url}`; + window.open(fullUrl, "_blank"); + }); - const fullUrl = url.startsWith("http") ? url : `https://${url}`; - window.open(fullUrl, "_blank"); -}); + // Reload page command + window.terminal.registerCommand("reload", "Reload the current page", () => { + window.terminal.printInfo("Reloading page..."); + setTimeout(() => { + window.location.reload(); + }, 500); + }); -// Reload page command -window.terminal.registerCommand("reload", "Reload the current page", () => { - window.terminal.printInfo("Reloading page..."); - setTimeout(() => { - window.location.reload(); - }, 500); -}); + // PAGE NAVIGATION -// PAGE NAVIGATION + window.terminal.registerCommand( + "latest", + "View the latest post, regardles of section", + () => { + let latestPostLink = document.getElementById("latest-post-link"); + window.location.href = latestPostLink.textContent; + }, + ); -window.terminal.registerCommand( - "latest", - "View the latest post, regardles of section", - () => { - let latestPostLink = document.getElementById("latest-post-link"); - window.location.href = latestPostLink.textContent; - }, -); - -// About command -window.terminal.registerCommand("about", "About this site", () => { - window.location.href = "/about/"; -}); + // About command + window.terminal.registerCommand("about", "About this site", () => { + window.location.href = "/about/"; + }); +} diff --git a/assets/js/commands/utility.js b/assets/js/commands/utility.js index dd00f0a..d164c5a 100644 --- a/assets/js/commands/utility.js +++ b/assets/js/commands/utility.js @@ -1,59 +1,60 @@ // Utility Commands Module // Useful utility commands -// Time command -window.terminal.registerCommand("time", "Display current time", () => { - const now = new Date(); - window.terminal.print(`Current time: ${now.toLocaleString()}`); -}); +if (window.terminal) { + // Time command + window.terminal.registerCommand("time", "Display current time", () => { + const now = new Date(); + window.terminal.print(`Current time: ${now.toLocaleString()}`); + }); -// Calculator command -window.terminal.registerCommand("calc", "Simple calculator", (args) => { - if (args.length === 0) { - window.terminal.printError("Usage: calc "); - window.terminal.print("Example: calc 5 + 3"); - return; - } + // Calculator command + window.terminal.registerCommand("calc", "Simple calculator", (args) => { + if (args.length === 0) { + window.terminal.printError("Usage: calc "); + window.terminal.print("Example: calc 5 + 3"); + return; + } - try { - const expression = args.join(" "); - // Note: eval is dangerous in production! This is just for demo purposes - const result = eval(expression); - window.terminal.printSuccess(`Result: ${result}`); - } catch (error) { - window.terminal.printError("Invalid expression"); - } -}); + try { + const expression = args.join(" "); + // Note: eval is dangerous in production! This is just for demo purposes + const result = eval(expression); + window.terminal.printSuccess(`Result: ${result}`); + } catch (error) { + window.terminal.printError("Invalid expression"); + } + }); -// Demo command -// window.terminal.registerCommand("demo", "Show demo content", () => { -// window.terminal.printSuccess("=== Demo Content ==="); -// window.terminal.print(""); -// window.terminal.print("This is regular text"); -// window.terminal.printInfo("This is info text"); -// window.terminal.printWarning("This is warning text"); -// window.terminal.printError("This is error text"); -// window.terminal.print(""); -// window.terminal.printHTML( -// 'You can also use HTML with links!', -// ); -// }); + // Demo command + // window.terminal.registerCommand("demo", "Show demo content", () => { + // window.terminal.printSuccess("=== Demo Content ==="); + // window.terminal.print(""); + // window.terminal.print("This is regular text"); + // window.terminal.printInfo("This is info text"); + // window.terminal.printWarning("This is warning text"); + // window.terminal.printError("This is error text"); + // window.terminal.print(""); + // window.terminal.printHTML( + // 'You can also use HTML with links!', + // ); + // }); -// ASCII art command -// window.terminal.registerCommand("ascii", "Display ASCII art", () => { -// const art = ` -// _____ _ _ -// |_ _|__ _ __ _ __ ___ (_)_ __ __ _| | -// | |/ _ \\ '__| '_ \` _ \\| | '_ \\ / _\` | | -// | | __/ | | | | | | | | | | | (_| | | -// |_|\\___|_| |_| |_| |_|_|_| |_|\\__,_|_| -// `; -// window.terminal.print(art, "success"); -// }); + // ASCII art command + // window.terminal.registerCommand("ascii", "Display ASCII art", () => { + // const art = ` + // _____ _ _ + // |_ _|__ _ __ _ __ ___ (_)_ __ __ _| | + // | |/ _ \\ '__| '_ \` _ \\| | '_ \\ / _\` | | + // | | __/ | | | | | | | | | | | (_| | | + // |_|\\___|_| |_| |_| |_|_|_| |_|\\__,_|_| + // `; + // window.terminal.print(art, "success"); + // }); -// ASCII art command -window.terminal.registerCommand("nerv", "Display NERV logo", () => { - const art = ` + // ASCII art command + window.terminal.registerCommand("nerv", "Display NERV logo", () => { + const art = ` # ## %*### #****************** #******************* @@ -87,11 +88,12 @@ window.terminal.registerCommand("nerv", "Display NERV logo", () => { #* `; - window.terminal.print(art, "success"); -}); + window.terminal.print(art, "success"); + }); -// Greet command -window.terminal.registerCommand("greet", "Greet the user", (args) => { - const name = args.length > 0 ? args.join(" ") : "User"; - window.terminal.printSuccess(`Hello, ${name}! Welcome to the terminal.`); -}); + // Greet command + window.terminal.registerCommand("greet", "Greet the user", (args) => { + const name = args.length > 0 ? args.join(" ") : "User"; + window.terminal.printSuccess(`Hello, ${name}! Welcome to the terminal.`); + }); +} diff --git a/assets/js/init.js b/assets/js/init.js index 856ce56..97c9a34 100644 --- a/assets/js/init.js +++ b/assets/js/init.js @@ -1,16 +1,21 @@ // Terminal Initialization Script -// This script creates the terminal instance immediately -// so command modules can register commands +// This script creates the terminal instance only if terminal element exists -// Create global terminal instance immediately window.terminal = new TerminalShell(); -// Boot the terminal when DOM is ready -if (document.readyState === "loading") { - document.addEventListener("DOMContentLoaded", () => { +// Function to initialize terminal +function initTerminal() { + // Check if terminal element exists + if (document.getElementById("terminal")) { + // Boot the terminal terminal.boot(); - }); + } +} + +// Wait for DOM to be ready +if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", initTerminal); } else { // DOM already loaded - terminal.boot(); + initTerminal(); } diff --git a/assets/js/terminal.js b/assets/js/terminal.js index 6024570..8fc0b2f 100644 --- a/assets/js/terminal.js +++ b/assets/js/terminal.js @@ -1,14 +1,17 @@ // Terminal Shell System class TerminalShell { constructor() { - this.output = document.getElementById("output"); - this.input = document.getElementById("input"); - this.inputContainer = document.getElementById("input-container"); this.history = []; this.historyIndex = -1; this.commands = {}; - this.setupEventListeners(); + if (document.getElementById("terminal")) { + this.output = document.getElementById("output"); + this.input = document.getElementById("input"); + this.inputContainer = document.getElementById("input-container"); + + this.setupEventListeners(); + } } // Boot sequence