the terminal is no longer just styling but an actual tiny terminal emulator lol
This commit is contained in:
1 parent
7451fe33b7
commit
4f18ef5844
8 files changed
+584
-116
No files matched your search
@@ -0,0 +1,278 @@
|
||||
import {Component, ElementRef, Input, OnChanges, OnInit, SimpleChanges, TemplateRef, ViewChild, ViewEncapsulation} from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { Router } from '@angular/router';
|
||||
import {TranslatePipe} from '@ngx-translate/core';
|
||||
import {NgTemplateOutlet} from '@angular/common';
|
||||
|
||||
type TerminalLine =
|
||||
| { type: 'command'; text: string }
|
||||
| { type: 'output'; text: string }
|
||||
| { type: 'error'; text: string }
|
||||
| { type: 'template'; template: TemplateRef<any> }
|
||||
| { type: 'entries'; entries: TerminalEntry[] };
|
||||
|
||||
type TerminalEntryType = 'file' | 'dir' | 'html';
|
||||
interface TerminalEntry {
|
||||
name: string;
|
||||
type: TerminalEntryType;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-terminal-emulator',
|
||||
encapsulation: ViewEncapsulation.None,
|
||||
standalone: true,
|
||||
imports: [FormsModule, TranslatePipe, NgTemplateOutlet],
|
||||
templateUrl: './terminal-emulator.component.html',
|
||||
styleUrl: './terminal-emulator.component.css'
|
||||
})
|
||||
|
||||
export class TerminalEmulatorComponent implements OnInit, OnChanges {
|
||||
@ViewChild('terminalContainer') terminalContainer!: ElementRef<HTMLDivElement>;
|
||||
@ViewChild('terminalOutput') terminalOutput!: ElementRef<HTMLDivElement>;
|
||||
@ViewChild('terminalInput') terminalInput!: ElementRef<HTMLInputElement>;
|
||||
|
||||
@ViewChild('aboutTemplate') aboutTemplate!: TemplateRef<any>;
|
||||
@ViewChild('catTemplate') catTemplate!: TemplateRef<any>;
|
||||
@ViewChild('toolsTemplate') toolsTemplate!: TemplateRef<any>;
|
||||
|
||||
@Input() bootOnVisible = false;
|
||||
|
||||
history: TerminalLine[] = [];
|
||||
command = '';
|
||||
booting = true;
|
||||
private hasBooted = false;
|
||||
|
||||
private rootEntries: TerminalEntry[] = [
|
||||
{ name: 'about.txt', type: 'file' },
|
||||
{ name: 'tools', type: 'dir' },
|
||||
{ name: 'portfolio.html', type: 'html' },
|
||||
{ name: 'blog.html', type: 'html' },
|
||||
{ name: 'socials.html', type: 'html' }
|
||||
];
|
||||
|
||||
constructor(private router: Router) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.booting = false;
|
||||
}
|
||||
|
||||
ngOnChanges(changes: SimpleChanges) {
|
||||
if (changes['bootOnVisible']?.currentValue && !this.hasBooted) {
|
||||
this.hasBooted = true;
|
||||
void this.playIntro();
|
||||
}
|
||||
}
|
||||
|
||||
private async playIntro() {
|
||||
this.booting = true;
|
||||
|
||||
await this.typeCommand('cat about.txt');
|
||||
this.history.push({
|
||||
type: 'template',
|
||||
template: this.aboutTemplate
|
||||
});
|
||||
this.scrollToBottom();
|
||||
|
||||
await this.sleep(400);
|
||||
|
||||
await this.typeCommand('ls tools/');
|
||||
this.history.push({
|
||||
type: 'template',
|
||||
template: this.toolsTemplate
|
||||
});
|
||||
this.scrollToBottom();
|
||||
|
||||
await this.sleep(300);
|
||||
|
||||
this.booting = false;
|
||||
|
||||
setTimeout(() => {
|
||||
this.focusInput();
|
||||
}, 0);
|
||||
}
|
||||
|
||||
private async typeCommand(text: string) {
|
||||
let current = '';
|
||||
|
||||
this.history.push({ type: 'command', text: '$ ' });
|
||||
const commandIndex = this.history.length - 1;
|
||||
|
||||
for (const char of text) {
|
||||
current += char;
|
||||
this.history[commandIndex] = {
|
||||
type: 'command',
|
||||
text: `$ ${current}`
|
||||
};
|
||||
this.scrollToBottom();
|
||||
await this.sleep(50);
|
||||
}
|
||||
}
|
||||
|
||||
runCommand() {
|
||||
const cmd = this.command.trim();
|
||||
if (!cmd || this.booting) return;
|
||||
|
||||
this.history.push({ type: 'command', text: `$ ${cmd}` });
|
||||
|
||||
const parts = cmd.toLowerCase().split(/\s+/);
|
||||
const base = parts[0];
|
||||
const arg = parts.slice(1).join(' ');
|
||||
|
||||
switch (base) {
|
||||
case 'help':
|
||||
this.history.push({
|
||||
type: 'output',
|
||||
text: 'Available commands: help, clear, whoami, ls, cat, portfolio, blog, socials'
|
||||
});
|
||||
break;
|
||||
|
||||
case 'whoami':
|
||||
this.history.push({
|
||||
type: 'output',
|
||||
text: 'itzMiney'
|
||||
});
|
||||
break;
|
||||
|
||||
case 'ls':
|
||||
this.handleLs(arg);
|
||||
break;
|
||||
|
||||
case 'cat':
|
||||
this.handleCat(arg);
|
||||
break;
|
||||
|
||||
case 'portfolio':
|
||||
this.history.push({
|
||||
type: 'output',
|
||||
text: 'Opening portfolio...'
|
||||
});
|
||||
void this.router.navigate(['/portfolio']);
|
||||
break;
|
||||
|
||||
case 'blog':
|
||||
this.history.push({
|
||||
type: 'output',
|
||||
text: 'Opening blog...'
|
||||
});
|
||||
void this.router.navigate(['/blog']);
|
||||
break;
|
||||
|
||||
case 'socials':
|
||||
this.history.push({
|
||||
type: 'output',
|
||||
text: 'Opening socials...'
|
||||
});
|
||||
void this.router.navigate(['/socials']);
|
||||
break;
|
||||
|
||||
case 'clear':
|
||||
this.history = [];
|
||||
this.command = '';
|
||||
this.scrollToBottom();
|
||||
return;
|
||||
|
||||
default:
|
||||
this.history.push({
|
||||
type: 'error',
|
||||
text: `command not found: ${cmd}`
|
||||
});
|
||||
}
|
||||
|
||||
this.command = '';
|
||||
this.scrollToBottom();
|
||||
}
|
||||
|
||||
private handleLs(arg: string) {
|
||||
const normalized = arg.replace(/\/+$/, '');
|
||||
|
||||
if (!normalized) {
|
||||
this.history.push({
|
||||
type: 'entries',
|
||||
entries: this.rootEntries
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (normalized === 'tools') {
|
||||
this.history.push({
|
||||
type: 'template',
|
||||
template: this.toolsTemplate
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.history.push({
|
||||
type: 'error',
|
||||
text: `ls: cannot access '${arg}': No such file or directory`
|
||||
});
|
||||
}
|
||||
|
||||
private handleCat(arg: string) {
|
||||
const normalized = arg.trim();
|
||||
|
||||
if (!normalized) {
|
||||
this.history.push({
|
||||
type: 'template',
|
||||
template: this.catTemplate
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
switch (normalized) {
|
||||
case 'about.txt':
|
||||
this.history.push({
|
||||
type: 'template',
|
||||
template: this.aboutTemplate
|
||||
});
|
||||
break;
|
||||
|
||||
case 'portfolio.html':
|
||||
this.history.push({
|
||||
type: 'output',
|
||||
text: 'Navigate to /portfolio'
|
||||
});
|
||||
break;
|
||||
|
||||
case 'blog.html':
|
||||
this.history.push({
|
||||
type: 'output',
|
||||
text: 'Navigate to /blog'
|
||||
});
|
||||
break;
|
||||
|
||||
case 'socials.html':
|
||||
this.history.push({
|
||||
type: 'output',
|
||||
text: 'Navigate to /socials'
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
this.history.push({
|
||||
type: 'error',
|
||||
text: `cat: ${arg}: No such file or directory`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
focusInput() {
|
||||
if (!this.booting && this.terminalInput) {
|
||||
this.terminalInput.nativeElement.focus();
|
||||
}
|
||||
}
|
||||
|
||||
private scrollToBottom() {
|
||||
setTimeout(() => {
|
||||
if (!this.terminalOutput) return;
|
||||
const el = this.terminalContainer.nativeElement;
|
||||
el.scrollTo({
|
||||
top: el.scrollHeight,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user