xpl / ololog

A better console.log for the log-driven debugging junkies
https://www.npmjs.com/package/ololog
The Unlicense
215 stars 8 forks source link

How to use with blessed library #10

Closed ourarash closed 5 years ago

ourarash commented 5 years ago

I was trying to use this with the npm blessed library, so that I have two boxes: one for output log and one for showing progress which doesn't scroll.

I used:

let renderedText = ololog.before('render')(...args);

and was able to make set of new functions called mainLog. I could make mainLog.info(), error, and warn to work, but I can't make without any tags, e.g. mainLog("print this item");

My complete code is here. In general, how would you do this?

xpl commented 5 years ago

@ourarash Instead of wrapping ololog into a class, like you do:

class mainLogClass {

    info(...args) {
        let renderedText = ololog.info.before('render')(...args);

        mainBox.pushLine(renderedText);
        mainBox.scroll(1);
        screen.render();
        return this;
    }

    // ... et cetera ...
}

const mainLog = new mainLogClass();

...you can simply override the render step by using the configure (so you don't need the mainLogClass):

const mainLog = ololog.configure ({

    render (text) {

        mainBox.pushLine (text)
        mainBox.scroll (1)
        screen.render ()
    }
})
mainLog.info(`This is line ${i++}`, `  hi`, ` ${i}`)
mainLog (`This is line ${i++}`, `  hi`, ` ${i}`)

Also, if you really need chaining (I noticed that you return this from your wrapped methods, I guess for that), you can override the returnValue step (this is the last step in the ololog's call chain, see here):

const mainLog = ololog.configure ({

    render (text) {

        mainBox.pushLine (text)
        mainBox.scroll (1)
        screen.render ()
    }

    returnValue () { return this }
})

Then you could do:

mainLog.info(`This is line ${i++}`, `  hi`, ` ${i}`)
       .warn(`This is line ${i++}`, `  hi`, ` ${i}`)

Let me know if the above helps.

xpl commented 5 years ago

@ourarash Also, in that render override you can receive the consoleMethod param (which can be info/debug/warn/error) — for example, if you need to route the rendered text to different places depending on what log method you called:

const mainLog = ololog.configure ({

    render (text, { consoleMethod = 'log' /* can be also debug/info/error/warn */ }) {

        /* do different things depending on the `consoleMethod` value */
    }
})

mainLog.error ('foo')
mainLog ('bar')
ourarash commented 5 years ago

Thanks, this worked great!