npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamic-ascii-table

v1.0.0

Published

DynamicAsciiTable efficiently displays dynamic tables in terminals, ideal for applications with complex task monitoring requirements. It offers customizable formatting and seamless data handling.

Downloads

17

Readme

CircleCI PRs Welcome

DynamicAsciiTable

This module helps to display a dynamic table in the terminal. The module was developed for applications that need to monitor a large number of different tasks using multithreading and asynchronous queries.

┌───────┬──────────────┬──────────────┬─────────────────┐
│ nr    │ deployments  │ authList     │ accumulatorList │
├───────┼──────────────┼──────────────┼─────────────────┤
│ 7     │          472 │          404 │             332 │
│ 8     │          466 │          406 │             328 │
│ 9     │          468 │          384 │             384 │
│ 10    │          458 │          400 │                 │
└───────┴──────────────┴──────────────┴─────────────────┘

Features:

  • Efficient resource usage.
  • Formatting and alignment of the header and content areas.
  • Separation of data input and data display.
  • Auto-detection of terminal width.
  • Autoscrolling and sorting functionality to keep the latest rows in view.

Quickstart

To authentically represent the usage, you can import a sample dataset with getDemoData, which then synchronously inserts the data and updates the table.

Terminal

npm i dynamicAsciiTable

Code

import { DynamicAsciiTable, getDemoData } from 'dynamicAsciiTable'

const demoData = getDemoData(1000, 100)
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))

const dt = new DynamicAsciiTable()
const { columnNames, columnLengths, columnAlignments, headerAlignment } = demoData['init']
dt.init({ columnNames, columnLengths, columnAlignments, headerAlignment })

for (const row of demoData['rows']) {
    const { rowIndex, columnName, value } = row
    dt.setValue({ rowIndex, columnName, value })
    dt.print()
    await delay(10)
}

Code

This example shows how to query public Nodes with Node.js.

Table of Contents

Methods

Among the public methods, the most important ones are:

  • .init(): Initializes the internal data and can be updated with another .init().
  • .setValue(): This is where the data is played in.
  • .print(): This is how the data is displayed as a table, and from the second retrieval on, it is overwritten. Unless a .init() resets the table and data. Then a new table is written.

constructor()

Method

constructor( silent )

| Key | Type | Default | Description | Required | |--------------|----------|---------|----------------------------------------------------|----------| | silent | boolean | false | Whether to disable comments (true for yes, false for no).| Yes |

Example

import { DynamicAsciiTable } from 'dynamicAsciiTable'
const dt = new DynamicAsciiTable() 

Returns

true

init()

With this method, all variables needed internally are initialized. Also, templates are created so that the print method can run as efficiently as possible.

Method

.init( { columnNames, columnLengths, columnAlignments, headerAlignment } )

| Key | Type | Description | Required | |--------------------|----------|---------------------------------------------------|----------| | columnNames | array of strings | Array of column names. Example [ 'nr', 'test' ] | Yes | | columnLengths | array of numbers | Array of column lengths. [ 10, 10 ] | No | | columnAlignments | array of strings | Array of column alignments ['left', 'right', 'center']. | No | | headerAlignment | string | Alignment of the header left, right, center. | No |

Example

true

Returns

import { DynamicAsciiTable, getDemoData } from '../src/index.mjs'

const demoData = getDemoData( 1000, 100 ) 
const dt = new DynamicAsciiTable()
const { columnNames, columnLengths, columnAlignments, headerAlignment} = demoData['init']
dt.init( { columnNames, columnLengths, columnAlignments, headerAlignment } )

print()

This method outputs the actual table. The header is written at the first call after the .init() method. From the second call on, the body is deleted in order to be overwritten with the new information.

Method

.print()

| Key | Type | Description | Required | |--------------|--------|--------------------------------------|----------| | None | | No parameters required. | |

This table describes the usage of the .print() method.

Example

