@visulima/fmt 1.1.18 → 1.1.20

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/index.mjs CHANGED
@@ -1 +1,651 @@
1
- var $=Object.defineProperty;var h=(e,r)=>$(e,"name",{value:r,configurable:!0});var v=Object.defineProperty,m=h((e,r)=>v(e,"name",{value:r,configurable:!0}),"m$1");const y=new Map([["aliceblue","#f0f8ff"],["antiquewhite","#faebd7"],["aqua","#00ffff"],["aquamarine","#7fffd4"],["azure","#f0ffff"],["beige","#f5f5dc"],["bisque","#ffe4c4"],["black","#000000"],["blanchedalmond","#ffebcd"],["blue","#0000ff"],["blueviolet","#8a2be2"],["brown","#a52a2a"],["burlywood","#deb887"],["cadetblue","#5f9ea0"],["chartreuse","#7fff00"],["chocolate","#d2691e"],["coral","#ff7f50"],["cornflowerblue","#6495ed"],["cornsilk","#fff8dc"],["crimson","#dc143c"],["cyan","#00ffff"],["darkblue","#00008b"],["darkcyan","#008b8b"],["darkgoldenrod","#b8860b"],["darkgray","#a9a9a9"],["darkgreen","#006400"],["darkgrey","#a9a9a9"],["darkkhaki","#bdb76b"],["darkmagenta","#8b008b"],["darkolivegreen","#556b2f"],["darkorange","#ff8c00"],["darkorchid","#9932cc"],["darkred","#8b0000"],["darksalmon","#e9967a"],["darkseagreen","#8fbc8f"],["darkslateblue","#483d8b"],["darkslategray","#2f4f4f"],["darkslategrey","#2f4f4f"],["darkturquoise","#00ced1"],["darkviolet","#9400d3"],["deeppink","#ff1493"],["deepskyblue","#00bfff"],["dimgray","#696969"],["dimgrey","#696969"],["dodgerblue","#1e90ff"],["firebrick","#b22222"],["floralwhite","#fffaf0"],["forestgreen","#228b22"],["fuchsia","#ff00ff"],["gainsboro","#dcdcdc"],["ghostwhite","#f8f8ff"],["gold","#ffd700"],["goldenrod","#daa520"],["gray","#808080"],["green","#008000"],["greenyellow","#adff2f"],["grey","#808080"],["honeydew","#f0fff0"],["hotpink","#ff69b4"],["indianred","#cd5c5c"],["indigo","#4b0082"],["ivory","#fffff0"],["khaki","#f0e68c"],["lavender","#e6e6fa"],["lavenderblush","#fff0f5"],["lawngreen","#7cfc00"],["lemonchiffon","#fffacd"],["lightblue","#add8e6"],["lightcoral","#f08080"],["lightcyan","#e0ffff"],["lightgoldenrodyellow","#fafad2"],["lightgray","#d3d3d3"],["lightgreen","#90ee90"],["lightgrey","#d3d3d3"],["lightpink","#ffb6c1"],["lightsalmon","#ffa07a"],["lightseagreen","#20b2aa"],["lightskyblue","#87cefa"],["lightslategray","#778899"],["lightslategrey","#778899"],["lightsteelblue","#b0c4de"],["lightyellow","#ffffe0"],["lime","#00ff00"],["limegreen","#32cd32"],["linen","#faf0e6"],["magenta","#ff00ff"],["maroon","#800000"],["mediumaquamarine","#66cdaa"],["mediumblue","#0000cd"],["mediumorchid","#ba55d3"],["mediumpurple","#9370db"],["mediumseagreen","#3cb371"],["mediumslateblue","#7b68ee"],["mediumspringgreen","#00fa9a"],["mediumturquoise","#48d1cc"],["mediumvioletred","#c71585"],["midnightblue","#191970"],["mintcream","#f5fffa"],["mistyrose","#ffe4e1"],["moccasin","#ffe4b5"],["navajowhite","#ffdead"],["navy","#000080"],["oldlace","#fdf5e6"],["olive","#808000"],["olivedrab","#6b8e23"],["orange","#ffa500"],["orangered","#ff4500"],["orchid","#da70d6"],["palegoldenrod","#eee8aa"],["palegreen","#98fb98"],["paleturquoise","#afeeee"],["palevioletred","#db7093"],["papayawhip","#ffefd5"],["peachpuff","#ffdab9"],["peru","#cd853f"],["pink","#ffc0cb"],["plum","#dda0dd"],["powderblue","#b0e0e6"],["purple","#800080"],["rebeccapurple","#663399"],["red","#ff0000"],["rosybrown","#bc8f8f"],["royalblue","#4169e1"],["saddlebrown","#8b4513"],["salmon","#fa8072"],["sandybrown","#f4a460"],["seagreen","#2e8b57"],["seashell","#fff5ee"],["sienna","#a0522d"],["silver","#c0c0c0"],["skyblue","#87ceeb"],["slateblue","#6a5acd"],["slategray","#708090"],["slategrey","#708090"],["snow","#fffafa"],["springgreen","#00ff7f"],["steelblue","#4682b4"],["tan","#d2b48c"],["teal","#008080"],["thistle","#d8bfd8"],["tomato","#ff6347"],["turquoise","#40e0d0"],["violet","#ee82ee"],["wheat","#f5deb3"],["white","#ffffff"],["whitesmoke","#f5f5f5"],["yellow","#ffff00"],["yellowgreen","#9acd32"]]),A=/^#([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})?$/i,C=/^#([\dA-F])([\dA-F])([\dA-F])([\dA-F])?$/i,D=/^rgba?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/,M=/^hsla?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/,B=m(()=>({__proto__:null,backgroundColor:null,color:null,fontStyle:null,fontWeight:null,textDecorationColor:null,textDecorationLine:[]}),"getDefaultCss"),p=/\s+/g,g=m(e=>{y.has(e)&&(e=y.get(e));const r=A.exec(e);if(r)return[Number.parseInt(r[1],16),Number.parseInt(r[2],16),Number.parseInt(r[3],16)];const t=C.exec(e);if(t)return[Number.parseInt(`${t[1]}${t[1]}`,16),Number.parseInt(`${t[2]}${t[2]}`,16),Number.parseInt(`${t[3]}${t[3]}`,16)];const f=D.exec(e);if(f)return[Math.round(Math.max(0,Math.min(255,Number(f[1])))),Math.round(Math.max(0,Math.min(255,Number(f[2])))),Math.round(Math.max(0,Math.min(255,Number(f[3]))))];const i=M.exec(e);if(i){let a=Number(i[1])%360;a<0&&(a+=360);const l=Math.max(0,Math.min(100,Number(i[2])))/100,n=Math.max(0,Math.min(100,Number(i[3])))/100,c=(1-Math.abs(2*n-1))*l,u=c*(1-Math.abs(a/60%2-1)),o=n-c/2;let s,d,b;return a<60?{0:s,1:d,2:b}=[c,u,0]:a<120?{0:s,1:d,2:b}=[u,c,0]:a<180?{0:s,1:d,2:b}=[0,c,u]:a<240?{0:s,1:d,2:b}=[0,u,c]:a<300?{0:s,1:d,2:b}=[u,0,c]:{0:s,1:d,2:b}=[c,0,u],[Math.round((s+o)*255),Math.round((d+o)*255),Math.round((b+o)*255)]}return null},"parseCssColor"),k=m((e,r)=>e?.[0]===r?.[0]&&e?.[1]===r?.[1]&&e?.[2]===r?.[2],"colorEquals"),P=m(e=>{const r=B(),t=[];let f=!1,i=null,a=0,l="";for(const n of e){if(n==="(")a++;else if(a>0)n===")"&&a--;else if(f){if(n===";"){const c=l.trim();c!==""&&t.push([i,c]),i=null,l="",f=!1;continue}}else if(n===":"){i=l.trim(),l="",f=!0;continue}l+=n}if(f&&a===0){const n=l.trim();n!==""&&t.push([i,n]),i=null,l=""}for(const{0:n,1:c}of t)switch(n){case"background-color":{c!=null&&(r.backgroundColor=c);break}case"color":{c!=null&&(r.color=c);break}case"font-style":{["italic","oblique","oblique 14deg"].includes(c)&&(r.fontStyle="italic");break}case"font-weight":{c==="bold"&&(r.fontWeight=c);break}case"text-decoration":{r.textDecorationColor=null,r.textDecorationLine=[];const u=c.split(p);for(const o of u){const s=g(o);s!=null?r.textDecorationColor=s:["line-through","overline","underline"].includes(o)&&r.textDecorationLine.push(o)}break}case"text-decoration-color":{const u=g(c);u!=null&&(r.textDecorationColor=u);break}case"text-decoration-line":{r.textDecorationLine=[];const u=c.split(p);for(const o of u)["line-through","overline","underline"].includes(o)&&r.textDecorationLine.push(o);break}}return r},"parseCss"),N=m((e,r=null)=>{r=r??B();let t="";if(!k(e.backgroundColor,r.backgroundColor))if(e.backgroundColor==null)t+="\x1B[49m";else switch(e.backgroundColor){case"black":{t+="\x1B[40m";break}case"blue":{t+="\x1B[44m";break}case"cyan":{t+="\x1B[46m";break}case"green":{t+="\x1B[42m";break}case"magenta":{t+="\x1B[45m";break}case"red":{t+="\x1B[41m";break}case"white":{t+="\x1B[47m";break}case"yellow":{t+="\x1B[43m";break}default:if(Array.isArray(e.backgroundColor)){const{0:f,1:i,2:a}=e.backgroundColor;t+=`\x1B[48;2;${f};${i};${a}m`}else{const f=g(e.backgroundColor);if(f===null)t+="\x1B[49m";else{const{0:i,1:a,2:l}=f;t+=`\x1B[48;2;${i};${a};${l}m`}}}if(!k(e.color,r.color))if(e.color==null)t+="\x1B[39m";else switch(e.color){case"black":{t+="\x1B[30m";break}case"blue":{t+="\x1B[34m";break}case"cyan":{t+="\x1B[36m";break}case"green":{t+="\x1B[32m";break}case"magenta":{t+="\x1B[35m";break}case"red":{t+="\x1B[31m";break}case"white":{t+="\x1B[37m";break}case"yellow":{t+="\x1B[33m";break}default:if(Array.isArray(e.color)){const{0:f,1:i,2:a}=e.color;t+=`\x1B[38;2;${f};${i};${a}m`}else{const f=g(e.color);if(f===null)t+="\x1B[39m";else{const{0:i,1:a,2:l}=f;t+=`\x1B[38;2;${i};${a};${l}m`}}}if(e.fontWeight!==r.fontWeight&&(t+=e.fontWeight==="bold"?"\x1B[1m":"\x1B[22m"),e.fontStyle!==r.fontStyle&&(t+=e.fontStyle==="italic"?"\x1B[3m":"\x1B[23m"),!k(e.textDecorationColor,r.textDecorationColor))if(e.textDecorationColor==null)t+="\x1B[59m";else{const{0:f,1:i,2:a}=e.textDecorationColor;t+=`\x1B[58;2;${f};${i};${a}m`}return e.textDecorationLine.includes("line-through")!==r.textDecorationLine.includes("line-through")&&(t+=e.textDecorationLine.includes("line-through")?"\x1B[9m":"\x1B[29m"),e.textDecorationLine.includes("overline")!==r.textDecorationLine.includes("overline")&&(t+=e.textDecorationLine.includes("overline")?"\x1B[53m":"\x1B[55m"),e.textDecorationLine.includes("underline")!==r.textDecorationLine.includes("underline")&&(t+=e.textDecorationLine.includes("underline")?"\x1B[4m":"\x1B[24m"),t},"cssToAnsi");var F=Object.defineProperty,x=h((e,r)=>F(e,"name",{value:r,configurable:!0}),"y");const L=x(e=>{try{return JSON.stringify(e)}catch{return'"[Circular]"'}},"tryStringify"),w="%".codePointAt(0),j="s".codePointAt(0),q="d".codePointAt(0),S="f".codePointAt(0),E="i".codePointAt(0),I="O".codePointAt(0),O="o".codePointAt(0),T="j".codePointAt(0),W="c".codePointAt(0),_=x((e,r=[],t={})=>{if(typeof e!="string"&&typeof e!="object"||e===null)throw new TypeError(`fmt must be a string or object, got ${e===null?"null":typeof e}`);const f=t.stringify??L,i=1;if(typeof e=="object"){const o=r.length+i;if(o===1)return"{}";const s=Array.from({length:o});s[0]=f(e);for(let d=1;d<o;d++)s[d]=f(r[d-i]);return s.join(" ")}if(r.length===0)return e;let a="",l=1-i,n=-1,c=!1,u=null;for(let o=0;o<e.length;){if(e.codePointAt(o)===w&&o+1<e.length){n=n>-1?n:0;const s=e.codePointAt(o+1);if(s===void 0){++l;break}switch(s){case W:{if(globalThis.window===void 0){const d=P(r[l]);n<o&&(a+=e.slice(n,o)),a+=N(d,u),a!==""&&(c=!0,u=d)}n=o+2,o++;break}case q:case S:{if(l>=r.length||r[l]==null)break;n<o&&(a+=e.slice(n,o)),a+=Number(r[l]).toString(),n=o+2,o++;break}case E:{if(l>=r.length||r[l]==null)break;n<o&&(a+=e.slice(n,o)),a+=Math.floor(Number(r[l])).toString(),n=o+2,o++;break}case T:case I:case O:{if(l>=r.length||r[l]===void 0)break;n<o&&(a+=e.slice(n,o));const d=r[l],b=typeof d;if(b==="string"){a+=`'${d}'`,n=o+2;break}if(b==="function"){a+=d.name?`[Function: ${d.name}]`:"[Function: <anonymous>]",n=o+2;break}a+=f(d),n=o+2,o++;break}case w:{n<o&&(a+=e.slice(n,o)),a+="%",n=o+2,o++,l--;break}case j:{if(l>=r.length)break;n<o&&(a+=e.slice(n,o)),a+=typeof r[l]=="object"?f(r[l]):String(r[l]),n=o+2,o++;break}default:typeof t.formatters?.[s]=="function"&&(n<o&&(a+=e.slice(n,o)),a+=t.formatters[s](r[l]),n=o+2,o++)}++l}++o}return n===-1?e:(n<e.length&&(a+=e.slice(n)),c&&(a+="\x1B[0m"),a)},"format"),J=x((e={})=>{const r={};return typeof e.formatters=="object"&&Object.entries(e.formatters).forEach(([t,f])=>{if(t.length===0)throw new Error(`Formatter %${t} has no characters`);if(t.length>1)throw new Error(`Formatter %${t} has more than one character`);if(typeof f!="function")throw new TypeError(`Formatter for %${t} is not a function`);const i=t.codePointAt(0);if(i===void 0)throw new Error(`${t}.codePointAt(0) failed to return a value, please report this issue`);r[i]=f}),(t,f=[],i={})=>_(t,f,{...i,formatters:r})},"build");export{J as build,_ as format};
1
+ const colorKeywords = /* @__PURE__ */ new Map([
2
+ ["aliceblue", "#f0f8ff"],
3
+ ["antiquewhite", "#faebd7"],
4
+ ["aqua", "#00ffff"],
5
+ ["aquamarine", "#7fffd4"],
6
+ ["azure", "#f0ffff"],
7
+ ["beige", "#f5f5dc"],
8
+ ["bisque", "#ffe4c4"],
9
+ ["black", "#000000"],
10
+ ["blanchedalmond", "#ffebcd"],
11
+ ["blue", "#0000ff"],
12
+ ["blueviolet", "#8a2be2"],
13
+ ["brown", "#a52a2a"],
14
+ ["burlywood", "#deb887"],
15
+ ["cadetblue", "#5f9ea0"],
16
+ ["chartreuse", "#7fff00"],
17
+ ["chocolate", "#d2691e"],
18
+ ["coral", "#ff7f50"],
19
+ ["cornflowerblue", "#6495ed"],
20
+ ["cornsilk", "#fff8dc"],
21
+ ["crimson", "#dc143c"],
22
+ ["cyan", "#00ffff"],
23
+ ["darkblue", "#00008b"],
24
+ ["darkcyan", "#008b8b"],
25
+ ["darkgoldenrod", "#b8860b"],
26
+ ["darkgray", "#a9a9a9"],
27
+ ["darkgreen", "#006400"],
28
+ ["darkgrey", "#a9a9a9"],
29
+ ["darkkhaki", "#bdb76b"],
30
+ ["darkmagenta", "#8b008b"],
31
+ ["darkolivegreen", "#556b2f"],
32
+ ["darkorange", "#ff8c00"],
33
+ ["darkorchid", "#9932cc"],
34
+ ["darkred", "#8b0000"],
35
+ ["darksalmon", "#e9967a"],
36
+ ["darkseagreen", "#8fbc8f"],
37
+ ["darkslateblue", "#483d8b"],
38
+ ["darkslategray", "#2f4f4f"],
39
+ ["darkslategrey", "#2f4f4f"],
40
+ ["darkturquoise", "#00ced1"],
41
+ ["darkviolet", "#9400d3"],
42
+ ["deeppink", "#ff1493"],
43
+ ["deepskyblue", "#00bfff"],
44
+ ["dimgray", "#696969"],
45
+ ["dimgrey", "#696969"],
46
+ ["dodgerblue", "#1e90ff"],
47
+ ["firebrick", "#b22222"],
48
+ ["floralwhite", "#fffaf0"],
49
+ ["forestgreen", "#228b22"],
50
+ ["fuchsia", "#ff00ff"],
51
+ ["gainsboro", "#dcdcdc"],
52
+ ["ghostwhite", "#f8f8ff"],
53
+ ["gold", "#ffd700"],
54
+ ["goldenrod", "#daa520"],
55
+ ["gray", "#808080"],
56
+ ["green", "#008000"],
57
+ ["greenyellow", "#adff2f"],
58
+ ["grey", "#808080"],
59
+ ["honeydew", "#f0fff0"],
60
+ ["hotpink", "#ff69b4"],
61
+ ["indianred", "#cd5c5c"],
62
+ ["indigo", "#4b0082"],
63
+ ["ivory", "#fffff0"],
64
+ ["khaki", "#f0e68c"],
65
+ ["lavender", "#e6e6fa"],
66
+ ["lavenderblush", "#fff0f5"],
67
+ ["lawngreen", "#7cfc00"],
68
+ ["lemonchiffon", "#fffacd"],
69
+ ["lightblue", "#add8e6"],
70
+ ["lightcoral", "#f08080"],
71
+ ["lightcyan", "#e0ffff"],
72
+ ["lightgoldenrodyellow", "#fafad2"],
73
+ ["lightgray", "#d3d3d3"],
74
+ ["lightgreen", "#90ee90"],
75
+ ["lightgrey", "#d3d3d3"],
76
+ ["lightpink", "#ffb6c1"],
77
+ ["lightsalmon", "#ffa07a"],
78
+ ["lightseagreen", "#20b2aa"],
79
+ ["lightskyblue", "#87cefa"],
80
+ ["lightslategray", "#778899"],
81
+ ["lightslategrey", "#778899"],
82
+ ["lightsteelblue", "#b0c4de"],
83
+ ["lightyellow", "#ffffe0"],
84
+ ["lime", "#00ff00"],
85
+ ["limegreen", "#32cd32"],
86
+ ["linen", "#faf0e6"],
87
+ ["magenta", "#ff00ff"],
88
+ ["maroon", "#800000"],
89
+ ["mediumaquamarine", "#66cdaa"],
90
+ ["mediumblue", "#0000cd"],
91
+ ["mediumorchid", "#ba55d3"],
92
+ ["mediumpurple", "#9370db"],
93
+ ["mediumseagreen", "#3cb371"],
94
+ ["mediumslateblue", "#7b68ee"],
95
+ ["mediumspringgreen", "#00fa9a"],
96
+ ["mediumturquoise", "#48d1cc"],
97
+ ["mediumvioletred", "#c71585"],
98
+ ["midnightblue", "#191970"],
99
+ ["mintcream", "#f5fffa"],
100
+ ["mistyrose", "#ffe4e1"],
101
+ ["moccasin", "#ffe4b5"],
102
+ ["navajowhite", "#ffdead"],
103
+ ["navy", "#000080"],
104
+ ["oldlace", "#fdf5e6"],
105
+ ["olive", "#808000"],
106
+ ["olivedrab", "#6b8e23"],
107
+ ["orange", "#ffa500"],
108
+ ["orangered", "#ff4500"],
109
+ ["orchid", "#da70d6"],
110
+ ["palegoldenrod", "#eee8aa"],
111
+ ["palegreen", "#98fb98"],
112
+ ["paleturquoise", "#afeeee"],
113
+ ["palevioletred", "#db7093"],
114
+ ["papayawhip", "#ffefd5"],
115
+ ["peachpuff", "#ffdab9"],
116
+ ["peru", "#cd853f"],
117
+ ["pink", "#ffc0cb"],
118
+ ["plum", "#dda0dd"],
119
+ ["powderblue", "#b0e0e6"],
120
+ ["purple", "#800080"],
121
+ ["rebeccapurple", "#663399"],
122
+ ["red", "#ff0000"],
123
+ ["rosybrown", "#bc8f8f"],
124
+ ["royalblue", "#4169e1"],
125
+ ["saddlebrown", "#8b4513"],
126
+ ["salmon", "#fa8072"],
127
+ ["sandybrown", "#f4a460"],
128
+ ["seagreen", "#2e8b57"],
129
+ ["seashell", "#fff5ee"],
130
+ ["sienna", "#a0522d"],
131
+ ["silver", "#c0c0c0"],
132
+ ["skyblue", "#87ceeb"],
133
+ ["slateblue", "#6a5acd"],
134
+ ["slategray", "#708090"],
135
+ ["slategrey", "#708090"],
136
+ ["snow", "#fffafa"],
137
+ ["springgreen", "#00ff7f"],
138
+ ["steelblue", "#4682b4"],
139
+ ["tan", "#d2b48c"],
140
+ ["teal", "#008080"],
141
+ ["thistle", "#d8bfd8"],
142
+ ["tomato", "#ff6347"],
143
+ ["turquoise", "#40e0d0"],
144
+ ["violet", "#ee82ee"],
145
+ ["wheat", "#f5deb3"],
146
+ ["white", "#ffffff"],
147
+ ["whitesmoke", "#f5f5f5"],
148
+ ["yellow", "#ffff00"],
149
+ ["yellowgreen", "#9acd32"]
150
+ ]);
151
+ const HASH_PATTERN = /^#([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})?$/i;
152
+ const SMALL_HASH_PATTERN = /^#([\dA-F])([\dA-F])([\dA-F])([\dA-F])?$/i;
153
+ const RGB_PATTERN = /^rgba?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/;
154
+ const HSL_PATTERN = /^hsla?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/;
155
+ const getDefaultCss = () => {
156
+ return {
157
+ __proto__: null,
158
+ backgroundColor: null,
159
+ color: null,
160
+ fontStyle: null,
161
+ fontWeight: null,
162
+ textDecorationColor: null,
163
+ textDecorationLine: []
164
+ };
165
+ };
166
+ const SPACE_PATTERN = /\s+/g;
167
+ const parseCssColor = (colorString) => {
168
+ if (colorKeywords.has(colorString)) {
169
+ colorString = colorKeywords.get(colorString);
170
+ }
171
+ const hashMatch = HASH_PATTERN.exec(colorString);
172
+ if (hashMatch) {
173
+ return [Number.parseInt(hashMatch[1], 16), Number.parseInt(hashMatch[2], 16), Number.parseInt(hashMatch[3], 16)];
174
+ }
175
+ const smallHashMatch = SMALL_HASH_PATTERN.exec(colorString);
176
+ if (smallHashMatch) {
177
+ return [
178
+ Number.parseInt(`${smallHashMatch[1]}${smallHashMatch[1]}`, 16),
179
+ Number.parseInt(`${smallHashMatch[2]}${smallHashMatch[2]}`, 16),
180
+ Number.parseInt(`${smallHashMatch[3]}${smallHashMatch[3]}`, 16)
181
+ ];
182
+ }
183
+ const rgbMatch = RGB_PATTERN.exec(colorString);
184
+ if (rgbMatch) {
185
+ return [
186
+ Math.round(Math.max(0, Math.min(255, Number(rgbMatch[1])))),
187
+ Math.round(Math.max(0, Math.min(255, Number(rgbMatch[2])))),
188
+ Math.round(Math.max(0, Math.min(255, Number(rgbMatch[3]))))
189
+ ];
190
+ }
191
+ const hslMatch = HSL_PATTERN.exec(colorString);
192
+ if (hslMatch) {
193
+ let h = Number(hslMatch[1]) % 360;
194
+ if (h < 0) {
195
+ h += 360;
196
+ }
197
+ const s = Math.max(0, Math.min(100, Number(hslMatch[2]))) / 100;
198
+ const l = Math.max(0, Math.min(100, Number(hslMatch[3]))) / 100;
199
+ const c = (1 - Math.abs(2 * l - 1)) * s;
200
+ const x = c * (1 - Math.abs(h / 60 % 2 - 1));
201
+ const m = l - c / 2;
202
+ let r_;
203
+ let g_;
204
+ let b_;
205
+ if (h < 60) {
206
+ ({ 0: r_, 1: g_, 2: b_ } = [c, x, 0]);
207
+ } else if (h < 120) {
208
+ ({ 0: r_, 1: g_, 2: b_ } = [x, c, 0]);
209
+ } else if (h < 180) {
210
+ ({ 0: r_, 1: g_, 2: b_ } = [0, c, x]);
211
+ } else if (h < 240) {
212
+ ({ 0: r_, 1: g_, 2: b_ } = [0, x, c]);
213
+ } else if (h < 300) {
214
+ ({ 0: r_, 1: g_, 2: b_ } = [x, 0, c]);
215
+ } else {
216
+ ({ 0: r_, 1: g_, 2: b_ } = [c, 0, x]);
217
+ }
218
+ return [Math.round((r_ + m) * 255), Math.round((g_ + m) * 255), Math.round((b_ + m) * 255)];
219
+ }
220
+ return null;
221
+ };
222
+ const colorEquals = (color1, color2) => color1?.[0] === color2?.[0] && color1?.[1] === color2?.[1] && color1?.[2] === color2?.[2];
223
+ const parseCss = (cssString) => {
224
+ const css = getDefaultCss();
225
+ const rawEntries = [];
226
+ let inValue = false;
227
+ let currentKey = null;
228
+ let parenthesesDepth = 0;
229
+ let currentPart = "";
230
+ for (const c of cssString) {
231
+ if (c === "(") {
232
+ parenthesesDepth++;
233
+ } else if (parenthesesDepth > 0) {
234
+ if (c === ")") {
235
+ parenthesesDepth--;
236
+ }
237
+ } else if (inValue) {
238
+ if (c === ";") {
239
+ const value = currentPart.trim();
240
+ if (value !== "") {
241
+ rawEntries.push([currentKey, value]);
242
+ }
243
+ currentKey = null;
244
+ currentPart = "";
245
+ inValue = false;
246
+ continue;
247
+ }
248
+ } else if (c === ":") {
249
+ currentKey = currentPart.trim();
250
+ currentPart = "";
251
+ inValue = true;
252
+ continue;
253
+ }
254
+ currentPart += c;
255
+ }
256
+ if (inValue && parenthesesDepth === 0) {
257
+ const value = currentPart.trim();
258
+ if (value !== "") {
259
+ rawEntries.push([currentKey, value]);
260
+ }
261
+ currentKey = null;
262
+ currentPart = "";
263
+ }
264
+ for (const { 0: key, 1: value } of rawEntries) {
265
+ switch (key) {
266
+ case "background-color": {
267
+ if (value != void 0) {
268
+ css.backgroundColor = value;
269
+ }
270
+ break;
271
+ }
272
+ case "color": {
273
+ if (value != void 0) {
274
+ css.color = value;
275
+ }
276
+ break;
277
+ }
278
+ case "font-style": {
279
+ if (["italic", "oblique", "oblique 14deg"].includes(value)) {
280
+ css.fontStyle = "italic";
281
+ }
282
+ break;
283
+ }
284
+ case "font-weight": {
285
+ if (value === "bold") {
286
+ css.fontWeight = value;
287
+ }
288
+ break;
289
+ }
290
+ case "text-decoration": {
291
+ css.textDecorationColor = null;
292
+ css.textDecorationLine = [];
293
+ const arguments_ = value.split(SPACE_PATTERN);
294
+ for (const argument of arguments_) {
295
+ const maybeColor = parseCssColor(argument);
296
+ if (maybeColor != void 0) {
297
+ css.textDecorationColor = maybeColor;
298
+ } else if (["line-through", "overline", "underline"].includes(argument)) {
299
+ css.textDecorationLine.push(argument);
300
+ }
301
+ }
302
+ break;
303
+ }
304
+ case "text-decoration-color": {
305
+ const color = parseCssColor(value);
306
+ if (color != void 0) {
307
+ css.textDecorationColor = color;
308
+ }
309
+ break;
310
+ }
311
+ case "text-decoration-line": {
312
+ css.textDecorationLine = [];
313
+ const lineTypes = value.split(SPACE_PATTERN);
314
+ for (const lineType of lineTypes) {
315
+ if (["line-through", "overline", "underline"].includes(lineType)) {
316
+ css.textDecorationLine.push(lineType);
317
+ }
318
+ }
319
+ break;
320
+ }
321
+ }
322
+ }
323
+ return css;
324
+ };
325
+ const cssToAnsi = (css, previousCss = null) => {
326
+ previousCss = previousCss ?? getDefaultCss();
327
+ let ansi = "";
328
+ if (!colorEquals(css.backgroundColor, previousCss.backgroundColor)) {
329
+ if (css.backgroundColor == void 0) {
330
+ ansi += "\x1B[49m";
331
+ } else {
332
+ switch (css.backgroundColor) {
333
+ case "black": {
334
+ ansi += "\x1B[40m";
335
+ break;
336
+ }
337
+ case "blue": {
338
+ ansi += "\x1B[44m";
339
+ break;
340
+ }
341
+ case "cyan": {
342
+ ansi += "\x1B[46m";
343
+ break;
344
+ }
345
+ case "green": {
346
+ ansi += "\x1B[42m";
347
+ break;
348
+ }
349
+ case "magenta": {
350
+ ansi += "\x1B[45m";
351
+ break;
352
+ }
353
+ case "red": {
354
+ ansi += "\x1B[41m";
355
+ break;
356
+ }
357
+ case "white": {
358
+ ansi += "\x1B[47m";
359
+ break;
360
+ }
361
+ case "yellow": {
362
+ ansi += "\x1B[43m";
363
+ break;
364
+ }
365
+ default: {
366
+ if (Array.isArray(css.backgroundColor)) {
367
+ const { 0: r, 1: g, 2: b } = css.backgroundColor;
368
+ ansi += `\x1B[48;2;${r};${g};${b}m`;
369
+ } else {
370
+ const parsed = parseCssColor(css.backgroundColor);
371
+ if (parsed === null) {
372
+ ansi += "\x1B[49m";
373
+ } else {
374
+ const { 0: r, 1: g, 2: b } = parsed;
375
+ ansi += `\x1B[48;2;${r};${g};${b}m`;
376
+ }
377
+ }
378
+ }
379
+ }
380
+ }
381
+ }
382
+ if (!colorEquals(css.color, previousCss.color)) {
383
+ if (css.color == void 0) {
384
+ ansi += "\x1B[39m";
385
+ } else {
386
+ switch (css.color) {
387
+ case "black": {
388
+ ansi += "\x1B[30m";
389
+ break;
390
+ }
391
+ case "blue": {
392
+ ansi += "\x1B[34m";
393
+ break;
394
+ }
395
+ case "cyan": {
396
+ ansi += "\x1B[36m";
397
+ break;
398
+ }
399
+ case "green": {
400
+ ansi += "\x1B[32m";
401
+ break;
402
+ }
403
+ case "magenta": {
404
+ ansi += "\x1B[35m";
405
+ break;
406
+ }
407
+ case "red": {
408
+ ansi += "\x1B[31m";
409
+ break;
410
+ }
411
+ case "white": {
412
+ ansi += "\x1B[37m";
413
+ break;
414
+ }
415
+ case "yellow": {
416
+ ansi += "\x1B[33m";
417
+ break;
418
+ }
419
+ default: {
420
+ if (Array.isArray(css.color)) {
421
+ const { 0: r, 1: g, 2: b } = css.color;
422
+ ansi += `\x1B[38;2;${r};${g};${b}m`;
423
+ } else {
424
+ const parsed = parseCssColor(css.color);
425
+ if (parsed === null) {
426
+ ansi += "\x1B[39m";
427
+ } else {
428
+ const { 0: r, 1: g, 2: b } = parsed;
429
+ ansi += `\x1B[38;2;${r};${g};${b}m`;
430
+ }
431
+ }
432
+ }
433
+ }
434
+ }
435
+ }
436
+ if (css.fontWeight !== previousCss.fontWeight) {
437
+ ansi += css.fontWeight === "bold" ? "\x1B[1m" : "\x1B[22m";
438
+ }
439
+ if (css.fontStyle !== previousCss.fontStyle) {
440
+ ansi += css.fontStyle === "italic" ? "\x1B[3m" : "\x1B[23m";
441
+ }
442
+ if (!colorEquals(css.textDecorationColor, previousCss.textDecorationColor)) {
443
+ if (css.textDecorationColor == void 0) {
444
+ ansi += "\x1B[59m";
445
+ } else {
446
+ const { 0: r, 1: g, 2: b } = css.textDecorationColor;
447
+ ansi += `\x1B[58;2;${r};${g};${b}m`;
448
+ }
449
+ }
450
+ if (css.textDecorationLine.includes("line-through") !== previousCss.textDecorationLine.includes("line-through")) {
451
+ ansi += css.textDecorationLine.includes("line-through") ? "\x1B[9m" : "\x1B[29m";
452
+ }
453
+ if (css.textDecorationLine.includes("overline") !== previousCss.textDecorationLine.includes("overline")) {
454
+ ansi += css.textDecorationLine.includes("overline") ? "\x1B[53m" : "\x1B[55m";
455
+ }
456
+ if (css.textDecorationLine.includes("underline") !== previousCss.textDecorationLine.includes("underline")) {
457
+ ansi += css.textDecorationLine.includes("underline") ? "\x1B[4m" : "\x1B[24m";
458
+ }
459
+ return ansi;
460
+ };
461
+
462
+ const tryStringify = (o) => {
463
+ try {
464
+ return JSON.stringify(o);
465
+ } catch {
466
+ return '"[Circular]"';
467
+ }
468
+ };
469
+ const CHAR_PERCENT = "%".codePointAt(0);
470
+ const CHAR_s = "s".codePointAt(0);
471
+ const CHAR_d = "d".codePointAt(0);
472
+ const CHAR_f = "f".codePointAt(0);
473
+ const CHAR_i = "i".codePointAt(0);
474
+ const CHAR_O = "O".codePointAt(0);
475
+ const CHAR_o = "o".codePointAt(0);
476
+ const CHAR_j = "j".codePointAt(0);
477
+ const CHAR_c = "c".codePointAt(0);
478
+ const format = (fmt, arguments_ = [], options = {}) => {
479
+ if (typeof fmt !== "string" && typeof fmt !== "object" || fmt === null) {
480
+ throw new TypeError(`fmt must be a string or object, got ${fmt === null ? "null" : typeof fmt}`);
481
+ }
482
+ const stringify = options.stringify ?? tryStringify;
483
+ const offset = 1;
484
+ if (typeof fmt === "object") {
485
+ const argumentsLength = arguments_.length + offset;
486
+ if (argumentsLength === 1) {
487
+ return "{}";
488
+ }
489
+ const objects = Array.from({ length: argumentsLength });
490
+ objects[0] = stringify(fmt);
491
+ for (let index = 1; index < argumentsLength; index++) {
492
+ objects[index] = stringify(arguments_[index - offset]);
493
+ }
494
+ return objects.join(" ");
495
+ }
496
+ if (arguments_.length === 0) {
497
+ return fmt;
498
+ }
499
+ let result = "";
500
+ let a = 1 - offset;
501
+ let lastPosition = -1;
502
+ let usedStyle = false;
503
+ let previousCss = null;
504
+ for (let index = 0; index < fmt.length; ) {
505
+ if (fmt.codePointAt(index) === CHAR_PERCENT && index + 1 < fmt.length) {
506
+ lastPosition = lastPosition > -1 ? lastPosition : 0;
507
+ const c = fmt.codePointAt(index + 1);
508
+ if (c === void 0) {
509
+ ++a;
510
+ break;
511
+ }
512
+ switch (c) {
513
+ case CHAR_c: {
514
+ if (globalThis.window === void 0) {
515
+ const css = parseCss(arguments_[a]);
516
+ if (lastPosition < index) {
517
+ result += fmt.slice(lastPosition, index);
518
+ }
519
+ result += cssToAnsi(css, previousCss);
520
+ if (result !== "") {
521
+ usedStyle = true;
522
+ previousCss = css;
523
+ }
524
+ }
525
+ lastPosition = index + 2;
526
+ index++;
527
+ break;
528
+ }
529
+ case CHAR_d:
530
+ case CHAR_f: {
531
+ if (a >= arguments_.length || arguments_[a] == void 0) {
532
+ break;
533
+ }
534
+ if (lastPosition < index) {
535
+ result += fmt.slice(lastPosition, index);
536
+ }
537
+ result += Number(arguments_[a]).toString();
538
+ lastPosition = index + 2;
539
+ index++;
540
+ break;
541
+ }
542
+ case CHAR_i: {
543
+ if (a >= arguments_.length || arguments_[a] == void 0) {
544
+ break;
545
+ }
546
+ if (lastPosition < index) {
547
+ result += fmt.slice(lastPosition, index);
548
+ }
549
+ result += Math.floor(Number(arguments_[a])).toString();
550
+ lastPosition = index + 2;
551
+ index++;
552
+ break;
553
+ }
554
+ case CHAR_j:
555
+ case CHAR_O:
556
+ case CHAR_o: {
557
+ if (a >= arguments_.length || arguments_[a] === void 0) {
558
+ break;
559
+ }
560
+ if (lastPosition < index) {
561
+ result += fmt.slice(lastPosition, index);
562
+ }
563
+ const temporaryArgument = arguments_[a];
564
+ const type = typeof temporaryArgument;
565
+ if (type === "string") {
566
+ result += `'${temporaryArgument}'`;
567
+ lastPosition = index + 2;
568
+ break;
569
+ }
570
+ if (type === "function") {
571
+ result += temporaryArgument.name ? `[Function: ${temporaryArgument.name}]` : "[Function: <anonymous>]";
572
+ lastPosition = index + 2;
573
+ break;
574
+ }
575
+ result += stringify(temporaryArgument);
576
+ lastPosition = index + 2;
577
+ index++;
578
+ break;
579
+ }
580
+ case CHAR_PERCENT: {
581
+ if (lastPosition < index) {
582
+ result += fmt.slice(lastPosition, index);
583
+ }
584
+ result += "%";
585
+ lastPosition = index + 2;
586
+ index++;
587
+ a--;
588
+ break;
589
+ }
590
+ case CHAR_s: {
591
+ if (a >= arguments_.length) {
592
+ break;
593
+ }
594
+ if (lastPosition < index) {
595
+ result += fmt.slice(lastPosition, index);
596
+ }
597
+ result += typeof arguments_[a] === "object" ? stringify(arguments_[a]) : String(arguments_[a]);
598
+ lastPosition = index + 2;
599
+ index++;
600
+ break;
601
+ }
602
+ default: {
603
+ if (typeof options.formatters?.[c] === "function") {
604
+ if (lastPosition < index) {
605
+ result += fmt.slice(lastPosition, index);
606
+ }
607
+ result += options.formatters[c](arguments_[a]);
608
+ lastPosition = index + 2;
609
+ index++;
610
+ }
611
+ }
612
+ }
613
+ ++a;
614
+ }
615
+ ++index;
616
+ }
617
+ if (lastPosition === -1) {
618
+ return fmt;
619
+ }
620
+ if (lastPosition < fmt.length) {
621
+ result += fmt.slice(lastPosition);
622
+ }
623
+ if (usedStyle) {
624
+ result += "\x1B[0m";
625
+ }
626
+ return result;
627
+ };
628
+ const build = (options = {}) => {
629
+ const formatters = {};
630
+ if (typeof options.formatters === "object") {
631
+ Object.entries(options.formatters).forEach(([key, formatterFunction]) => {
632
+ if (key.length === 0) {
633
+ throw new Error(`Formatter %${key} has no characters`);
634
+ }
635
+ if (key.length > 1) {
636
+ throw new Error(`Formatter %${key} has more than one character`);
637
+ }
638
+ if (typeof formatterFunction !== "function") {
639
+ throw new TypeError(`Formatter for %${key} is not a function`);
640
+ }
641
+ const c = key.codePointAt(0);
642
+ if (c === void 0) {
643
+ throw new Error(`${key}.codePointAt(0) failed to return a value, please report this issue`);
644
+ }
645
+ formatters[c] = formatterFunction;
646
+ });
647
+ }
648
+ return (f, arguments_ = [], formatOptions = {}) => format(f, arguments_, { ...formatOptions, formatters });
649
+ };
650
+
651
+ export { build, format };
@@ -0,0 +1,12 @@
1
+ type CssObject = {
2
+ __proto__: null;
3
+ backgroundColor: string | null;
4
+ color: string | null;
5
+ fontStyle: string | null;
6
+ fontWeight: string | null;
7
+ textDecorationColor: [number, number, number] | null;
8
+ textDecorationLine: string[];
9
+ };
10
+ export declare const parseCss: (cssString: string) => CssObject;
11
+ export declare const cssToAnsi: (css: CssObject, previousCss?: CssObject | null) => string;
12
+ export {};