From f5693e924302d7cd7514c769723acb055a2b44d2 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 28 Oct 2022 05:10:57 -0500 Subject: [PATCH] feat: better unicode support --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/messages/index.ts | 12 ++++++------ src/prompt/elements/select.js | 4 ++-- src/prompt/elements/text.js | 4 ++-- src/utils/index.ts | 5 ++++- 6 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc5d23e..41ca309 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @astrojs/cli-kit +## 0.1.1 + +### Patch Changes + +- Update `isWin` util to respect a `FORCE_UNICODE` process.env variable + ## 0.1.0 ### Minor Changes diff --git a/package.json b/package.json index ef078dc..a7e1c86 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/cli-kit", "type": "module", - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "types": "./dist/index.d.ts", "packageManager": "pnpm@7.11.0", diff --git a/src/messages/index.ts b/src/messages/index.ts index 9a7f8e9..436d088 100644 --- a/src/messages/index.ts +++ b/src/messages/index.ts @@ -1,7 +1,7 @@ import readline from 'node:readline'; import color from 'chalk'; import logUpdate from 'log-update'; -import { random, randomBetween, sleep, isWin } from '../utils/index.js' +import { random, randomBetween, sleep, useAscii } from '../utils/index.js' import { action } from '../prompt/util/action.js'; import { strip } from '../prompt/util/clear.js'; @@ -37,10 +37,10 @@ export const say = async (messages: string | string[] = [], { clear = false, hat }) const _messages = Array.isArray(messages) ? messages : [messages]; - const eyes = isWin ? ['•', '•', 'o', 'o', '•', 'O', '^', '•'] : ['●', '●', '●', '●', '●', '○', '○', '•']; - const mouths = isWin ? ['•', 'O', '*', 'o', 'o', '•', '-'] : ['•', '○', '■', '▪', '▫', '▬', '▭', '-', '○']; - const walls = isWin ? ['—', '|'] : ['─', '│']; - const corners = isWin ? ['+', '+', '+', '+'] : ['╭', '╮', '╰', '╯']; + const eyes = useAscii ? ['•', '•', 'o', 'o', '•', 'O', '^', '•'] : ['●', '●', '●', '●', '●', '○', '○', '•']; + const mouths = useAscii ? ['•', 'O', '*', 'o', 'o', '•', '-'] : ['•', '○', '■', '▪', '▫', '▬', '▭', '-', '○']; + const walls = useAscii ? ['—', '|'] : ['─', '│']; + const corners = useAscii ? ['+', '+', '+', '+'] : ['╭', '╮', '╰', '╯']; const face = (msg: string, { mouth = mouths[0], eye = eyes[0] } = {}) => { const [h, v] = walls; @@ -67,7 +67,7 @@ export const say = async (messages: string | string[] = [], { clear = false, hat j++; } if (!cancelled) await sleep(100); - const text = '\n' + face(_message.join(' '), { mouth: isWin ? 'u' : '◡', eye: isWin ? '^' : '◠' }); + const text = '\n' + face(_message.join(' '), { mouth: useAscii ? 'u' : '◡', eye: useAscii ? '^' : '◠' }); logUpdate(text); if (!cancelled) await sleep(randomBetween(800, 900)); i++; diff --git a/src/prompt/elements/select.js b/src/prompt/elements/select.js index 95d82f2..8da6c60 100644 --- a/src/prompt/elements/select.js +++ b/src/prompt/elements/select.js @@ -1,7 +1,7 @@ import Prompt from './prompt.js'; import { erase, cursor } from 'sisteransi'; import color from 'chalk'; -import { isWin } from '../../utils/index.js'; +import { useAscii } from '../../utils/index.js'; import clear, { strip } from '../util/clear.js'; export default class SelectPrompt extends Prompt { @@ -141,7 +141,7 @@ export default class SelectPrompt extends Prompt { if (this.done) { this.outputText.push(`${prefix} `, color.dim(`${this.choices[this.cursor]?.label}`)); } else { - this.outputText.push(this.choices.map((choice, i) => i === this.cursor ? `${prefix} ${color.green(isWin ? '>' : '●')} ${this.highlight(choice.label)} ${choice.hint ? color.dim(choice.hint) : ''}` : color.dim(`${prefix} ${isWin ? '—' : '○'} ${choice.label} `)).join('\n')) + this.outputText.push(this.choices.map((choice, i) => i === this.cursor ? `${prefix} ${color.green(useAscii ? '>' : '●')} ${this.highlight(choice.label)} ${choice.hint ? color.dim(choice.hint) : ''}` : color.dim(`${prefix} ${useAscii ? '—' : '○'} ${choice.label} `)).join('\n')) } this.outputText = this.outputText.join('') diff --git a/src/prompt/elements/text.js b/src/prompt/elements/text.js index 7c986ab..50e7d62 100644 --- a/src/prompt/elements/text.js +++ b/src/prompt/elements/text.js @@ -1,7 +1,7 @@ import Prompt from './prompt.js'; import { erase, cursor } from 'sisteransi'; import color from 'chalk'; -import { isWin } from '../../utils/index.js'; +import { useAscii } from '../../utils/index.js'; import clear, { lines, strip } from '../util/clear.js'; /** @@ -208,7 +208,7 @@ export default class TextPrompt extends Prompt { ].join(''); if (this.error) { - this.outputError += ` ${color.redBright((isWin ? '> ' : '▶ ') + this.errorMsg)}`; + this.outputError += ` ${color.redBright((useAscii ? '> ' : '▶ ') + this.errorMsg)}`; } this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore + cursor.move(this.cursorOffset, 0)); diff --git a/src/utils/index.ts b/src/utils/index.ts index 6cb3d0d..359e74f 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,7 +4,10 @@ import { exec } from 'node:child_process'; import { platform } from 'node:os'; import { strip } from '../prompt/util/clear.js'; -export const isWin = platform() === 'win32'; +const toBoolean = (value: '0' | 'false' | string) => !['0', 'false'].includes(value) + +const { FORCE_UNICODE = '0' } = process.env ?? {}; +export const useAscii = platform() === 'darwin' && !toBoolean(FORCE_UNICODE); export const hookExit = () => { const onExit = (code: number) => {