Skip to content

Listr renderer with simple tree support (non concurrent tasks)

Notifications You must be signed in to change notification settings

pionl/listr-simple-tree-renderer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

listr-simple-tree-renderer

Listr simple tree renderer

screenshot.gif

Install

$ npm install --save listr-simple-tree-renderer

Usage

const SimpleTreeRenderer = require('listr-simple-tree-renderer');
const Listr = require('listr');

const list = new Listr([
	{
		title: 'foo',
		task: () => Promise.resolve('bar')
	},
	{
        title: 'Output can be function to change text',
        task: (context, task) => {
            task.output = () => {
                return `${figures.tick} Custom render`
            }
        }
    }
], {
	renderer: SimpleTreeRenderer
});

list.run();

Options

These options should be provided in the Listr options object.

indentString

Type: boolean Default: true

Append every indention based on corresponding level task.

taskIcon

Type: string Default: pointer

Icon that will be displayed next to the task name. Icon list in figures package

Spinner

Need to show spinner? Use custom rendering which will clear the output before changing task.

const logUpdate = require('log-update')
const ElegantSpinner = require('elegant-spinner')

/**
 * Shows spinner and runs given task. Returns value from the promise task.
 *
 * Do not output any data within promise task.
 *
 * @param {string} title
 * @param {Function} promiseTask
 * @return {Promise<any>}
 */
module.exports = async function (title, promiseTask) {
    function cancel () {
        clearInterval(interval)
        logUpdate.clear()
    }

    const spinner = ElegantSpinner()

    // Show the spinner
    const interval = setInterval(() => {
        logUpdate(spinner() + ' ' + title)
    }, 100)

    try {
        const response = await promiseTask()
        cancel()
        return response
    } catch (e) {
        cancel()
        throw e
    }
}

task = {
    title: 'test',
    task: (context, task) => showSpinner('Loading', async () => {
    	return await somethingThatWillReturnTitle()
    }).then((title) => {
        task.output = title
    })
}

Related

License

MIT © Martin Kluska