@iwascreatedtocreate/asciikit 0.1.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/text.js ADDED
@@ -0,0 +1,230 @@
1
+ import { own } from './util.js';
2
+ import { cells, charWidth, graphemes, stringWidth } from './width.js';
3
+ /** Word-wrap plain text to `width` columns. Emoji and CJK count as two columns. */
4
+ export function wrapText(text, width, options = {}) {
5
+ const max = Math.max(1, Math.floor(width) || 1);
6
+ const hard = options.hard ?? true;
7
+ const out = [];
8
+ for (const para of String(text).split('\n')) {
9
+ const words = para.split(/(\s+)/).filter((w) => w !== '');
10
+ let line = '';
11
+ let lineW = 0;
12
+ const flush = () => {
13
+ out.push(options.keepSpaces ? line : line.trim());
14
+ line = '';
15
+ lineW = 0;
16
+ };
17
+ for (const word of words) {
18
+ const w = stringWidth(word);
19
+ if (/^\s+$/.test(word)) {
20
+ if (lineW && lineW + w <= max) {
21
+ line += word;
22
+ lineW += w;
23
+ }
24
+ else if (lineW)
25
+ flush();
26
+ continue;
27
+ }
28
+ if (lineW + w <= max) {
29
+ line += word;
30
+ lineW += w;
31
+ continue;
32
+ }
33
+ if (lineW && line.trim())
34
+ flush();
35
+ else {
36
+ line = '';
37
+ lineW = 0;
38
+ }
39
+ if (w <= max || !hard) {
40
+ line = word;
41
+ lineW = w;
42
+ continue;
43
+ }
44
+ // word is longer than a whole line: chop it into column-sized pieces
45
+ let piece = '';
46
+ let pieceW = 0;
47
+ for (const g of graphemes(word)) {
48
+ const gw = charWidth(g);
49
+ if (pieceW + gw > max && piece) {
50
+ out.push(piece);
51
+ piece = '';
52
+ pieceW = 0;
53
+ }
54
+ piece += g;
55
+ pieceW += gw;
56
+ }
57
+ line = piece;
58
+ lineW = pieceW;
59
+ }
60
+ flush();
61
+ }
62
+ return out.join('\n');
63
+ }
64
+ /** Cut text to at most `width` columns, ending with `ellipsis` when it was cut. */
65
+ export function truncate(text, width, ellipsis = '…') {
66
+ const max = Math.max(0, Math.floor(width) || 0);
67
+ if (stringWidth(text) <= max)
68
+ return text;
69
+ const room = max - stringWidth(ellipsis);
70
+ if (room <= 0)
71
+ return cells(ellipsis).slice(0, max).join('');
72
+ let out = '';
73
+ let w = 0;
74
+ for (const g of graphemes(text)) {
75
+ const gw = charWidth(g);
76
+ if (w + gw > room)
77
+ break;
78
+ out += g;
79
+ w += gw;
80
+ }
81
+ return out + ellipsis;
82
+ }
83
+ // ---------------------------------------------------------------------------
84
+ // fancy unicode text styles
85
+ const A = 65;
86
+ const a = 97;
87
+ const ZERO = 48;
88
+ const from = (cp) => String.fromCodePoint(cp);
89
+ // note: the math alphanumeric block has holes where letters already existed (ℎ, ℬ, ℭ…)
90
+ const MATH = {
91
+ bold: { upper: 0x1d400, lower: 0x1d41a, digits: 0x1d7ce },
92
+ italic: { upper: 0x1d434, lower: 0x1d44e, holes: { h: 0x210e } },
93
+ boldItalic: { upper: 0x1d468, lower: 0x1d482 },
94
+ script: {
95
+ upper: 0x1d49c,
96
+ lower: 0x1d4b6,
97
+ holes: { B: 0x212c, E: 0x2130, F: 0x2131, H: 0x210b, I: 0x2110, L: 0x2112, M: 0x2133, R: 0x211b, e: 0x212f, g: 0x210a, o: 0x2134 },
98
+ },
99
+ boldScript: { upper: 0x1d4d0, lower: 0x1d4ea },
100
+ fraktur: { upper: 0x1d504, lower: 0x1d51e, holes: { C: 0x212d, H: 0x210c, I: 0x2111, R: 0x211c, Z: 0x2128 } },
101
+ boldFraktur: { upper: 0x1d56c, lower: 0x1d586 },
102
+ doubleStruck: {
103
+ upper: 0x1d538,
104
+ lower: 0x1d552,
105
+ digits: 0x1d7d8,
106
+ holes: { C: 0x2102, H: 0x210d, N: 0x2115, P: 0x2119, Q: 0x211a, R: 0x211d, Z: 0x2124 },
107
+ },
108
+ sans: { upper: 0x1d5a0, lower: 0x1d5ba, digits: 0x1d7e2 },
109
+ sansBold: { upper: 0x1d5d4, lower: 0x1d5ee, digits: 0x1d7ec },
110
+ sansItalic: { upper: 0x1d608, lower: 0x1d622 },
111
+ sansBoldItalic: { upper: 0x1d63c, lower: 0x1d656 },
112
+ monospace: { upper: 0x1d670, lower: 0x1d68a, digits: 0x1d7f6 },
113
+ };
114
+ const SMALL_CAPS = 'ᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘǫʀꜱᴛᴜᴠᴡxʏᴢ';
115
+ const UPSIDE_LO = 'ɐqɔpǝɟƃɥᴉɾʞlɯuodbɹsʇnʌʍxʎz';
116
+ const UPSIDE_UP = '∀ᗺƆᗡƎℲ⅁HIſꞰ˥WNOԀῸᴚS⊥∩ΛMX⅄Z';
117
+ const UPSIDE_DIGITS = '0ƖᘔƐᔭϛ9L86';
118
+ const UPSIDE_PUNCT = {
119
+ '.': '˙', ',': "'", "'": ',', '"': '„', '?': '¿', '!': '¡', '(': ')', ')': '(', '[': ']', ']': '[',
120
+ '{': '}', '}': '{', '<': '>', '>': '<', _: '‾', '&': '⅋', ';': '؛',
121
+ };
122
+ const COMBINING = {
123
+ strike: '̶',
124
+ underline: '̲',
125
+ doubleUnderline: '̳',
126
+ overline: '̅',
127
+ slash: '̸',
128
+ };
129
+ export const fancyStyles = [
130
+ ...Object.keys(MATH),
131
+ 'fullwidth',
132
+ 'circled',
133
+ 'circledNegative',
134
+ 'squared',
135
+ 'squaredNegative',
136
+ 'parenthesized',
137
+ 'smallCaps',
138
+ 'upsideDown',
139
+ ...Object.keys(COMBINING),
140
+ ];
141
+ function mapChar(ch, style) {
142
+ const cp = ch.codePointAt(0);
143
+ const up = ch >= 'A' && ch <= 'Z';
144
+ const lo = ch >= 'a' && ch <= 'z';
145
+ const dig = ch >= '0' && ch <= '9';
146
+ const math = own(MATH, style);
147
+ if (math) {
148
+ const hole = math.holes && own(math.holes, ch);
149
+ if (hole)
150
+ return from(hole);
151
+ if (up)
152
+ return from(math.upper + cp - A);
153
+ if (lo && math.lower)
154
+ return from(math.lower + cp - a);
155
+ if (dig && math.digits)
156
+ return from(math.digits + cp - ZERO);
157
+ return ch;
158
+ }
159
+ switch (style) {
160
+ case 'fullwidth':
161
+ if (ch === ' ')
162
+ return ' ';
163
+ return cp > 0x20 && cp < 0x7f ? from(cp + 0xfee0) : ch;
164
+ case 'circled':
165
+ if (up)
166
+ return from(0x24b6 + cp - A);
167
+ if (lo)
168
+ return from(0x24d0 + cp - a);
169
+ if (ch === '0')
170
+ return '⓪';
171
+ if (dig)
172
+ return from(0x2460 + cp - ZERO - 1);
173
+ return ch;
174
+ case 'circledNegative':
175
+ if (up || lo)
176
+ return from(0x1f150 + ch.toUpperCase().charCodeAt(0) - A);
177
+ if (ch === '0')
178
+ return '⓿';
179
+ if (dig)
180
+ return from(0x2776 + cp - ZERO - 1);
181
+ return ch;
182
+ case 'squared':
183
+ return up || lo ? from(0x1f130 + ch.toUpperCase().charCodeAt(0) - A) : ch;
184
+ case 'squaredNegative':
185
+ return up || lo ? from(0x1f170 + ch.toUpperCase().charCodeAt(0) - A) : ch;
186
+ case 'parenthesized':
187
+ if (up || lo)
188
+ return from(0x249c + ch.toLowerCase().charCodeAt(0) - a);
189
+ if (dig && ch !== '0')
190
+ return from(0x2474 + cp - ZERO - 1);
191
+ return ch;
192
+ case 'smallCaps':
193
+ return up || lo ? SMALL_CAPS[ch.toLowerCase().charCodeAt(0) - a] : ch;
194
+ case 'upsideDown':
195
+ if (lo)
196
+ return [...UPSIDE_LO][cp - a];
197
+ if (up)
198
+ return [...UPSIDE_UP][cp - A];
199
+ if (dig)
200
+ return [...UPSIDE_DIGITS][cp - ZERO];
201
+ return own(UPSIDE_PUNCT, ch) ?? ch;
202
+ default: {
203
+ const mark = own(COMBINING, style);
204
+ return mark && ch !== ' ' && ch !== '\n' ? ch + mark : ch;
205
+ }
206
+ }
207
+ }
208
+ /**
209
+ * Restyle text with Unicode look-alike characters (𝐛𝐨𝐥𝐝, 𝔣𝔯𝔞𝔨𝔱𝔲𝔯, ⓒⓘⓡⓒⓛⓔⓓ, ꜱᴍᴀʟʟ ᴄᴀᴘꜱ, uʍop ǝpısdn…).
210
+ * Works anywhere plain text does: bios, titles, terminals. Pass several styles to combine
211
+ * a letter style with marks, e.g. `fancy('hi', ['bold', 'underline'])`.
212
+ */
213
+ export function fancy(text, style) {
214
+ const styles = typeof style === 'string' ? [style] : style;
215
+ let out = String(text);
216
+ for (const s of styles) {
217
+ out = out
218
+ .split('\n')
219
+ .map((line) => {
220
+ // map the base char only, so accents and other marks stay attached
221
+ const mapped = graphemes(line).map((g) => {
222
+ const base = String.fromCodePoint(g.codePointAt(0));
223
+ return mapChar(base, s) + g.slice(base.length);
224
+ });
225
+ return (s === 'upsideDown' ? mapped.reverse() : mapped).join('');
226
+ })
227
+ .join('\n');
228
+ }
229
+ return out;
230
+ }
package/package.json CHANGED
@@ -1,87 +1,93 @@
1
- {
2
- "name": "@iwascreatedtocreate/asciikit",
3
- "version": "0.1.2",
4
- "description": "The advanced ASCII art toolkit for React & Next.js — 300+ FIGlet fonts, full smushing engine, effects, animations and image-to-ASCII. Import fonts like icons.",
5
- "keywords": [
6
- "ascii",
7
- "ascii-art",
8
- "figlet",
9
- "react",
10
- "nextjs",
11
- "banner",
12
- "text-art",
13
- "fonts",
14
- "terminal"
15
- ],
16
- "homepage": "https://asciikit.iwascreatedtocreate.com",
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/iwascreatedtocreate/asciikit.git"
20
- },
21
- "bugs": {
22
- "url": "https://github.com/iwascreatedtocreate/asciikit/issues"
23
- },
24
- "license": "MIT",
25
- "type": "module",
26
- "sideEffects": false,
27
- "main": "./dist/index.js",
28
- "module": "./dist/index.js",
29
- "types": "./dist/index.d.ts",
30
- "exports": {
31
- ".": {
32
- "types": "./dist/index.d.ts",
33
- "default": "./dist/index.js"
34
- },
35
- "./react": {
36
- "types": "./dist/react/index.d.ts",
37
- "default": "./dist/react/index.js"
38
- },
39
- "./fonts": {
40
- "types": "./dist/fonts/index.d.ts",
41
- "default": "./dist/fonts/index.js"
42
- },
43
- "./fonts/lazy": {
44
- "types": "./dist/fonts/lazy.d.ts",
45
- "default": "./dist/fonts/lazy.js"
46
- },
47
- "./fonts/meta": {
48
- "types": "./dist/fonts/meta.d.ts",
49
- "default": "./dist/fonts/meta.js"
50
- },
51
- "./fonts/*": {
52
- "types": "./dist/fonts/*.d.ts",
53
- "default": "./dist/fonts/*.js"
54
- },
55
- "./package.json": "./package.json"
56
- },
57
- "files": [
58
- "dist",
59
- "README.md",
60
- "LICENSE"
61
- ],
62
- "scripts": {
63
- "build": "node scripts/generate-fonts.mjs && tsc -p tsconfig.json",
64
- "test": "node --test test/core.test.mjs"
65
- },
66
- "peerDependencies": {
67
- "react": ">=18"
68
- },
69
- "peerDependenciesMeta": {
70
- "react": {
71
- "optional": true
72
- }
73
- },
74
- "devDependencies": {
75
- "@types/node": "^22.20.7",
76
- "@types/react": "^19.3.3",
77
- "figlet": "^1.13.0",
78
- "react": "^19.3.1",
79
- "typescript": "^5.9.3"
80
- },
81
- "bin": {
82
- "asciikit": "./dist/cli.js"
83
- },
84
- "engines": {
85
- "node": ">=18.3"
86
- }
87
- }
1
+ {
2
+ "name": "@iwascreatedtocreate/asciikit",
3
+ "version": "0.3.0",
4
+ "description": "The advanced ASCII art toolkit for React & Next.js — 300+ FIGlet fonts, full smushing engine, effects, animations and image-to-ASCII. Import fonts like icons.",
5
+ "keywords": [
6
+ "ascii",
7
+ "ascii-art",
8
+ "figlet",
9
+ "react",
10
+ "nextjs",
11
+ "banner",
12
+ "text-art",
13
+ "fonts",
14
+ "terminal"
15
+ ],
16
+ "homepage": "https://asciikit.iwascreatedtocreate.com",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/iwascreatedtocreate/asciikit.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/iwascreatedtocreate/asciikit/issues"
23
+ },
24
+ "license": "MIT",
25
+ "type": "module",
26
+ "sideEffects": [
27
+ "./dist/element.js"
28
+ ],
29
+ "main": "./dist/index.js",
30
+ "module": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "default": "./dist/index.js"
36
+ },
37
+ "./react": {
38
+ "types": "./dist/react/index.d.ts",
39
+ "default": "./dist/react/index.js"
40
+ },
41
+ "./element": {
42
+ "types": "./dist/element.d.ts",
43
+ "default": "./dist/element.js"
44
+ },
45
+ "./fonts": {
46
+ "types": "./dist/fonts/index.d.ts",
47
+ "default": "./dist/fonts/index.js"
48
+ },
49
+ "./fonts/lazy": {
50
+ "types": "./dist/fonts/lazy.d.ts",
51
+ "default": "./dist/fonts/lazy.js"
52
+ },
53
+ "./fonts/meta": {
54
+ "types": "./dist/fonts/meta.d.ts",
55
+ "default": "./dist/fonts/meta.js"
56
+ },
57
+ "./fonts/*": {
58
+ "types": "./dist/fonts/*.d.ts",
59
+ "default": "./dist/fonts/*.js"
60
+ },
61
+ "./package.json": "./package.json"
62
+ },
63
+ "files": [
64
+ "dist",
65
+ "README.md",
66
+ "LICENSE"
67
+ ],
68
+ "scripts": {
69
+ "build": "node scripts/generate-fonts.mjs && tsc -p tsconfig.json",
70
+ "test": "node --test test/core.test.mjs"
71
+ },
72
+ "peerDependencies": {
73
+ "react": ">=18"
74
+ },
75
+ "peerDependenciesMeta": {
76
+ "react": {
77
+ "optional": true
78
+ }
79
+ },
80
+ "devDependencies": {
81
+ "@types/node": "^24.19.4",
82
+ "@types/react": "^19.3.3",
83
+ "figlet": "^1.13.0",
84
+ "react": "^19.3.1",
85
+ "typescript": "^7.0.2"
86
+ },
87
+ "bin": {
88
+ "asciikit": "./dist/cli.js"
89
+ },
90
+ "engines": {
91
+ "node": ">=18.3"
92
+ }
93
+ }