Fixing terminal to only boot if required

This commit is contained in:
Dan 2025-12-09 14:28:30 +00:00
parent 6315c36efc
commit 1d9bc87f1b
6 changed files with 250 additions and 234 deletions

View file

@ -1,6 +1,5 @@
// Core Commands Module // Core Commands Module
// These are essential commands for the terminal if (window.terminal) {
// Help command // Help command
window.terminal.registerCommand("help", "Display available commands", () => { window.terminal.registerCommand("help", "Display available commands", () => {
window.terminal.print("Available commands:", "success"); window.terminal.print("Available commands:", "success");
@ -8,7 +7,8 @@ window.terminal.registerCommand("help", "Display available commands", () => {
const commands = Object.keys(window.terminal.commands).sort(); const commands = Object.keys(window.terminal.commands).sort();
commands.forEach((cmd) => { commands.forEach((cmd) => {
const desc = window.terminal.commands[cmd].description || "No description"; const desc =
window.terminal.commands[cmd].description || "No description";
window.terminal.print(` ${cmd.padEnd(15)} - ${desc}`); window.terminal.print(` ${cmd.padEnd(15)} - ${desc}`);
}); });
window.terminal.print(""); window.terminal.print("");
@ -20,9 +20,13 @@ window.terminal.registerCommand("clear", "Clear the terminal screen", () => {
}); });
// Echo command // Echo command
window.terminal.registerCommand("echo", "Echo text to the terminal", (args) => { window.terminal.registerCommand(
"echo",
"Echo text to the terminal",
(args) => {
window.terminal.print(args.join(" ")); window.terminal.print(args.join(" "));
}); },
);
// History command // History command
window.terminal.registerCommand("history", "Show command history", () => { window.terminal.registerCommand("history", "Show command history", () => {
@ -39,3 +43,4 @@ window.terminal.registerCommand("history", "Show command history", () => {
window.terminal.print(` ${idx + 1}. ${cmd}`); window.terminal.print(` ${idx + 1}. ${cmd}`);
}); });
}); });
}

View file

@ -1,7 +1,5 @@
// Custom Commands Module // Custom Commands Module
// Add your custom commands here! if (window.terminal) {
// This file is a template for creating your own commands
// Example: Weather command (placeholder) // Example: Weather command (placeholder)
// window.terminal.registerCommand( // window.terminal.registerCommand(
// "weather", // "weather",
@ -77,7 +75,9 @@ window.terminal.registerCommand("colors", "Display available colors", () => {
'<span class="success">● Success (bright green)</span>', '<span class="success">● Success (bright green)</span>',
); );
window.terminal.printHTML('<span class="info">● Info (blue)</span>'); window.terminal.printHTML('<span class="info">● Info (blue)</span>');
window.terminal.printHTML('<span class="warning">● Warning (orange)</span>'); window.terminal.printHTML(
'<span class="warning">● Warning (orange)</span>',
);
}); });
// ADD YOUR OWN COMMANDS BELOW THIS LINE // ADD YOUR OWN COMMANDS BELOW THIS LINE
@ -99,3 +99,4 @@ window.terminal.registerCommand('commandname', 'Command description', (args) =>
window.terminal.printHTML('<strong>HTML content</strong>'); window.terminal.printHTML('<strong>HTML content</strong>');
}); });
*/ */
}

View file

@ -1,6 +1,5 @@
// Navigation Commands Module // Navigation Commands Module
// Commands for navigating to different pages or URLs if (window.terminal) {
// Navigate to URL command // Navigate to URL command
window.terminal.registerCommand("goto", "Navigate to a URL", (args) => { window.terminal.registerCommand("goto", "Navigate to a URL", (args) => {
if (args.length === 0) { if (args.length === 0) {
@ -59,3 +58,4 @@ window.terminal.registerCommand(
window.terminal.registerCommand("about", "About this site", () => { window.terminal.registerCommand("about", "About this site", () => {
window.location.href = "/about/"; window.location.href = "/about/";
}); });
}

View file

@ -1,6 +1,7 @@
// Utility Commands Module // Utility Commands Module
// Useful utility commands // Useful utility commands
if (window.terminal) {
// Time command // Time command
window.terminal.registerCommand("time", "Display current time", () => { window.terminal.registerCommand("time", "Display current time", () => {
const now = new Date(); const now = new Date();
@ -95,3 +96,4 @@ window.terminal.registerCommand("greet", "Greet the user", (args) => {
const name = args.length > 0 ? args.join(" ") : "User"; const name = args.length > 0 ? args.join(" ") : "User";
window.terminal.printSuccess(`Hello, ${name}! Welcome to the terminal.`); window.terminal.printSuccess(`Hello, ${name}! Welcome to the terminal.`);
}); });
}

View file

@ -1,16 +1,21 @@
// Terminal Initialization Script // Terminal Initialization Script
// This script creates the terminal instance immediately // This script creates the terminal instance only if terminal element exists
// so command modules can register commands
// Create global terminal instance immediately
window.terminal = new TerminalShell(); window.terminal = new TerminalShell();
// Boot the terminal when DOM is ready // Function to initialize terminal
if (document.readyState === "loading") { function initTerminal() {
document.addEventListener("DOMContentLoaded", () => { // Check if terminal element exists
terminal.boot(); if (document.getElementById("terminal")) {
}); // Boot the terminal
} else {
// DOM already loaded
terminal.boot(); terminal.boot();
} }
}
// Wait for DOM to be ready
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initTerminal);
} else {
// DOM already loaded
initTerminal();
}

View file

@ -1,15 +1,18 @@
// Terminal Shell System // Terminal Shell System
class TerminalShell { class TerminalShell {
constructor() { constructor() {
this.output = document.getElementById("output");
this.input = document.getElementById("input");
this.inputContainer = document.getElementById("input-container");
this.history = []; this.history = [];
this.historyIndex = -1; this.historyIndex = -1;
this.commands = {}; this.commands = {};
if (document.getElementById("terminal")) {
this.output = document.getElementById("output");
this.input = document.getElementById("input");
this.inputContainer = document.getElementById("input-container");
this.setupEventListeners(); this.setupEventListeners();
} }
}
// Boot sequence // Boot sequence
async boot() { async boot() {