txiki.js icon indicating copy to clipboard operation
txiki.js copied to clipboard

feat: support console.log with colors

Open ahaoboy opened this issue 1 year ago • 3 comments

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)

ahaoboy avatar Jul 16 '24 06:07 ahaoboy

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.

saghul avatar Jul 16 '24 06:07 saghul

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
]

ahaoboy avatar Jul 16 '24 07:07 ahaoboy

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.

saghul avatar Jul 16 '24 07:07 saghul