File size: 25,333 Bytes
06163ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
/**
* CatOS Terminal Module
* Handles terminal functionality and cat-themed commands
*/
class Terminal {
constructor() {
this.terminals = new Map();
this.commandHistory = [];
this.historyIndex = -1;
this.currentDirectory = '~';
// File system structure for simulation
this.fileSystem = {
'~': {
type: 'dir',
contents: {
'projects': { type: 'dir', contents: {
'sneaky-cat-proxy.md': { type: 'file', content: 'Project info...' },
'cat-photo-gallery.md': { type: 'file', content: 'Gallery info...' },
'robo-cat-manager.md': { type: 'file', content: 'Bot manager info...' }
}},
'documents': { type: 'dir', contents: {
'resume.pdf': { type: 'file', content: 'Professional resume' },
'cat-wisdom.txt': { type: 'file', content: 'Ancient cat knowledge' }
}},
'pictures': { type: 'dir', contents: {
'profile.jpg': { type: 'file', content: 'Profile picture' },
'cats': { type: 'dir', contents: {
'fluffy.jpg': { type: 'file', content: 'A fluffy cat' },
'whiskers.png': { type: 'file', content: 'Cat with whiskers' }
}}
}},
'README.md': { type: 'file', content: 'Welcome to CatOS!' }
}
}
};
}
initialize(terminalId, core) {
this.core = core;
const terminal = {
id: terminalId,
element: document.querySelector(`[data-terminal-id="${terminalId}"]`),
input: document.getElementById(`terminal-input-${terminalId}`),
content: document.getElementById(`terminal-content-${terminalId}`),
prompt: document.getElementById(`prompt-${terminalId}`)
};
if (!terminal.input || !terminal.content) return;
this.terminals.set(terminalId, terminal);
this.setupTerminalEvents(terminalId);
terminal.input.focus();
}
setupTerminalEvents(terminalId) {
const terminal = this.terminals.get(terminalId);
if (!terminal) return;
terminal.input.addEventListener('keydown', (e) => {
switch(e.key) {
case 'Enter':
this.handleCommand(terminalId);
break;
case 'ArrowUp':
e.preventDefault();
this.navigateHistory(terminalId, -1);
break;
case 'ArrowDown':
e.preventDefault();
this.navigateHistory(terminalId, 1);
break;
case 'Tab':
e.preventDefault();
this.handleTabCompletion(terminalId);
break;
}
});
// Focus input when clicking on terminal content
terminal.content.addEventListener('click', () => {
terminal.input.focus();
});
}
handleCommand(terminalId) {
const terminal = this.terminals.get(terminalId);
if (!terminal) return;
const command = terminal.input.value.trim();
if (!command) return;
// Add command to history
this.commandHistory.push(command);
this.historyIndex = this.commandHistory.length;
// Display command
this.addTerminalLine(terminal.content, `${this.getPromptText()} ${command}`, 'terminal-prompt');
// Parse and execute command
const [cmd, ...args] = command.split(' ');
this.executeCommand(terminal.content, cmd.toLowerCase(), args);
// Clear input and scroll to bottom
terminal.input.value = '';
terminal.content.scrollTop = terminal.content.scrollHeight;
}
executeCommand(content, cmd, args) {
switch(cmd) {
case 'help':
this.terminalHelp(content);
break;
case 'clear':
this.clearTerminal(content);
break;
case 'ls':
this.terminalLs(content, args[0]);
break;
case 'cd':
this.terminalCd(content, args[0]);
break;
case 'pwd':
this.terminalPwd(content);
break;
case 'cat':
this.terminalCat(content, args[0]);
break;
case 'whoami':
this.terminalWhoami(content, this.getVisitorInfo());
break;
case 'date':
this.terminalDate(content);
break;
case 'echo':
this.terminalEcho(content, args.join(' '));
break;
case 'meow':
this.terminalMeow(content, args.join(' '));
break;
case 'purr':
this.terminalPurr(content);
break;
case 'scratch':
this.terminalScratch(content);
break;
case 'nap':
this.terminalNap(content);
break;
case 'fortune':
this.terminalFortune(content);
break;
case 'ps':
this.terminalPs(content, args);
break;
case 'uptime':
this.terminalUptime(content);
break;
case 'curl':
this.terminalCurl(content, args[0]);
break;
case 'git':
this.terminalGit(content, args);
break;
case 'npm':
this.terminalNpm(content, args);
break;
case 'history':
this.terminalHistory(content);
break;
case 'exit':
case 'quit':
this.terminalExit(content);
break;
default:
this.terminalCommandNotFound(content, cmd);
}
}
getPromptText() {
return `cat@catos:${this.currentDirectory}$`;
}
addTerminalLine(content, text, className = 'terminal-text') {
const line = document.createElement('div');
line.className = 'terminal-line';
line.innerHTML = `<span class="${className}">${text}</span>`;
content.appendChild(line);
}
addTerminalOutput(content, text) {
const line = document.createElement('div');
line.className = 'terminal-line';
line.innerHTML = `<span class="terminal-output">${text}</span>`;
content.appendChild(line);
}
clearTerminal(content) {
content.innerHTML = '';
this.addTerminalLine(content, 'Terminal cleared! Ready for more cat commands! ๐ฑ', 'terminal-success');
}
terminalHelp(content) {
const helpText = `
<span class="terminal-category">๐ Navigation:</span>
ls [path] - List directory contents
cd [directory] - Change directory
pwd - Show current directory
cat [file] - Display file contents
<span class="terminal-category">๐ System:</span>
whoami - Display visitor information
ps aux - Show running processes
uptime - System uptime
date - Current date and time
history - Command history
clear - Clear terminal
<span class="terminal-category">๐ Network:</span>
curl [url] - Fetch web content
git [command] - Git operations
npm [command] - NPM operations
<span class="terminal-category">๐ฑ Cat Commands:</span>
meow [message] - Cat responses
purr - Show happiness level
scratch - Stress relief
nap - Take a quick break
fortune - Cat wisdom
<span class="terminal-category">๐ก Tips:</span>
Use โ/โ arrows for command history
Try: cat projects/sneaky-cat-proxy.md
Pro tip: Type 'm' anywhere for surprise meows! ๐ธ
`;
this.addTerminalOutput(content, helpText);
}
terminalWhoami(content, visitorInfo) {
const info = `
<span class="terminal-category">๐ต๏ธ Visitor Detective Results:</span>
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Browser: ${visitorInfo.browser}
โ Platform: ${visitorInfo.platform}
โ Screen: ${visitorInfo.screenWidth}x${visitorInfo.screenHeight}
โ Language: ${visitorInfo.language}
โ Visit Time: ${new Date(visitorInfo.visitTime).toLocaleString()}
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
<span class="terminal-success">*purrs* Nice to meet you, fellow human! ๐ฑ</span>
`;
this.addTerminalOutput(content, info);
}
getVisitorInfo() {
return {
browser: navigator.userAgent.split(' ').pop().split('/')[0] || 'Unknown',
platform: navigator.platform || 'Unknown',
screenWidth: screen.width,
screenHeight: screen.height,
language: navigator.language || 'Unknown',
visitTime: Date.now()
};
}
terminalLs(content, path) {
const targetPath = path || this.currentDirectory;
const contents = this.getDirectoryContents(targetPath);
if (!contents) {
this.addTerminalOutput(content, `ls: cannot access '${targetPath}': No such file or directory ๐ฟ`);
return;
}
let output = `<span class="terminal-category">๐ Contents of ${targetPath}:</span>\n`;
contents.forEach(item => {
const type = item.type === 'dir' ? '<span class="terminal-directory">DIR</span>' : '<span class="terminal-file">FILE</span>';
output += `${item.icon} ${type} ${item.name}\n`;
});
this.addTerminalOutput(content, output);
}
getDirectoryContents(path) {
// Navigate to the specified path in our file system
let current = this.fileSystem['~'];
if (path !== '~' && path !== '.') {
const parts = path.replace(/^~\//, '').split('/').filter(p => p);
for (const part of parts) {
if (current.contents && current.contents[part]) {
current = current.contents[part];
} else {
return null;
}
}
}
if (current.type !== 'dir') return null;
// Convert to display format
const contents = [];
if (current.contents) {
for (const [name, item] of Object.entries(current.contents)) {
contents.push({
name,
type: item.type,
icon: item.type === 'dir' ? '๐' : '๐'
});
}
}
return contents;
}
terminalCd(content, path) {
if (!path || path === '~') {
this.currentDirectory = '~';
this.addTerminalOutput(content, `<span class="terminal-success">Changed to home directory ๐ </span>`);
return;
}
// Simple directory navigation simulation
if (path === '..') {
if (this.currentDirectory !== '~') {
const parts = this.currentDirectory.split('/');
parts.pop();
this.currentDirectory = parts.join('/') || '~';
this.addTerminalOutput(content, `<span class="terminal-success">Moved up one directory ๐</span>`);
} else {
this.addTerminalOutput(content, `Already at root directory! ๐ `);
}
return;
}
// Check if directory exists
const contents = this.getDirectoryContents(this.currentDirectory);
const targetDir = contents?.find(item => item.name === path && item.type === 'dir');
if (targetDir) {
this.currentDirectory = this.currentDirectory === '~' ? `~/${path}` : `${this.currentDirectory}/${path}`;
this.addTerminalOutput(content, `<span class="terminal-success">Changed to ${this.currentDirectory} ๐</span>`);
} else {
this.addTerminalOutput(content, `cd: ${path}: No such directory ๐ฟ`);
}
// Update all terminal prompts
this.terminals.forEach(terminal => {
if (terminal.prompt) {
terminal.prompt.textContent = this.getPromptText();
}
});
}
terminalPwd(content) {
this.addTerminalOutput(content, `<span class="terminal-success">${this.currentDirectory}</span>`);
}
terminalCat(content, filename) {
if (!filename) {
this.addTerminalOutput(content, 'cat: missing file operand ๐\nUsage: cat <filename>');
return;
}
// Special handling for project files
if (filename.includes('sneaky-cat-proxy')) {
const project = this.core.appManager.projects['sneaky-cat-proxy'];
this.addTerminalOutput(content, this.formatProjectInfo(project));
} else if (filename.includes('cat-photo-gallery')) {
const project = this.core.appManager.projects['cat-photo-gallery'];
this.addTerminalOutput(content, this.formatProjectInfo(project));
} else if (filename.includes('robo-cat-manager')) {
const project = this.core.appManager.projects['robo-cat-manager'];
this.addTerminalOutput(content, this.formatProjectInfo(project));
} else {
this.addTerminalOutput(content, `cat: ${filename}: No such file or directory ๐ฟ`);
}
}
formatProjectInfo(project) {
return `
<span class="terminal-category">${project.icon} ${project.title}</span>
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
<span class="terminal-success">Description:</span>
${project.description}
<span class="terminal-success">Technologies:</span>
${project.technologies.map(tech => `โข ${tech}`).join('\n')}
<span class="terminal-success">Features:</span>
${project.features.map(feature => `๐พ ${feature}`).join('\n')}
<span class="terminal-success">Links:</span>
โข GitHub: ${project.github}
โข Demo: ${project.demo}
<span class="terminal-category">*purrs approvingly* ๐ธ</span>
`;
}
terminalDate(content) {
const now = new Date();
const dateString = now.toLocaleString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short'
});
this.addTerminalOutput(content, `<span class="terminal-success">${dateString}</span>`);
}
terminalEcho(content, message) {
this.addTerminalOutput(content, message || '');
}
// Cat-specific commands
terminalMeow(content, message) {
const catResponses = [
"๐ฑ *meows back* Purrfect!",
"๐ธ *purrs* That's interesting!",
"๐บ *head bumps* I agree!",
"๐ *surprised meow* Really?!",
"๐ป *affectionate meow* Aww!",
"๐น *laughing meow* Haha!",
"๐พ *grumpy meow* Hmph!"
];
if (message) {
const response = catResponses[Math.floor(Math.random() * catResponses.length)];
this.addTerminalOutput(content, `You said: "${message}"\n${response}`);
} else {
this.addTerminalOutput(content, catResponses[Math.floor(Math.random() * catResponses.length)]);
}
}
terminalPurr(content) {
const happiness = Math.floor(Math.random() * 100) + 1;
const purrLevel = happiness > 80 ? "MAXIMUM PURR" : happiness > 60 ? "Happy purrs" : happiness > 40 ? "Content purrs" : "Quiet purrs";
this.addTerminalOutput(content, `
<span class="terminal-category">๐ธ Current Happiness Level: ${happiness}%</span>
Status: ${purrLevel}
Mood: ${happiness > 70 ? "๐ป Ecstatic" : happiness > 50 ? "๐ Happy" : "๐ Neutral"}
*${purrLevel.toLowerCase()}* ๐พ
`);
}
terminalScratch(content) {
this.addTerminalOutput(content, `
<span class="terminal-category">๐พ *scratch scratch*</span>
Ahh, that's better! Stress levels reduced.
Your virtual scratching post has been used.
<span class="terminal-success">+10 Comfort Points! ๐</span>
`);
}
terminalNap(content) {
this.addTerminalOutput(content, `
<span class="terminal-category">๐ด Taking a quick cat nap...</span>
*curls up in a sunny spot*
Zzz... ๐ค
<span class="terminal-success">Refreshed and ready! Energy restored! ๐ฑ</span>
`);
}
terminalFortune(content) {
const fortunes = [
'๐พ A warm laptop keyboard is worth two in the bush.',
'๐ฑ The best code is written at 3 AM with a cat on your keyboard.',
'๐ธ In the kingdom of bugs, the debugger is king.',
'๐ฏ A feature is only as good as its documentation... said no cat ever.',
'๐ป The cloud is just someone else\'s computer, probably with better WiFi.',
'๐พ Remember: There are no mistakes, only happy little bugs.',
'๐ฑ The cloud is just other people\'s litter boxes.'
];
const fortune = fortunes[Math.floor(Math.random() * fortunes.length)];
this.addTerminalOutput(content, `<span class="terminal-category">๐ฎ Cat Fortune:</span>\n${fortune}`);
}
terminalPs(content, args) {
const processes = `
<span class="terminal-category">๐ CatOS Process Status:</span>
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
cat 1337 15.2 8.5 245760 32156 pts/0 Sl+ 09:30 0:42 /usr/bin/coffee-maker
cat 2020 12.1 4.2 128000 16384 pts/1 S 09:32 0:15 /usr/bin/yarn-ball-tracker
cat 3000 8.7 2.1 64000 8192 pts/2 R 09:35 0:08 /usr/bin/mouse-detector
cat 4040 5.3 1.8 32000 4096 pts/3 S 09:38 0:03 /usr/bin/nap-scheduler
rafael 9999 98.5 75.2 2048000 234567 pts/5 R+ 09:35 5:67 /usr/bin/coding-furiously
<span class="terminal-success">Current mood: Caffeinated and ready to code! โ</span>
`;
this.addTerminalOutput(content, processes);
}
terminalUptime(content) {
const uptime = `
<span class="terminal-category">โฑ๏ธ System Uptime:</span>
Developer: 5+ years of coding experience
Coffee Machine: 3 hours since last refill โ
Cat OS: 42 days, 13 hours, 37 minutes (no crashes!)
Motivation Level: 87% (pretty good for a Monday!)
Purr Engine: Running at optimal frequency ๐ธ
<span class="terminal-success">Load average: 1.33, 7.77, 42.00 (that's normal for a cat) ๐</span>
`;
this.addTerminalOutput(content, uptime);
}
terminalCurl(content, url) {
if (!url) {
this.addTerminalOutput(content, 'curl: no URL specified ๐\nUsage: curl <url>');
return;
}
// Simulate curl with cat-themed responses
this.addTerminalOutput(content, `<span class="terminal-category">๐ Fetching ${url}...</span>`);
setTimeout(() => {
if (url.includes('github.com')) {
this.addTerminalOutput(content, `
<span class="terminal-success">โ
Connected successfully!</span>
Repository found: Lots of cat-themed code! ๐ธ
Stars: โญโญโญโญโญ (purr-fect rating)
Issues: 3 (all related to insufficient treats)
`);
} else {
this.addTerminalOutput(content, `
<span class="terminal-success">โ
Response received!</span>
Status: 200 OK ๐บ
Content-Type: text/purr-fect
Cat-Approval: 100%
`);
}
}, 1000);
}
terminalGit(content, args) {
const subcommand = args[0] || 'status';
switch(subcommand) {
case 'status':
this.addTerminalOutput(content, `
<span class="terminal-category">๐ Git Status:</span>
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
modified: src/cat-behavior.js
modified: config/treats.json
Untracked files:
hairball.log
<span class="terminal-success">*purrs* Everything looks good! ๐พ</span>
`);
break;
case 'log':
this.addTerminalOutput(content, `
<span class="terminal-category">๐ Git Log:</span>
commit a1b2c3d (HEAD -> main, origin/main)
Author: Cat Developer <cat@catos.dev>
Date: Today
Fix: Improved treat dispensing algorithm ๐ช
commit e4f5g6h
Author: Cat Developer <cat@catos.dev>
Date: Yesterday
Feature: Added nap scheduling system ๐ด
`);
break;
default:
this.addTerminalOutput(content, `git ${subcommand}: Not implemented yet, but it sounds purr-fessional! ๐ฑโ๐ป`);
}
}
terminalNpm(content, args) {
const subcommand = args[0] || 'help';
switch(subcommand) {
case 'install':
this.addTerminalOutput(content, `
<span class="terminal-category">๐ฆ Installing cat-packages...</span>
+ catnip@4.2.0
+ yarn-ball@1.3.7
+ treat-dispenser@2.1.0
+ purr-engine@8.0.1
<span class="terminal-success">โ
All packages installed successfully! ๐ธ</span>
Note: Remember to pet your dependencies regularly.
`);
break;
case 'start':
this.addTerminalOutput(content, `
<span class="terminal-category">๐ Starting development server...</span>
> CatOS@9.0.0 start
> cat-dev-server --purr
Local: http://localhost:3000 ๐พ
Network: http://192.168.1.100:3000
`);
break;
case 'run':
this.addTerminalOutput(content, `Available scripts: start, build, test, purr, nap`);
break;
default:
this.addTerminalOutput(content, `npm ${subcommand}: Command not found. Try 'npm run purr' instead! ๐น`);
}
}
terminalHistory(content) {
if (this.commandHistory.length === 0) {
this.addTerminalOutput(content, 'No commands in history yet! ๐');
return;
}
let output = '<span class="terminal-category">๐ Command History:</span>\n';
this.commandHistory.forEach((cmd, index) => {
output += `${index + 1}. ${cmd}\n`;
});
this.addTerminalOutput(content, output);
}
terminalExit(content) {
this.addTerminalOutput(content, `
<span class="terminal-category">๐ Goodbye!</span>
Thanks for using CatOS Terminal!
*purrs farewell* ๐ธ
<span class="terminal-success">Tip: Close the window to fully exit, or keep coding! ๐พ</span>
`);
}
terminalCommandNotFound(content, cmd) {
const suggestions = [
"Maybe you meant 'meow'? ๐ฑ",
"Try 'help' for available commands! ๐",
"That's not a valid cat command! ๐น",
"*confused cat noises* ๐",
"Command not found in the litter box! ๐ฆ"
];
const suggestion = suggestions[Math.floor(Math.random() * suggestions.length)];
this.addTerminalOutput(content, `${cmd}: command not found\n${suggestion}`);
}
// History navigation
navigateHistory(terminalId, direction) {
const terminal = this.terminals.get(terminalId);
if (!terminal || this.commandHistory.length === 0) return;
this.historyIndex += direction;
if (this.historyIndex < 0) {
this.historyIndex = 0;
} else if (this.historyIndex >= this.commandHistory.length) {
this.historyIndex = this.commandHistory.length;
terminal.input.value = '';
return;
}
terminal.input.value = this.commandHistory[this.historyIndex];
}
// Tab completion (basic)
handleTabCompletion(terminalId) {
const terminal = this.terminals.get(terminalId);
if (!terminal) return;
const input = terminal.input.value;
const commands = ['help', 'clear', 'ls', 'cd', 'pwd', 'cat', 'whoami', 'date', 'echo',
'meow', 'purr', 'scratch', 'nap', 'fortune', 'ps', 'uptime', 'curl',
'git', 'npm', 'history', 'exit'];
const matches = commands.filter(cmd => cmd.startsWith(input));
if (matches.length === 1) {
terminal.input.value = matches[0];
} else if (matches.length > 1) {
this.addTerminalOutput(terminal.content, `Possible completions: ${matches.join(', ')}`);
}
}
}
// Export for use in main system
window.CatOSTerminal = new Terminal(); |