Giter Club home page Giter Club logo

Comments (6)

ayonious avatar ayonious commented on August 24, 2024 1

a new function render for Table object has been exposed from version 2.4.0

from console-table-printer.

 avatar commented on August 24, 2024 1

Wow, you literally just did this... 😆

from console-table-printer.

arifkalayci avatar arifkalayci commented on August 24, 2024

I need to do something similar too. Looking at the code, I found out that the table is printed to the global console object. Something like this seems to work but hacky:

const { StringDecoder } = require('string_decoder');

class StringWritable extends stream.Writable {
  constructor(options) {
    super(options);
    this._decoder = new StringDecoder(options && options.defaultEncoding);
    this.data = '';
  }
  _write(chunk, encoding, callback) {
    if (encoding === 'buffer') {
      chunk = this._decoder.write(chunk);
    }
    this.data += chunk;
    callback();
  }
  _final(callback) {
    this.data += this._decoder.end();
    callback();
  }
}

const { printTable } = require("console-table-printer");

const testCases = [
  { index: 3, text: "I would like some gelb bananen bitte", value: 100 },
  { index: 4, text: "I hope batch update is working", value: 300 },
];

const { Console } = require('console');

// Save present console in a variable
const oldConsole = console;

// Create a new console which will print to a string and override 'console' global
const stream = new StringWritable();
console = new Console(stream);

printTable(testCases);

// Get old console back
console = oldConsole;

console.log(stream.data);

What I want to do is to print to a remote console. Overriding global console works, but in general it's desirable that the library accepts a custom console.

from console-table-printer.

ayonious avatar ayonious commented on August 24, 2024

There is already an internal function that I can expose that can help in this case.
Take a look at this.
https://github.com/ayonious/console-table-printer/blob/master/test/internalTable/simpleTable.test.ts#L14

Instead putting the output in console it returns array of string

[ '┌───────┬──────────────────────────────────────┬───────┐', '\u001b[37m│\u001b[0m\u001b[37m \u001b[0m\u001b[01mindex\u001b[0m\u001b[37m │\u001b[0m\u001b[37m \u001b[0m\u001b[01m text\u001b[0m\u001b[37m │\u001b[0m\u001b[37m \u001b[0m\u001b[01mvalue\u001b[0m\u001b[37m │\u001b[0m', '├───────┼──────────────────────────────────────┼───────┤', '\u001b[37m│\u001b[0m\u001b[37m \u001b[0m\u001b[37m 3\u001b[0m\u001b[37m │\u001b[0m\u001b[37m \u001b[0m\u001b[37mI would like some gelb bananen bitte\u001b[0m\u001b[37m │\u001b[0m\u001b[37m \u001b[0m\u001b[37m 100\u001b[0m\u001b[37m │\u001b[0m', '\u001b[37m│\u001b[0m\u001b[37m \u001b[0m\u001b[37m 4\u001b[0m\u001b[37m │\u001b[0m\u001b[37m \u001b[0m\u001b[37m I hope batch update is working\u001b[0m\u001b[37m │\u001b[0m\u001b[37m \u001b[0m\u001b[37m 300\u001b[0m\u001b[37m │\u001b[0m', '└───────┴──────────────────────────────────────┴───────┘', ];

My idea was you are supposed to console.log(or your custom logger) each element on this array. Make sure to put an endline after each logging.

So you will use it something like this

const t = new Table()
const ret = t.getConsoleLogs(); // array of string
// now use ret to output whereever you want

Does this sound good @elderapo @arifkalayci ?

from console-table-printer.

elderapo avatar elderapo commented on August 24, 2024

I'd suggest naming that method render and just return a single string (unless there is a reason why string[] would be better).

from console-table-printer.

 avatar commented on August 24, 2024

Fantastic! I just stumbled upon this update and was able to 2.2 -> 2.4 in my project seamlessly.

Now instead of clobbering the console in place,

this.context.stdout.write("thing");
this._table.printTable(); // x_x
this.context.stdout.write("other thing");

I can now elegantly...

this.context.stdout.write("thing");
this.context.stdout.write(this._table.render()); // ^_^
this.context.stdout.write("other thing");

🍻 Huzzah!

from console-table-printer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.