Files
Nathanwoodburn.github.io/templates/terminal.html
2025-10-27 12:56:38 +11:00

382 lines
14 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Terminal | Nathan.Woodburn/</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: #000;
font-family: 'Courier New', monospace;
color: #0f0;
overflow: hidden;
}
#terminal {
width: 100vw;
height: 100vh;
padding: 20px;
overflow-y: auto;
}
.line { margin-bottom: 5px; }
.prompt { color: #0f0; white-space: pre; }
.prompt-line { color: #0f0; }
.output { color: #fff; white-space: pre-wrap; }
.error { color: #f00; }
#input-line {
display: block;
}
.input-row {
display: flex;
}
#command-input {
background: transparent;
border: none;
color: #0f0;
font-family: 'Courier New', monospace;
font-size: 16px;
outline: none;
flex: 1;
caret-color: #0f0;
}
#nano-editor {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: #000;
z-index: 1000;
flex-direction: column;
}
#nano-editor.active {
display: flex;
}
#nano-header {
padding: 10px 20px;
border-bottom: 1px solid #0f0;
color: #0f0;
}
#nano-textarea {
flex: 1;
background: #000;
color: #fff;
border: none;
padding: 20px;
font-family: 'Courier New', monospace;
font-size: 16px;
resize: none;
outline: none;
}
#nano-footer {
padding: 10px 20px;
border-top: 1px solid #0f0;
color: #0f0;
font-size: 14px;
}
#nano-save-prompt {
display: none;
padding: 10px 20px;
background: #111;
border-top: 1px solid #0f0;
color: #fff;
}
#nano-save-prompt.active {
display: block;
}
</style>
</head>
<body>
<div id="terminal">
<div class="line output">Nathan.Woodburn/</div>
<div class="line output">Type 'help' for available commands</div>
<div class="line output"></div>
<div id="input-line">
<div class="prompt-line">┌──({{user}}@NathanWoodburn)-[~]</div>
<div class="input-row">
<span class="prompt-line">└─$&nbsp;</span>
<input type="text" id="command-input" autofocus autocomplete="off" spellcheck="false">
</div>
</div>
</div>
<!-- Nano Editor -->
<div id="nano-editor">
<div id="nano-header">nano - <span id="nano-filename"></span></div>
<textarea id="nano-textarea"></textarea>
<div id="nano-save-prompt">Save modified buffer? (Y/N)</div>
<div id="nano-footer">^X Exit ^O Save (Ctrl+X to exit, Ctrl+O to save)</div>
</div>
<script>
const terminal = document.getElementById('terminal');
const input = document.getElementById('command-input');
const inputLine = document.getElementById('input-line');
const nanoEditor = document.getElementById('nano-editor');
const nanoTextarea = document.getElementById('nano-textarea');
const nanoFilename = document.getElementById('nano-filename');
let commandHistory = [];
let historyIndex = -1;
let currentPwd = '~';
let nanoCurrentFile = '';
let nanoOriginalContent = '';
let nanoSavePromptActive = false;
// Function to update the prompt with current pwd
async function updatePrompt() {
try {
const response = await fetch('/terminal/pwd');
const data = await response.json();
if (data.output) {
currentPwd = data.output;
inputLine.querySelector('.prompt-line:first-child').textContent = `┌──({{user}}@NathanWoodburn)-[${currentPwd}]`;
}
} catch (err) {
console.error('Failed to fetch pwd:', err);
}
}
// Function to open nano editor
async function openNano(filename) {
nanoCurrentFile = filename;
nanoFilename.textContent = filename;
nanoSavePromptActive = false;
document.getElementById('nano-save-prompt').classList.remove('active');
// Try to fetch existing file content
try {
const parts = ['cat'].concat(filename.split(' '));
const response = await fetch('/terminal/execute/cat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ args: filename })
});
const data = await response.json();
if (data.output && !data.output.startsWith('cat:')) {
nanoTextarea.value = data.output;
nanoOriginalContent = data.output;
} else {
nanoTextarea.value = '';
nanoOriginalContent = '';
}
} catch (err) {
nanoTextarea.value = '';
nanoOriginalContent = '';
}
nanoEditor.classList.add('active');
nanoTextarea.focus();
}
// Function to close nano editor
async function closeNano(shouldSave = false) {
if (shouldSave) {
await saveNano();
}
nanoEditor.classList.remove('active');
nanoSavePromptActive = false;
document.getElementById('nano-save-prompt').classList.remove('active');
terminal.appendChild(inputLine);
inputLine.style.display = 'block';
input.disabled = false;
input.focus();
terminal.scrollTop = terminal.scrollHeight;
}
// Function to save nano content
async function saveNano() {
const content = nanoTextarea.value;
try {
const response = await fetch('/terminal/execute/nano', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ args: `${nanoCurrentFile} CONTENT ${content}` })
});
const data = await response.json();
nanoOriginalContent = content; // Update original content after save
// If output contains error, display it
if (data.output && data.output.startsWith('nano:')) {
const errorLine = document.createElement('div');
errorLine.className = 'line error';
errorLine.textContent = data.output;
terminal.appendChild(errorLine);
}
// Don't display output - nano saves silently
} catch (err) {
const errorLine = document.createElement('div');
errorLine.className = 'line error';
errorLine.textContent = 'Error saving file: ' + err.message;
terminal.appendChild(errorLine);
}
}
// Check if content has changed
function hasUnsavedChanges() {
return nanoTextarea.value !== nanoOriginalContent;
}
// Nano editor keyboard shortcuts
nanoTextarea.addEventListener('keydown', async (e) => {
if (e.ctrlKey && e.key === 'x') {
e.preventDefault();
if (nanoSavePromptActive) {
return; // Ignore if prompt is already active
}
if (hasUnsavedChanges()) {
// Show save prompt
nanoSavePromptActive = true;
document.getElementById('nano-save-prompt').classList.add('active');
document.getElementById('nano-footer').textContent = 'Press Y to save, N to discard, Ctrl+C to cancel';
} else {
await closeNano(false);
}
} else if (e.ctrlKey && e.key === 'o') {
e.preventDefault();
await saveNano();
} else if (e.ctrlKey && e.key === 'c' && nanoSavePromptActive) {
e.preventDefault();
// Cancel save prompt
nanoSavePromptActive = false;
document.getElementById('nano-save-prompt').classList.remove('active');
document.getElementById('nano-footer').textContent = '^X Exit ^O Save (Ctrl+X to exit, Ctrl+O to save)';
} else if (nanoSavePromptActive && (e.key === 'y' || e.key === 'Y')) {
e.preventDefault();
await closeNano(true);
} else if (nanoSavePromptActive && (e.key === 'n' || e.key === 'N')) {
e.preventDefault();
await closeNano(false);
}
});
// Update prompt on load
updatePrompt();
input.addEventListener('keydown', async (e) => {
if (e.key === 'Enter') {
const command = input.value.trim();
// Disable input and hide prompt during execution
input.disabled = true;
inputLine.style.display = 'none';
// Display command
const cmdLine = document.createElement('div');
cmdLine.className = 'line';
cmdLine.innerHTML = `<span class="prompt">┌──({{user}}@NathanWoodburn)-[${currentPwd}]\n└─$ </span>${command}`;
terminal.appendChild(cmdLine);
// Add to history
if (command) {
commandHistory.push(command);
historyIndex = commandHistory.length;
}
input.value = '';
// Handle clear command locally
if (command === 'clear') {
terminal.innerHTML = '';
terminal.appendChild(inputLine);
inputLine.style.display = 'block';
input.disabled = false;
input.focus();
return;
}
if (command === 'exit') {
// Redirect to /
window.location.href = '/';
return;
}
// Handle nano command specially
const parts = command.split(' ');
const baseCommand = parts[0];
if (baseCommand === 'nano') {
if (parts.length < 2) {
const errorLine = document.createElement('div');
errorLine.className = 'line output';
errorLine.textContent = 'Usage: nano [filename]';
terminal.appendChild(errorLine);
terminal.appendChild(inputLine);
inputLine.style.display = 'block';
input.disabled = false;
input.focus();
return;
}
await openNano(parts.slice(1).join(' '));
return;
}
// Execute command
if (command) {
try {
// Split command by space
const args = parts.slice(1).join(' ');
// POST request to /terminal/execute/command
const response = await fetch(`/terminal/execute/${encodeURIComponent(baseCommand)}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ args: args })
});
const data = await response.json();
if (data.output) {
const outputLine = document.createElement('div');
outputLine.className = 'line output';
outputLine.textContent = data.output;
terminal.appendChild(outputLine);
}
// Update prompt after cd command
if (baseCommand === 'cd' || baseCommand === 'reset') {
await updatePrompt();
}
} catch (err) {
const errorLine = document.createElement('div');
errorLine.className = 'line error';
errorLine.textContent = 'Error: ' + err.message;
terminal.appendChild(errorLine);
}
}
// Show prompt and re-enable input after command completes
terminal.appendChild(inputLine);
inputLine.style.display = 'block';
input.disabled = false;
input.focus();
terminal.scrollTop = terminal.scrollHeight;
} else if (e.key === 'ArrowUp') {
e.preventDefault();
if (historyIndex > 0) {
historyIndex--;
input.value = commandHistory[historyIndex];
}
} else if (e.key === 'ArrowDown') {
e.preventDefault();
if (historyIndex < commandHistory.length - 1) {
historyIndex++;
input.value = commandHistory[historyIndex];
} else {
historyIndex = commandHistory.length;
input.value = '';
}
}
});
// Keep input focused
document.addEventListener('click', () => input.focus());
input.focus();
</script>
</body>
</html>