Skip to content

Instantly share code, notes, and snippets.

@oguimbal
Created December 20, 2024 13:13
Show Gist options
  • Save oguimbal/b90e5b12008090784c411288d72f2751 to your computer and use it in GitHub Desktop.
Save oguimbal/b90e5b12008090784c411288d72f2751 to your computer and use it in GitHub Desktop.
console.log react styling
import React from 'react';
// cf https://github.com/vadimdemedes/ink
import {Text, Box} from 'ink';
import {render} from 'ink-testing-library';
function transform(value) {
if (React.isValidElement(value)) {
const {lastFrame} = render(<Box>{value}</Box>);
return lastFrame();
}
return value;
}
for (const k of ['log', 'error', 'warn', 'info', 'debug']) {
const original = console[k];
console[k] = (...args) => {
original.apply(console, args.map(transform));
return original;
};
}
// use it like that
console.log(
<>
<Text color="green">I am green</Text>
<Text color="black" backgroundColor="white">
I am black on white
</Text>
<Text color="#ffffff">I am white</Text>
<Text bold>I am bold</Text>
<Text italic>I am italic</Text>
<Text underline>I am underline</Text>
<Text strikethrough>I am strikethrough</Text>
<Text inverse>I am inversed</Text>
</>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment