From ba9aea4ad5a02b6cd985b64d9a4f6c7f187fee36 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 4 Mar 2022 05:17:15 -0500 Subject: [PATCH] sm shiiii --- package.json | 4 ++- src/Character.ts | 24 ++++++-------- src/Engine.ts | 82 +++++++++++++++++++++++++++++++++++++++++++++++- src/index.ts | 3 +- yarn.lock | 22 ++++++++++++- 5 files changed, 116 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 0675660..55c7683 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,14 @@ { "name": "renjs", "version": "1.0.0", - "main": "index.js", + "main": "out/index.js", "license": "MIT", "type": "module", "dependencies": { "@types/node": "^17.0.21", + "@types/prompts": "^2.0.14", "chalk": "^5.0.0", + "prompts": "^2.4.2", "sisteransi": "^1.0.5", "typescript": "^4.6.2" }, diff --git a/src/Character.ts b/src/Character.ts index 08f2f8d..d617b57 100644 --- a/src/Character.ts +++ b/src/Character.ts @@ -14,7 +14,14 @@ const sleep = (n: number) => new Promise(res => setTimeout(res, n)); async function glitchText(string: string) { const rchar = () => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM".charAt(Math.floor(Math.random() * 52)); const rcolor = () => chalk.ansi256(Math.floor(Math.random() * 240) + 16); + let inEscape = false; for(const char of string) { + if (char === '\x1b') inEscape = true; + else if (char.match(/[a-zA-Z]/)) inEscape = false; + if(inEscape) { + process.stdout.write(char); + continue; + } const r = 5 + Math.random() * 5; for(let i = 0; i < r; i ++) { process.stdout.write(rcolor()(rchar())); @@ -29,7 +36,7 @@ async function glitchText(string: string) { async function slowText(string: string) { for(const char of string) { process.stdout.write(char); - await sleep(50); + await sleep(10); } } @@ -54,17 +61,4 @@ export class Character { await slowText(chalk.italic(str)); console.log(); } -} - -const claire = new Character({ - name: "Claire", - color: 173 -}); - -console.clear(); -process.stdout.write(ansi.cursor.down(process.stdout.rows * 2) + ansi.cursor.hide); - -(async () => { - await claire.say("Nice weather we're having!"); - await claire.think("Nice weather we're having!"); -})(); \ No newline at end of file +} \ No newline at end of file diff --git a/src/Engine.ts b/src/Engine.ts index 4e94035..cf5bf12 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -1,3 +1,83 @@ +import ansi from 'sisteransi'; +import prompts from 'prompts'; + export class Engine { - + + constructor() { + process.on('beforeExit', () => { + this.prepScreenTop(); + }) + } + + async prompt(question: string, choices: { [key: number]: string } | { [key: string]: string }) { + const _choices = []; + for(const key in choices) { + // * idk how to TS this properly man + _choices.push({ title: (choices as any)[key] as string, value: key}) + } + const value = (await prompts({ + type: 'select', + name: 'value', + message: question, + choices: _choices, + initial: 1, + hint: ' ' + })).value; + process.stdout.write(ansi.cursor.hide); + return value; + } + + prepScreen() { + console.clear(); + process.stdout.write( + ansi.cursor.down(process.stdout.rows * 2) + + ansi.cursor.hide + ); + } + + play(fn: Function) { + setTimeout(() => { + fn(); + }, 0); + } + + emptyLine() { + console.log(); + } + + prepScreenTop() { + console.clear(); + process.stdout.write( + ansi.cursor.down(process.stdout.rows * 2) + + ansi.cursor.hide + + ansi.cursor.up(Math.floor(process.stdout.rows)) + ); + } + + async sceneChange(place: string, time?: string) { + console.log(''); + console.log(padCenterWithMargin(place)); + console.log(''); + } + + async timePass(s = 9) { + let bar = [' ', ' ']; + for(let i = 0; i < s; i ++) { + const str = bar.join('*'); + process.stdout.write('\n' + padCenter(str) + '\n' + ansi.cursor.up(2)); + await new Promise(res => setTimeout(res, 300)); + bar.push(' '); + } + process.stdout.write(ansi.cursor.down(2)); + } +} + +function padCenter(str: string, chr = ' ', w = process.stdout.columns) { + const left = Math.floor((w - str.length) / 2); + const right = w - str.length - left; + return chr.repeat(left) + str + chr.repeat(right); +} + +function padCenterWithMargin(str: string) { + return padCenter(padCenter(`\u2528 ${str} \u2520`, '\u2500', Math.max(process.stdout.columns - 8, 0))) } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 5d6b328..3aa3fbb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ -export { Engine } from './Engine'; \ No newline at end of file +export { Engine } from './Engine.js'; +export { Character } from './Character.js' \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index fa79a49..c19db63 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,16 +2,36 @@ # yarn lockfile v1 -"@types/node@^17.0.21": +"@types/node@*", "@types/node@^17.0.21": version "17.0.21" resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.21.tgz#864b987c0c68d07b4345845c3e63b75edd143644" integrity sha512-DBZCJbhII3r90XbQxI8Y9IjjiiOGlZ0Hr32omXIZvwwZ7p4DMMXGrKXVyPfuoBOri9XNtL0UK69jYIBIsRX3QQ== +"@types/prompts@^2.0.14": + version "2.0.14" + resolved "https://registry.yarnpkg.com/@types/prompts/-/prompts-2.0.14.tgz#10cb8899844bb0771cabe57c1becaaaca9a3b521" + integrity sha512-HZBd99fKxRWpYCErtm2/yxUZv6/PBI9J7N4TNFffl5JbrYMHBwF25DjQGTW3b3jmXq+9P6/8fCIb2ee57BFfYA== + dependencies: + "@types/node" "*" + chalk@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.0.0.tgz#bd96c6bb8e02b96e08c0c3ee2a9d90e050c7b832" integrity sha512-/duVOqst+luxCQRKEo4bNxinsOQtMP80ZYm7mMqzuh5PociNL0PvmHFvREJ9ueYL2TxlHjBcmLCdmocx9Vg+IQ== +kleur@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e" + integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w== + +prompts@^2.4.2: + version "2.4.2" + resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069" + integrity sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q== + dependencies: + kleur "^3.0.3" + sisteransi "^1.0.5" + sisteransi@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"