saghul / txiki.js

A tiny JavaScript runtime
MIT License
2.47k stars 165 forks source link

feat: support console.log with colors #609

Open ahaoboy opened 1 month ago

ahaoboy commented 1 month ago

Will it support colored console.log output? Using different colors for different types will make it easier to distinguish

https://github.com/ahaoboy/deno-ext/tree/main/deno-console

If someone wants to use a styled console, they can use the following code to override the original implementation

import { Console } from '@deno-ext/console'
const encoder = new TextEncoder();
const print = s => tjs.stdout.write(encoder.encode(s));
globalThis.console = new Console(print)

console.log(1)
saghul commented 1 month ago

Fun fact! There should be color support in there already, since we took the inspect util from Node: https://github.com/saghul/txiki.js/blob/84267a1612e47ec655fdf2b4afa1ff826d6b08b1/src/js/polyfills/console-util.js#L116

It's just not enabled by default.

ahaoboy commented 1 month ago

console.log is a very cumbersome thing, e.g. in some scenarios it may be better to output part of the information, otherwise it will be cluttered with a lot of useless information

> new Uint8Array(1000)
Uint8Array(1000) [
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0,
  ... 900 more items
]
saghul commented 1 month ago

Agreed. Note that the above is the REPL, which doesn't use console.log for its output, even though they both end up sending their output to tjs.stdout.