Skip to content

Commit

Permalink
feat: make output of @crayon/literal the same as @crayon/crayon
Browse files Browse the repository at this point in the history
  • Loading branch information
Im-Beast committed Aug 17, 2024
1 parent 71021ef commit 66179f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
19 changes: 15 additions & 4 deletions main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2024 Im-Beast. All rights reserved. MIT license.
import { env, test } from "@beast/compat";
import { assert, assertEquals, assertThrows } from "@std/assert";
import { assertEquals, assertThrows } from "@std/assert";

import crayon from "@crayon/crayon";
import "./main.ts";
Expand All @@ -12,10 +12,15 @@ test.ignoreIf(!NO_COLOR)("NO_COLOR Literal", () => {
});

test.ignoreIf(NO_COLOR)("Literal", () => {
assert(crayon`{red {bold Hello} world!}`.endsWith("\x1b[0m"));
assertEquals(
crayon`{red {bold Hello} world!}`,
"\x1b[31m\x1b[1mHello\x1b[0m\x1b[31m world!\x1b[0m",
crayon.red(`${crayon.bold("Hello")} world!`),
"Output of @crayon/literal differs from @crayon/crayon",
);

assertEquals(
crayon`{red {bold Hello} world!}`,
"\x1b[31m\x1b[1mHello\x1b[0m\x1b[31m world!\x1b[0m\x1b[0m",
);

const styledText = "{rgb(255,0,17) methods {bgRgb(127,255,0) work!}}";
Expand All @@ -26,7 +31,13 @@ test.ignoreIf(NO_COLOR)("Literal", () => {

assertEquals(
crayon`{rgb(255,0,17) methods {bgRgb(127,255,0) work!}}`,
"\x1b[38;2;255;0;17mmethods \x1b[48;2;127;255;0mwork!\x1b[0m\x1b[38;2;255;0;17m\x1b[0m",
crayon.rgb(255, 0, 17)(`methods ${crayon.bgRgb(127, 255, 0)("work!")}`),
"Output of @crayon/literal differs from @crayon/crayon",
);

assertEquals(
crayon`{rgb(255,0,17) methods {bgRgb(127,255,0) work!}}`,
"\x1b[38;2;255;0;17mmethods \x1b[48;2;127;255;0mwork!\x1b[0m\x1b[38;2;255;0;17m\x1b[0m\x1b[0m",
);
});

Expand Down
4 changes: 2 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export function literal(
styleBuffer += parsedStyle;
}

const matchedText = replaceAll(body, "\x1b[0m", "\x1b[0m" + styleBuffer);
text = replace(text, section, styleBuffer + matchedText + "\x1b[0m");
const matchedText = replaceAll(body, "\x1b[0m\x1b[0m", "\x1b[0m" + styleBuffer);
text = replace(text, section, styleBuffer + matchedText + "\x1b[0m\x1b[0m");

matches = text.match(literalStyleRegex);
}
Expand Down

0 comments on commit 66179f8

Please sign in to comment.