console-table-printer icon indicating copy to clipboard operation
console-table-printer copied to clipboard

Render raw string

Open MoKhajavi75 opened this issue 2 years ago • 1 comments

Hey!

I really appreciate your fantastic package!

I use render() to get a string but it has come with ANSI escape codes like [37m. Is there any way to get the raw text?

I want to use the output in a Telegram message so I need the raw text.

P.S. I can create a pull request if it's approved! Something like render(raw: boolean) => string with the false default value for raw to have backward compatibility

MoKhajavi75 avatar Jun 18 '22 01:06 MoKhajavi75

Interesting idea! Lets create a function called renderRaw and get output from that. Feel free to open a PR

ayonious avatar Aug 09 '22 13:08 ayonious

Been trying every RegEx I can think of / find and still can't get the control characters stripped from the output such that it renders as raw text. Bummer.

sheldonth avatar Oct 06 '22 22:10 sheldonth

This could be a good regex to use to strip all special chars. Needs some testing though https://github.com/ayonious/console-table-printer/blob/master/src/utils/console-utils.ts#L5

ayonious avatar Feb 24 '23 21:02 ayonious

After stripping all the ansii using that regex mentioned here This is how it looks like: To me its looking very much usable.

render_test_ts_snap_—_console-table-printer

// Find the latest regex from [the code](https://github.com/ayonious/console-table-printer/blob/master/src/utils/console-utils.ts#L5)
const colorRegex = /\x1b\[\d{1,3}(;\d{1,3})*m/g; // \x1b[30m \x1b[305m \x1b[38;5m

export const stripAnsi = (str: string): string => str.replace(colorRegex, '');

// Create table and add rows.....

// render, strip and print
const rendered = stripAnsi(p.render());
console.log(rendered);

ayonious avatar Jul 02 '23 10:07 ayonious

This has been included in this feature: https://console-table.netlify.app/docs/doc-no-ascii

If its not yet resolved feel free to open again

ayonious avatar Dec 22 '23 12:12 ayonious