Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
run biome
  • Loading branch information
GauBen committed Feb 26, 2026
commit d5d993c4e875056e2125f6132f2cdec9928af482
17 changes: 6 additions & 11 deletions packages/color/src/index.ts
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice! The styleText API is not great, so having a drop-in replacement for picocolors is a good thing.

Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { type InspectColor, styleText } from "node:util";
import { type InspectColor, styleText } from 'node:util';

export type StyleText = ((
text: string | TemplateStringsArray,
...args: unknown[]
) => string) & { [key in InspectColor]: StyleText };
export type StyleText = ((text: string | TemplateStringsArray, ...args: unknown[]) => string) & {
[key in InspectColor]: StyleText;
};

export function createStyleText(...styles: InspectColor[]): StyleText {
return new Proxy(
(text: string | TemplateStringsArray, ...args: unknown[]) =>
styleText(
styles,
typeof text === "string" ? text : String.raw({ raw: text }, ...args)
),
styleText(styles, typeof text === 'string' ? text : String.raw({ raw: text }, ...args)),
{
get(_, prop) {
if (typeof prop !== "string")
throw new TypeError("Property must be a string");
if (typeof prop !== 'string') throw new TypeError('Property must be a string');
return createStyleText(...styles, prop as InspectColor);
},
}
Expand Down
22 changes: 10 additions & 12 deletions packages/color/test/color.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import c, { createStyleText } from "@clack/color";
import { styleText } from "node:util";
import { test, expect } from "vitest";
import c, { createStyleText } from '@clack/color';

Check failure on line 1 in packages/color/test/color.test.ts

View workflow job for this annotation

GitHub Actions / scripts / run (types)

Cannot find module '@clack/color' or its corresponding type declarations.
import { styleText } from 'node:util';
import { test, expect } from 'vitest';

process.env.FORCE_COLOR = "1";
process.env.FORCE_COLOR = '1';

test("@clack/color", () => {
expect(c.redBright.underline("Hello World")).toBe(
styleText(["redBright", "underline"], "Hello World")
test('@clack/color', () => {
expect(c.redBright.underline('Hello World')).toBe(
styleText(['redBright', 'underline'], 'Hello World')
);

expect(c.bgCyan.bold`Hello World`).toBe(
styleText(["bgCyan", "bold"], "Hello World")
);
expect(c.bgCyan.bold`Hello World`).toBe(styleText(['bgCyan', 'bold'], 'Hello World'));

expect(createStyleText("red", "inverse")(`Hello World`)).toBe(
styleText(["red", "inverse"], "Hello World")
expect(createStyleText('red', 'inverse')(`Hello World`)).toBe(
styleText(['red', 'inverse'], 'Hello World')
);
});
Loading