Skip to content

Support CSS-based %c coloring? #1

@jcbhmr

Description

@jcbhmr

I'd be interested in contributing code to do this

To enable more cross-platform stuff (Firefox, Safari, etc.) it would be nice to treat Node.js as the "special case" due to Node.js' lack of support for %c and treat console.log("%cHi", "color:red") as the "most correct" way of doing things.

For instance, the provided example code in the README.md doesn't work in Firefox due to it using ANSI which only works on TTYs and Chrome-related browsers.

My estimated support table of ANSI vs %c-based colors:

Support ANSI colors Support %c specifier
Node.js 🟨 nodejs/node#29605
Deno
Chrome
Firefox
Safari unknown*
Opera

* I don't have an Apple device to test this on. Apple doesn't release Safari for Windows 😭

Since this package doesn't have any dependents on npm (and 0 DLs/week) I think it'd be very non-breaking to revamp the API surface for a v2 or whatever without angering any users.

image

Some ideas for how it might look as a user:

import { c, log, debug, info, error, warn } from "tinyrainbow"

// This returns an array to spread over multiple console.log() args
c`<green>SUCCESS</> <i>${message} <gray>${new Date()}</gray></i>`
//=> ["%cSUCCESS%c %c%s %c%s%c%c", "color:green", "",
//    "font-style:italic", message, "font-style:italic;color:gray",
//    new Date(), "font-style:italic", ""]

// and might be used like this?
console.log(...c`<green>SUCCESS</> <i>${message} <gray>${new Date()}</gray></i>`)

// or maybe a shortcut for every console.* method?
log`<red>UH OH</red> something went <i>wrong</>`
debug`Hello <b>WORLD</b>`
warn`<yellow>Oh no</yellow> <i>anyway...</i>`
error`<b><red>CATASROPHIC FAILURE`

image
image
image

Keep in mind that this lets you do WAYYYY more than just color the text! You can make it bigger, bolder, center it, add background color, and even add images! Obviously some properties like background-image are only supported in browsers and not in Deno 🤣

image
https://www.bennadel.com/blog/3941-styling-console-log-output-formatting-with-css.htm

This would also help this library establish a niche that isn't already claimed by another library! For the Google search "npm console colors" there's already like 10 different libraries that do ANSI codes, but all of the popular ones seem to fall flat in Firefox since they rely on ANSI codes.

I think this is a great time to pivot and make a name/identity as the library for cross-platform log coloration
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions