@heinrichb/console-toolkit 1.0.9 → 1.0.10
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/core/builders.d.ts +25 -0
- package/dist/core/utils.d.ts +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +39 -27
- package/dist/index.min.js +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PrintSegment, PrintLine, PrintBlock, PrintStyle } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a PrintSegment object.
|
|
4
|
+
*
|
|
5
|
+
* @param text - The text content of the segment.
|
|
6
|
+
* @param style - The optional style to apply to the text.
|
|
7
|
+
* @returns A PrintSegment object.
|
|
8
|
+
*/
|
|
9
|
+
export declare function segment(text: string, style?: PrintStyle): PrintSegment;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a PrintLine object.
|
|
12
|
+
*
|
|
13
|
+
* @param segments - An optional array of PrintSegments that make up the line. Defaults to [].
|
|
14
|
+
* @param style - The optional style to apply to the entire line.
|
|
15
|
+
* @returns A PrintLine object.
|
|
16
|
+
*/
|
|
17
|
+
export declare function line(segments?: PrintSegment[], style?: PrintStyle): PrintLine;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a PrintBlock object.
|
|
20
|
+
*
|
|
21
|
+
* @param lines - An optional array of PrintLines that make up the block. Defaults to [].
|
|
22
|
+
* @param style - The optional style to apply to the entire block.
|
|
23
|
+
* @returns A PrintBlock object.
|
|
24
|
+
*/
|
|
25
|
+
export declare function block(lines?: PrintLine[], style?: PrintStyle): PrintBlock;
|
package/dist/core/utils.d.ts
CHANGED
|
@@ -17,9 +17,9 @@ export declare function computeMaxWidth(lines: PrintLine[]): number;
|
|
|
17
17
|
/**
|
|
18
18
|
* Pads a PrintLine to a target width by adding an empty segment at the end.
|
|
19
19
|
*
|
|
20
|
-
* @param
|
|
20
|
+
* @param inputLine - The line to pad.
|
|
21
21
|
* @param targetWidth - The desired minimum width.
|
|
22
22
|
* @param padStyle - The style to apply to the padding spaces.
|
|
23
23
|
* @returns A new PrintLine with padding added if necessary.
|
|
24
24
|
*/
|
|
25
|
-
export declare function padLine(
|
|
25
|
+
export declare function padLine(inputLine: PrintLine, targetWidth: number, padStyle?: PrintStyle): PrintLine;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -117,21 +117,30 @@ function resolveStyle(style, gradientFactor = 0) {
|
|
|
117
117
|
}
|
|
118
118
|
return ansi;
|
|
119
119
|
}
|
|
120
|
+
// src/core/builders.ts
|
|
121
|
+
function segment(text, style) {
|
|
122
|
+
return { text, style };
|
|
123
|
+
}
|
|
124
|
+
function line(segments = [], style) {
|
|
125
|
+
return { segments, style };
|
|
126
|
+
}
|
|
127
|
+
function block(lines = [], style) {
|
|
128
|
+
return { lines, style };
|
|
129
|
+
}
|
|
130
|
+
|
|
120
131
|
// src/core/utils.ts
|
|
121
|
-
function getLineLength(
|
|
122
|
-
return
|
|
132
|
+
function getLineLength(line2) {
|
|
133
|
+
return line2.segments.reduce((acc, seg) => acc + seg.text.length, 0);
|
|
123
134
|
}
|
|
124
135
|
function computeMaxWidth(lines) {
|
|
125
136
|
return lines.length > 0 ? Math.max(...lines.map(getLineLength)) : 0;
|
|
126
137
|
}
|
|
127
|
-
function padLine(
|
|
128
|
-
const currentLength = getLineLength(
|
|
138
|
+
function padLine(inputLine, targetWidth, padStyle) {
|
|
139
|
+
const currentLength = getLineLength(inputLine);
|
|
129
140
|
if (currentLength < targetWidth) {
|
|
130
|
-
return
|
|
131
|
-
segments: [...line.segments, { text: " ".repeat(targetWidth - currentLength), style: padStyle }]
|
|
132
|
-
};
|
|
141
|
+
return line([...inputLine.segments, segment(" ".repeat(targetWidth - currentLength), padStyle)], inputLine.style);
|
|
133
142
|
}
|
|
134
|
-
return
|
|
143
|
+
return inputLine;
|
|
135
144
|
}
|
|
136
145
|
// src/core/printer.ts
|
|
137
146
|
var ESC2 = "\x1B";
|
|
@@ -165,8 +174,8 @@ class Printer {
|
|
|
165
174
|
let output = this.getClearSequence();
|
|
166
175
|
const lines = this.data.lines;
|
|
167
176
|
const blockStyle = this.data.style ?? {};
|
|
168
|
-
lines.forEach((
|
|
169
|
-
output += this.renderLine(
|
|
177
|
+
lines.forEach((line2, lineIndex) => {
|
|
178
|
+
output += this.renderLine(line2, lineIndex, lines.length, blockStyle);
|
|
170
179
|
output += `
|
|
171
180
|
`;
|
|
172
181
|
});
|
|
@@ -191,17 +200,17 @@ class Printer {
|
|
|
191
200
|
}
|
|
192
201
|
return blockStyle.color;
|
|
193
202
|
}
|
|
194
|
-
renderLine(
|
|
203
|
+
renderLine(line2, lineIndex, totalLines, parentBlockStyle) {
|
|
195
204
|
const blockColorForLine = this.resolveBlockColorForLine(parentBlockStyle, lineIndex, totalLines);
|
|
196
205
|
const baseLineStyle = {
|
|
197
206
|
modifiers: parentBlockStyle.modifiers,
|
|
198
207
|
color: blockColorForLine
|
|
199
208
|
};
|
|
200
|
-
const effectiveLineStyle = mergeStyles(baseLineStyle,
|
|
201
|
-
const totalChars =
|
|
209
|
+
const effectiveLineStyle = mergeStyles(baseLineStyle, line2.style);
|
|
210
|
+
const totalChars = line2.segments.reduce((acc, seg) => acc + seg.text.length, 0);
|
|
202
211
|
let currentCharIndex = 0;
|
|
203
212
|
let lineOutput = "";
|
|
204
|
-
|
|
213
|
+
line2.segments.forEach((seg) => {
|
|
205
214
|
const effectiveSegmentStyle = mergeStyles(effectiveLineStyle, seg.style);
|
|
206
215
|
if (Array.isArray(effectiveSegmentStyle.color)) {
|
|
207
216
|
const colors = effectiveSegmentStyle.color;
|
|
@@ -242,22 +251,22 @@ function mergeColumns(columns, separator = " ", defaultStyle, widths) {
|
|
|
242
251
|
for (let i = 0;i < maxLines; i++) {
|
|
243
252
|
let segments = [];
|
|
244
253
|
for (let j = 0;j < columns.length; j++) {
|
|
245
|
-
const
|
|
254
|
+
const currentLine = columns[j][i] || line();
|
|
246
255
|
if (j < columns.length - 1) {
|
|
247
|
-
const padded = padLine(
|
|
248
|
-
segments = [...segments, ...padded.segments,
|
|
256
|
+
const padded = padLine(currentLine, colWidths[j], defaultStyle);
|
|
257
|
+
segments = [...segments, ...padded.segments, segment(separator, defaultStyle)];
|
|
249
258
|
} else {
|
|
250
|
-
segments = [...segments, ...
|
|
259
|
+
segments = [...segments, ...currentLine.segments];
|
|
251
260
|
}
|
|
252
261
|
}
|
|
253
|
-
output.push(
|
|
262
|
+
output.push(line(segments));
|
|
254
263
|
}
|
|
255
264
|
return output;
|
|
256
265
|
}
|
|
257
266
|
function printColumns(columns, options = {}) {
|
|
258
267
|
const { widths, separator = " ", printer = new Printer } = options;
|
|
259
268
|
const mergedLines = mergeColumns(columns, separator, undefined, widths);
|
|
260
|
-
printer.print(
|
|
269
|
+
printer.print(block(mergedLines));
|
|
261
270
|
}
|
|
262
271
|
// src/components/progress.ts
|
|
263
272
|
function createProgressBar(options) {
|
|
@@ -291,22 +300,22 @@ function createProgressBar(options) {
|
|
|
291
300
|
const resolvedPercentageStyle = percentageStyle ?? style;
|
|
292
301
|
const segments = [];
|
|
293
302
|
if (startChar) {
|
|
294
|
-
segments.push(
|
|
303
|
+
segments.push(segment(startChar, resolvedStartStyle));
|
|
295
304
|
}
|
|
296
305
|
if (filledWidth > 0) {
|
|
297
|
-
segments.push(
|
|
306
|
+
segments.push(segment(fillChar.repeat(filledWidth), resolvedFillStyle));
|
|
298
307
|
}
|
|
299
308
|
if (emptyWidth > 0) {
|
|
300
|
-
segments.push(
|
|
309
|
+
segments.push(segment(emptyChar.repeat(emptyWidth), resolvedEmptyStyle));
|
|
301
310
|
}
|
|
302
311
|
if (endChar) {
|
|
303
|
-
segments.push(
|
|
312
|
+
segments.push(segment(endChar, resolvedEndStyle));
|
|
304
313
|
}
|
|
305
314
|
if (showPercentage) {
|
|
306
315
|
const percentageText = formatPercentage ? formatPercentage(p) : ` ${Math.round(p * 100)}%`;
|
|
307
|
-
segments.push(
|
|
316
|
+
segments.push(segment(percentageText, resolvedPercentageStyle));
|
|
308
317
|
}
|
|
309
|
-
return
|
|
318
|
+
return line(segments);
|
|
310
319
|
}
|
|
311
320
|
// src/components/spinner.ts
|
|
312
321
|
var SPINNERS = {
|
|
@@ -357,10 +366,11 @@ function getDragon(startColor = "#EF4444", endColor = "#F59E0B") {
|
|
|
357
366
|
return rawDragon.map((text, i) => {
|
|
358
367
|
const factor = rawDragon.length <= 1 ? 0 : i / (rawDragon.length - 1);
|
|
359
368
|
const colorStyle = interpolateColor(startColor, endColor, factor);
|
|
360
|
-
return
|
|
369
|
+
return line([segment(text, { color: colorStyle })]);
|
|
361
370
|
});
|
|
362
371
|
}
|
|
363
372
|
export {
|
|
373
|
+
segment,
|
|
364
374
|
rgbToAnsi,
|
|
365
375
|
resolveStyle,
|
|
366
376
|
resolveModifiersToAnsi,
|
|
@@ -369,6 +379,7 @@ export {
|
|
|
369
379
|
padLine,
|
|
370
380
|
mergeStyles,
|
|
371
381
|
mergeColumns,
|
|
382
|
+
line,
|
|
372
383
|
interpolateHex,
|
|
373
384
|
interpolateColor,
|
|
374
385
|
hexToRgb,
|
|
@@ -378,6 +389,7 @@ export {
|
|
|
378
389
|
createProgressBar,
|
|
379
390
|
computeMaxWidth,
|
|
380
391
|
colorToHex,
|
|
392
|
+
block,
|
|
381
393
|
Spinner,
|
|
382
394
|
SPINNERS,
|
|
383
395
|
RESET,
|
package/dist/index.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
2
|
-
`}),process.stdout.write(z),this.linesRendered=J.length}resolveBlockColorForLine(q,z,J){if(!q.color)return;if(Array.isArray(q.color)){if(J<=1)return q.color[0];let K=q.color,Q=z/(J-1),U=Math.max(0,Math.min(1,Q)),Y=1/(K.length-1),V=Math.min(Math.floor(U/Y),K.length-2),X=(U-V*Y)/Y,Z=K[V],$=K[V+1];return
|
|
1
|
+
var T="\x1B",I=`${T}[0m`,s={black:"#000000",red:"#EF4444",green:"#10B981",yellow:"#F59E0B",blue:"#3B82F6",magenta:"#EC4899",cyan:"#06B6D4",white:"#FFFFFF",gray:"#6B7280",grey:"#6B7280"},n={default:"0",bold:"1",dim:"2",italic:"3",underline:"4",inverse:"7",hidden:"8",strikethrough:"9"};function A(q){if(q.startsWith("#"))return q;let z=s[q.toLowerCase()];if(!z)return"#FFFFFF";return z}function P(q){let z=q.replace(/^#/,"");if(z.length!==6)return{r:255,g:255,b:255};return{r:parseInt(z.substring(0,2),16),g:parseInt(z.substring(2,4),16),b:parseInt(z.substring(4,6),16)}}function o(q,z,J){return`${T}[38;2;${q};${z};${J}m`}function w(q){let z=A(q),{r:J,g:K,b:Q}=P(z);return o(J,K,Q)}function v(q){if(!q||q.length===0)return"";return q.map((z)=>{let J=n[z];return J?`${T}[${J}m`:""}).join("")}function E(q){let z=Math.max(0,Math.min(255,Math.round(q))).toString(16);return z.length===1?"0"+z:z}function l(q,z,J){let K=Math.max(0,Math.min(1,J)),Q=P(q),U=P(z),Y=Q.r+K*(U.r-Q.r),V=Q.g+K*(U.g-Q.g),X=Q.b+K*(U.b-Q.b);return`#${E(Y)}${E(V)}${E(X)}`}function R(q,z,J){return l(A(q),A(z),J)}function k(q,z){if(q.length===0)return"";if(q.length===1)return w(q[0]);let J=Math.max(0,Math.min(1,z)),K=1/(q.length-1),Q=Math.min(Math.floor(J/K),q.length-2),U=(J-Q*K)/K,Y=q[Q],V=q[Q+1],X=R(Y,V,U);return w(X)}function x(q,z){if(!q&&!z)return{};if(!q)return z??{};if(!z)return q;return{modifiers:Array.from(new Set([...q.modifiers??[],...z.modifiers??[]])),color:z.color??q.color}}function S(q,z=0){if(!q)return"";let J="";if(q.modifiers)J+=v(q.modifiers);if(q.color)if(Array.isArray(q.color)){let K=k(q.color,z);J+=K}else J+=w(q.color);return J}function N(q,z){return{text:q,style:z}}function M(q=[],z){return{segments:q,style:z}}function u(q=[],z){return{lines:q,style:z}}function h(q){return q.segments.reduce((z,J)=>z+J.text.length,0)}function m(q){return q.length>0?Math.max(...q.map(h)):0}function y(q,z,J){let K=h(q);if(K<z)return M([...q.segments,N(" ".repeat(z-K),J)],q.style);return q}var p="\x1B";class b{linesRendered=0;isLive;data;constructor(q={}){this.isLive=q.live??!1,this.data=q.data}getClearSequence(){if(!this.isLive||this.linesRendered===0)return"";return`${p}[1A${p}[2K\r`.repeat(this.linesRendered)}clear(){if(this.linesRendered>0)process.stdout.write(this.getClearSequence()),this.linesRendered=0}print(q){if(q)this.data=q;if(!this.data)return;let z=this.getClearSequence(),J=this.data.lines,K=this.data.style??{};J.forEach((Q,U)=>{z+=this.renderLine(Q,U,J.length,K),z+=`
|
|
2
|
+
`}),process.stdout.write(z),this.linesRendered=J.length}resolveBlockColorForLine(q,z,J){if(!q.color)return;if(Array.isArray(q.color)){if(J<=1)return q.color[0];let K=q.color,Q=z/(J-1),U=Math.max(0,Math.min(1,Q)),Y=1/(K.length-1),V=Math.min(Math.floor(U/Y),K.length-2),X=(U-V*Y)/Y,Z=K[V],$=K[V+1];return R(Z,$,X)}return q.color}renderLine(q,z,J,K){let Q=this.resolveBlockColorForLine(K,z,J),U={modifiers:K.modifiers,color:Q},Y=x(U,q.style),V=q.segments.reduce(($,_)=>$+_.text.length,0),X=0,Z="";return q.segments.forEach(($)=>{let _=x(Y,$.style);if(Array.isArray(_.color)){let F=_.color,H=$.text,W=_.color===Y.color,B=v(_.modifiers);for(let G=0;G<H.length;G++){let j=0;if(W&&V>1)j=(X+G)/(V-1);else if(!W&&H.length>1)j=G/(H.length-1);let D=k(F,j);Z+=`${B}${D}${H[G]}`}Z+=I}else{let F=S(_);Z+=`${F}${$.text}${I}`}X+=$.text.length}),Z}}function t(q,z=" ",J,K){if(q.length===0)return[];let Q=Math.max(...q.map((V)=>V.length)),U=q.map((V,X)=>{if(K?.[X]!==void 0)return K[X];return m(V)}),Y=[];for(let V=0;V<Q;V++){let X=[];for(let Z=0;Z<q.length;Z++){let $=q[Z][V]||M();if(Z<q.length-1){let _=y($,U[Z],J);X=[...X,..._.segments,N(z,J)]}else X=[...X,...$.segments]}Y.push(M(X))}return Y}function Zq(q,z={}){let{widths:J,separator:K=" ",printer:Q=new b}=z,U=t(q,K,void 0,J);Q.print(u(U))}function Nq(q){let{progress:z,width:J=20,style:K,bracketStyle:Q,startStyle:U,endStyle:Y,barStyle:V,fillStyle:X,emptyStyle:Z,percentageStyle:$,startChar:_="[",endChar:F="]",fillChar:H="█",emptyChar:W="░",showPercentage:B=!0,formatPercentage:G}=q,j=Math.max(0,Math.min(1,z)),D=Math.round(j*J),C=J-D,L=Q??K,g=U??L,d=Y??L,f=V??K,c=X??f,a=Z??f,i=$??K,O=[];if(_)O.push(N(_,g));if(D>0)O.push(N(H.repeat(D),c));if(C>0)O.push(N(W.repeat(C),a));if(F)O.push(N(F,d));if(B){let r=G?G(j):` ${Math.round(j*100)}%`;O.push(N(r,i))}return M(O)}var Gq={dots:["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"],lines:["-","\\","|","/"],arrows:["←","↖","↑","↗","→","↘","↓","↙"],circle:["◐","◓","◑","◒"],square:["▖","▘","▝","▗"]};class e{frames;interval;startTime;constructor(q){this.frames=q.frames,this.interval=q.interval??80,this.startTime=Date.now()}getFrame(){let z=Date.now()-this.startTime,J=Math.floor(z/this.interval)%this.frames.length;return this.frames[J]}}function Oq(q="#EF4444",z="#F59E0B"){let J=[" ^ ^"," / \\ //\\"," |\\___/| / \\// .\\"," /O O \\__ / // | \\ \\","/ / \\_/_/ // | \\ \\","@___@' \\_// // | \\ \\ "," | \\_// // | \\ \\ "," | \\/// | \\ \\ "," _|_ / ) // | \\ _\\"," '/,_ _ _/ ( ; -. | _ _\\.-~ .-~~~^-."," ,-{ _ `-.|.-~-. .~ `."," '/\\ / ~-. _ .-~ .-~^-. \\"," `. { } / \\ \\"," .----~-\\. \\-' .~ \\ `. \\^-."," ///.----..> c \\ _ -~ `. ^-` ^-_"," ///-._ _ _ _ _ _ _}^ - - - - ~ ~--, .-~"," /.-'"];return J.map((K,Q)=>{let U=J.length<=1?0:Q/(J.length-1),Y=R(q,z,U);return M([N(K,{color:Y})])})}export{N as segment,o as rgbToAnsi,S as resolveStyle,v as resolveModifiersToAnsi,w as resolveColorToAnsi,Zq as printColumns,y as padLine,x as mergeStyles,t as mergeColumns,M as line,l as interpolateHex,R as interpolateColor,P as hexToRgb,h as getLineLength,k as getGradientColor,Oq as getDragon,Nq as createProgressBar,m as computeMaxWidth,A as colorToHex,u as block,e as Spinner,Gq as SPINNERS,I as RESET,b as Printer};
|
package/package.json
CHANGED