11import type { AnsiColor } from "@ansi-escape-code/type" ;
22
3+ /**
4+ * Union of parts that can make up an ANSI string. A part can be another
5+ * {@link Ansi} instance or any object with a `toString()` method.
6+ */
37export type AnsiPart = Ansi | { toString ( ) : string } ;
48
9+ /** Options describing how text should be styled. */
510export interface AnsiOptions {
611 weight : "normal" | "bold" | "dim" ;
712 italic : boolean ;
@@ -15,11 +20,20 @@ export interface AnsiOptions {
1520 backgroundColor : AnsiColor ;
1621}
1722
23+ /**
24+ * Template tag used to produce nested {@link Ansi} instances.
25+ *
26+ * @param strings - Raw template string segments.
27+ * @param values - Interpolated values to embed.
28+ */
1829export type AnsiTemplateTag = (
1930 strings : TemplateStringsArray ,
2031 ...values : readonly AnsiPart [ ]
2132) => Ansi ;
2233
34+ /**
35+ * Represents a styled string that can contain nested {@link Ansi} parts.
36+ */
2337export class Ansi {
2438 public static readonly defaultOptions : AnsiOptions = {
2539 weight : "normal" ,
@@ -36,13 +50,24 @@ export class Ansi {
3650
3751 public readonly parts : AnsiPart [ ] ;
3852
53+ /**
54+ * Creates a new instance.
55+ *
56+ * @param options - Styling options to apply to the contents.
57+ * @param parts - Parts that make up the styled output.
58+ */
3959 constructor (
4060 public readonly options : Partial < AnsiOptions > ,
4161 ...parts : AnsiPart [ ]
4262 ) {
4363 this . parts = parts ;
4464 }
4565
66+ /**
67+ * Renders this instance and all nested parts to a string.
68+ *
69+ * @param resolvedOuterOptions - Active options of the parent context.
70+ */
4671 public toString (
4772 resolvedOuterOptions : AnsiOptions = Ansi . defaultOptions
4873 ) : string {
@@ -236,10 +261,20 @@ export class Ansi {
236261 return [ 5 , 16 + r * 36 + g * 6 + b ] ;
237262 }
238263
264+ /**
265+ * Creates a 24 bit true color value.
266+ *
267+ * @param r - Red component (0-255)
268+ * @param g - Green component (0-255)
269+ * @param b - Blue component (0-255)
270+ */
239271 public static TRUE_COLOR ( r : number , g : number , b : number ) : AnsiColor {
240272 return [ 2 , r , g , b ] ;
241273 }
242274
275+ /**
276+ * Returns a template tag preconfigured with the provided options.
277+ */
243278 public static tt ( oprions : Partial < AnsiOptions > ) : AnsiTemplateTag {
244279 const options = ( oprions ?? { } ) as Partial < AnsiOptions > ;
245280 return ( strings : TemplateStringsArray , ...values : AnsiPart [ ] ) => {
@@ -249,6 +284,9 @@ export class Ansi {
249284 }
250285}
251286
287+ /**
288+ * Interleaves template string segments with their interpolated values.
289+ */
252290function interleave (
253291 strings : TemplateStringsArray ,
254292 values : readonly AnsiPart [ ]
0 commit comments