Xterm.js: How to use with "colors" lib?

Created on 18 Aug 2017  路  20Comments  路  Source: xtermjs/xterm.js

https://github.com/Marak/colors.js
https://www.npmjs.com/package/colors
https://www.npmjs.com/package/chalk

Is there anything preventing this from working with the lib mentioned above? It should just be dealing with the escape sequences correct?

I'm having an issue getting it to work, which is not to say it's an issue with xterm.js just that I am not aware of how to make it work or is an alternate color lib would be better suited.

Most helpful comment

@Tyriar ok I figured it out..

Chalk is doing some auto detection to see what it should support, using this lib: https://github.com/chalk/supports-color

Long story short, chalk thinks nothing is supported color wise, so it doesn't process the strings.

let options: any = {enabled: true, level: 2};
const forcedChalk = new chalk.constructor(options);

^ fixes it in the browser

where Levels are:

0: All colors disabled
1: Basic color support (16 colors)
2: 256 color support
3: Truecolor support (16 million colors)

All 20 comments

How are you using it? Are you generating a coloured string and then directly write it to the xterm.js instance?

Yes, for example, I just tried it with chalk (https://www.npmjs.com/package/chalk)

terminal.writeln(chalk.blue('Hello') + ' World' + chalk.red('!'));

I also tried it with colors (https://www.npmjs.com/package/colors)

terminal.writeln(colors.green('Hello World'));

and the output is black and white in both cases

I would expect this to work, as long as it generates the correct ansi escape codes for the color. How does the output from xterm look like? An uncoloured string? Can you send a screenshot?

image

Works for me:

image

image

image

Are you on windows? If so what shell are you using?

Oh wait, Windows shouldn't matter here. You wouldn't even need a shell hooked up for this to work.

I'm not sure why it's not working for you. Any errors in the console?

Ya from the node repl it works, this is in the browser which I think might be the issue.

I think @Tyriar was writing in the chrome developer console of the xterm.js demo (see the second screenshot, term.writeln...). Could you try with the same string he was using and see if it works for you?

@Tyriar Do you think this would also work if the terminal is vanilla and wasn't connected to a tty process that sets up xterm.js first? Just thinking if there are any sequences that xterm.js consumes to set the color mode...

Works for me
image

If I format the message on the server, instead of running chalk in the browser I get colors. If I use chalk directly in the browser, I don't get any colors.. lemme try one more thing to be sure I'm not just a total idiot (it may already be too late)

@aventurella try running the term.writeln('\u001b[34mHello\u001b[39m World\u001b[31m!\u001b[39m') directly without chalk.

@Tyriar ya, that works fine...

Now this has me wondering if it's not webpack doing something to chalk at bundle time.

Yeah it seems like a chalk issue then, try logging what chalk is returning or debug into the chalk call.

Closing since it's not a problem with xterm.js, I'm curious to hear the reason though.

@Tyriar ok I figured it out..

Chalk is doing some auto detection to see what it should support, using this lib: https://github.com/chalk/supports-color

Long story short, chalk thinks nothing is supported color wise, so it doesn't process the strings.

let options: any = {enabled: true, level: 2};
const forcedChalk = new chalk.constructor(options);

^ fixes it in the browser

where Levels are:

0: All colors disabled
1: Basic color support (16 colors)
2: 256 color support
3: Truecolor support (16 million colors)

Interesting, does chalk check by checking environment variables or something?

thanks @aventurella!

@Tyriar, ready for it? https://github.com/chalk/supports-color/blob/master/browser.js

boom. when chalk creates its config, it asks supports-color, which returns false for the browser.

@cdaringe Does this happen only when you build via browserify? Don't you always run chalk through a node process? 馃槙

@Tyriar, correct, im talking via a browser bundler ref. to clarify, my remark wasn't to highlight any issue with this package, rather, to just help drive further understanding :)

@aventurella thank you! but it seems that chalk.constructor is deprecated and may have been removed in the latest versions. We ought to use chalk.Instance instead.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jestapinski picture jestapinski  路  3Comments

zhangjie2012 picture zhangjie2012  路  3Comments

7PH picture 7PH  路  4Comments

chris-tse picture chris-tse  路  4Comments

Tyriar picture Tyriar  路  4Comments