import { DynamicAsciiTable } from '../src/index.mjs'

const delay = ( ms ) => new Promise( resolve => setTimeout( resolve, ms ) )
 
const dt = new DynamicAsciiTable()
dt.init( { 'columnNames': [ 'a', 'b', 'c' ] } )

const rows = [
    { rowIndex: 0, columnName: 'a', value: 1 },
    { rowIndex: 1, columnName: 'b', value: 2 },
    { rowIndex: 0, columnName: 'b', value: 3 },
    { rowIndex: 1, columnName: 'c', value: 4 }
]

for( const row of rows ) {
    dt.setValue( row )
    dt.print()
}

Returns

true

setValue()

With this method, the table can be filled with content.

Method

.setValue( { rowIndex, columnName, value, strict=true } )

| Key | Type | Description | Required | |---------------|----------|---------------------------------------------------------------|----------| | rowIndex | number | Index of the row where the value will be set. | Yes | | columnName | string | Name of the column where the value will be set. | Yes | | value | any | The value to be set in the specified cell. | Yes | | strict | boolean | Determines if strict mode is enabled (default is true). | No |

Example

import { DynamicAsciiTable } from '../src/index.mjs'

const dt = new DynamicAsciiTable()
dt.init( { 'columnNames': [ 'a' ] } )
dt.setValue( { rowIndex: 0, columnName: 'a', value: 1 } )
dt.print()

Returns

true

getValue()

With this method, a value can be output from the internal memory.

Method

.getValue( { rowIndex, columnName } ) 

| Key | Type | Description | Required | |---------------|----------|-----------------------------------------------|----------| | rowIndex | number | Index of the row from which to retrieve the value. | Yes | | columnName | string | Name of the column from which to retrieve the value.| Yes |

Example

import { DynamicAsciiTable } from '../src/index.mjs'

const dt = new DynamicAsciiTable()
dt.init( { 'columnNames': [ 'a', 'b', 'c' ] } )

dt.setValue( { rowIndex: 0, columnName: 'a', value: 1 } )
const value = dt.getValue( { rowIndex: 0, columnName: 'a' } )
console.log( '>', value )

Returns

[ value !== null, value ]

getConfig()

With this method, the current configuration can be output. The default configuration is located under ./src/data/config.mjs for inspection. Changing it is done with .setConfig().

Method

.getConfig() 

| Key | Type | Description | Required | |---------------|--------|---------------------------------------|----------| | None | | No parameters required. | |

This table describes the usage of the getConfig method.

Example

import { DynamicAsciiTable } from '../src/index.mjs'

const dt = new DynamicAsciiTable()
dt.init( { 'columnNames': [ 'a', 'b', 'c' ] } )
const config = dt.getConfig()

Returns

key/value Object

setConfig()

With this method, the configuration can be changed. It is recommended to first load the default setting and then adjust it according to your own preferences.

Method

.setConfig( { config } )

| Key | Type | Description | Required | |---------------|--------|-----------------------------------------------|----------| | config | object | An object containing the configuration settings to be set. | Yes |

Example

import { DynamicAsciiTable } from '../src/index.mjs'

const dt = new DynamicAsciiTable()
dt.init( { 'columnNames': [ 'a', 'b', 'c' ] } )
const config = dt.getConfig()
config['symbols']['use'] = 'double'
dt.setConfig( { config } )

Returns

true

health()

This method is an internal function to check if the class is loading properly.

Method

.health()

| Key | Type | Description | Required | |---------------|--------|---------------------------------------|----------| | None | | No parameters required. | |

Example

import { DynamicAsciiTable } from '../src/index.mjs'

const dt = new DynamicAsciiTable()
dt.init( { 'columnNames': [ 'a', 'b', 'c' ] } )
dt.setValue( { rowIndex: 0, columnName: 'a', value: 1 } )
dt.health()

Returns

boolean

License

This project is licensed under the MIT License - see the LICENSE file for details.