Skip to content

Commit

Permalink
updated commands and added documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bennett Schwartz committed Nov 13, 2023
1 parent e9f6502 commit d3dc1e9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 54 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Terminal-Simulator
Just a fun terminal simulator coded in HTML5, JS, and CSS. It combines different softwares, and it's a work in progress!

## Commands

`hello`: This command will return with "hello, {username}!"
`changename {USERNAME}`: This command will changed your cached name.
`clearcache`: This command will clear any local storage. See the (local cache document)[./cache.md].
8 changes: 8 additions & 0 deletions cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Cache
Find all the information about what is being stored on your browser's cache.

## Clearing the cache
To clear the cache, simply enter `clearcache`.

## Commands
* `changename`: Changes the stored "username".
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body>
<div class="terminal">
<div class="output">$ Welcome to the Linux Terminal Simulator</div>
<div class="output"></div>
<div class="prompt">$</div>
<div class="input">
<input type="text" id="commandInput" onkeydown="handleInput(event)">
Expand Down
125 changes: 72 additions & 53 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,72 @@
// Retrieve the name from localStorage. If not found, set a default value.
let name = localStorage.getItem('username') || "user";
terminal.loadAddon(fitAddon);
terminal.open(document.getElementsByClassName('terminal'));
fitAddon.fit();

terminal.prompt = () => {
terminal.write('\r\n');
terminal.write(`\x1b[32m${name}\x1b[0m$ `);
};

function changeName(newName) {
// Update the name variable
name = newName;
// Store the updated name in localStorage
localStorage.setItem('username', newName);
}

function handleInput(event) {
if (event.key === "Enter") {
const commandInput = document.getElementById("commandInput");
const command = commandInput.value;
const outputDiv = document.querySelector('.output');

// Simulate command execution
const output = executeCommand(command);

// Display command and output in the terminal
outputDiv.innerHTML += `<div class="prompt">$ ${command}</div>`;
outputDiv.innerHTML += `<div class="output">${output}</div>`;

// Clear the input field
commandInput.value = "";
}
}

function executeCommand(command) {
// Simulate command execution (add more commands through GitHub Pull Requests.)
if (command.toLowerCase() === "hello") {
return `Hello, ${name}!`;
} else if (command.toLowerCase().startsWith("changename ")) {
const newName = command.substring("changename ".length);
changeName(newName);
return `Name changed to ${name}`;
} else if (command.toLowerCase() === "clearcache") {
localStorage.clear();
window.location.reload(true);
}
else {
return `Command not found: ${command}`;
}
}

// Get the current date and time on page load
document.addEventListener('DOMContentLoaded', () => {
const outputDiv = document.querySelector('.output');
const currentDate = new Date();
const formattedDate = currentDate.toLocaleString();

// Display the initial date and time in the terminal
outputDiv.innerHTML += `<div class="output">$ Terminal @${formattedDate}</div>`;
});
// Retrieve the name from localStorage. If not found, set a default value.
let name = localStorage.getItem('username') || "user";
terminal.loadAddon(fitAddon);
terminal.open(document.getElementsByClassName('terminal'));
fitAddon.fit();

terminal.prompt = () => {
terminal.write('\r\n');
terminal.write(`\x1b[32m${name}\x1b[0m$ `);
};

function changeName(newName) {
// Update the name variable
name = newName;
// Store the updated name in localStorage
localStorage.setItem('username', newName);
}

function handleInput(event) {
if (event.key === "Enter") {
const commandInput = document.getElementById("commandInput");
const command = commandInput.value;
const outputDiv = document.querySelector('.output');

// Simulate command execution
const output = executeCommand(command);

// Display command and output in the terminal
outputDiv.innerHTML += `<div class="prompt">$ ${command}</div>`;
outputDiv.innerHTML += `<div class="output">${output}</div>`;

// Clear the input field
commandInput.value = "";
}
}

function executeCommand(command) {
// Simulate command execution (add more commands through GitHub Pull Requests.)
if (command.toLowerCase() === "hello") {
return `Hello, ${name}!`;
} else if (command.toLowerCase().startsWith("changename ")) {
const newName = command.substring("changename ".length);
changeName(newName);
return `Name changed to ${name}`;
} else if (command.toLowerCase() === "clearcache") {
localStorage.clear();
window.location.reload(true);
}
else if (command.toLowerCase().startsWith("changename")) {
const newName = command.substring("changename ".length).trim();

if (newName === "") {
return "'Name' not defined.";
}

changeName(newName);
return `Name changed to ${name}`;
}

else {
return `Command not found: ${command}`;
}
}

0 comments on commit d3dc1e9

Please sign in to comment.