Skip to content

Commit

Permalink
feat: better unicode support
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Oct 28, 2022
1 parent 3540a32 commit f5693e9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
Expand Down
12 changes: 6 additions & 6 deletions src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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++;
Expand Down
4 changes: 2 additions & 2 deletions src/prompt/elements/select.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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('')

Expand Down
4 changes: 2 additions & 2 deletions src/prompt/elements/text.js
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 4 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit f5693e9

Please sign in to comment.