Added loop to console (closes #1)

This commit is contained in:
Zakary Timson 2022-05-03 16:41:55 -04:00
parent f4ff3c3ad7
commit ce0e7dcc98
4 changed files with 48 additions and 20 deletions

View File

@ -1,34 +1,31 @@
import {Component, Input, ViewChild} from '@angular/core'; import {Component, Input, ViewChild} from '@angular/core';
import { take } from 'rxjs'; import {sleep} from '../../misc/utils';
import {TypewriterComponent} from '../typewriter/typewriter.component'; import {TypewriterComponent} from '../typewriter/typewriter.component';
export type ConsoleConfig = {
input: string,
output: () => string
}
@Component({ @Component({
selector: 'console', selector: 'console',
templateUrl: './console.component.html', templateUrl: './console.component.html',
styleUrls: ['./console.component.scss'] styleUrls: ['./console.component.scss']
}) })
export class ConsoleComponent { export class ConsoleComponent {
done = () => {};
input = ''; input = '';
output: string[] = []; output: string[] = [];
prompt = '>' prompt = '>'
done = () => {};
@Input() height: string = 'auto'; @Input() height: string = 'auto';
@ViewChild(TypewriterComponent) typewriter!: TypewriterComponent; @ViewChild(TypewriterComponent) typewriter!: TypewriterComponent;
exec(input: string, output: () => string) { clear() { this.output = []; }
this.done = () => {
this.output.push(`${this.prompt} ${input}`); exec(input: string, output: () => any, pause = 1000) {
console.log(output()); this.done = async () => {
this.output.push(output()); await sleep(pause);
this.input = ''; this.input = '';
this.output.push(`${this.prompt} ${input}`);
const out = output();
if(typeof out == 'string') this.output.push(out);
}; };
this.input = input; this.input = input;
} }

18
src/app/misc/utils.ts Normal file
View File

@ -0,0 +1,18 @@
/**
* Use with await to pause the script for a specified amount of time (in miliseconds).
*
* **Example:**
* ```
* async () => {
* ...
* await sleep(1000) // Wait 1 second
* ...
* }
* ```
*
* @param {number} ms - Time to pause in miliseconds
* @returns {Promise<unknown>} - Promise you should await
*/
export function sleep(ms: number) {
return new Promise(res => setTimeout(res, ms));
}

View File

@ -1,15 +1,27 @@
import {Component, ViewChild} from '@angular/core'; import {AfterViewInit, Component, ViewChild} from '@angular/core';
import {ConsoleComponent, ConsoleConfig} from '../../components/console/console.component'; import {ConsoleComponent} from '../../components/console/console.component';
import {QuoteService} from '../../services/quote.service'; import {QuoteService} from '../../services/quote.service';
import {sleep} from '../../misc/utils';
@Component({ @Component({
selector: 'home', selector: 'home',
templateUrl: './home.component.html' templateUrl: './home.component.html'
}) })
export class HomeComponent { export class HomeComponent implements AfterViewInit {
@ViewChild(ConsoleComponent) console!: ConsoleComponent; @ViewChild(ConsoleComponent) console!: ConsoleComponent;
constructor(private quotes: QuoteService) { constructor(private quotes: QuoteService) { }
setTimeout(() => this.console.exec('bash ./random-thought.sh', () => quotes.random()), 3000);
ngAfterViewInit() { this.animateConsole(); }
animateConsole() {
setTimeout(async () => {
this.console.exec('bash ./random-thought.sh', () => this.quotes.random());
await sleep(10000);
this.console.exec('clear', async () => {
this.console.clear();
this.animateConsole();
});
}, 1000);
} }
} }

View File

@ -6,8 +6,9 @@
"types": [] "types": []
}, },
"files": [ "files": [
"src/main.ts", "src/main.ts",
"src/polyfills.ts" "src/polyfills.ts",
"src/app/misc/utils.ts"
], ],
"include": [ "include": [
"src/**/*.d.ts" "src/**/*.d.ts"