mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add type and color to response
This commit is contained in:
1 parent
5cad3ecc12
commit
a151d2d667
6 files changed
+143
-17
No files matched your search
+41
-8
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
const fs = require('fs').promises
|
||||
const { exit } = require('process')
|
||||
@@ -5,6 +6,24 @@ const readline = require('readline')
|
||||
|
||||
const RETURN_SYMBOL = '\u21B5'
|
||||
|
||||
// ANSI color map
|
||||
const ANSI_COLORS = {
|
||||
black: 30,
|
||||
red: 31,
|
||||
green: 32,
|
||||
yellow: 33,
|
||||
blue: 34,
|
||||
magenta: 35,
|
||||
cyan: 36,
|
||||
white: 37,
|
||||
}
|
||||
|
||||
function colorize(content, color) {
|
||||
const colorCode = ANSI_COLORS[color]
|
||||
if (!colorCode) return content
|
||||
return `\x1b[${colorCode}m${content}\x1b[0m`
|
||||
}
|
||||
|
||||
process.stdin.setRawMode(true)
|
||||
|
||||
const rl = readline.createInterface({
|
||||
@@ -19,13 +38,14 @@ function waitForEnter() {
|
||||
})
|
||||
}
|
||||
|
||||
function printResponse(response, delay) {
|
||||
function printLine(content, delay, color) {
|
||||
const printContent = `${colorize(content, color)}${RETURN_SYMBOL}`
|
||||
return new Promise((resolve) => {
|
||||
let i = 0
|
||||
const timer = setInterval(() => {
|
||||
process.stdout.write(response[i])
|
||||
process.stdout.write(printContent[i])
|
||||
i += 1
|
||||
if (i >= response.length) {
|
||||
if (i >= printContent.length) {
|
||||
clearInterval(timer)
|
||||
waitForEnter().then(resolve)
|
||||
}
|
||||
@@ -33,7 +53,7 @@ function printResponse(response, delay) {
|
||||
|
||||
waitForEnter().then(() => {
|
||||
clearInterval(timer)
|
||||
process.stdout.write(response.slice(i))
|
||||
process.stdout.write(printContent.slice(i))
|
||||
process.stdout.write('\n')
|
||||
resolve()
|
||||
})
|
||||
@@ -42,8 +62,18 @@ function printResponse(response, delay) {
|
||||
|
||||
async function printResponses(responses, delay = 100) {
|
||||
for (const response of responses) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await printResponse(response + RETURN_SYMBOL, delay)
|
||||
const { type, content, speaker, color } = response
|
||||
if (type === 'narrative') {
|
||||
for (const line of content) {
|
||||
await printLine(line, delay, color || 'white')
|
||||
}
|
||||
}
|
||||
if (type === 'dialogue') {
|
||||
console.log(`${speaker}:`)
|
||||
for (const line of content) {
|
||||
await printLine(`\t${line}`, delay, color || 'green')
|
||||
}
|
||||
}
|
||||
}
|
||||
rl.close()
|
||||
}
|
||||
@@ -88,11 +118,14 @@ async function main() {
|
||||
})
|
||||
const data = await res.json()
|
||||
if (data.responses) {
|
||||
await printResponses(data.responses, 100)
|
||||
await printResponses(data.responses, 60)
|
||||
}
|
||||
if (data.end) {
|
||||
console.log(colorize('Quest completed!', 'blue'))
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
main().then(exit)
|
||||
main().then(exit).catch(console.error)
|
||||
Reference in new issue
Block a user