Skip to content

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

License

Notifications You must be signed in to change notification settings

a6b8/dynamicAsciiTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 dynamic-ascii-table

Code

import { DynamicAsciiTable, getDemoData } from 'dynamic-ascii-table'

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 'dynamic-ascii-table'
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 'dynamic-ascii-table'

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 'dynamic-ascii-table'

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 'dynamic-ascii-table'

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 'dynamic-ascii-table'

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 'dynamic-ascii-table'

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 'dynamic-ascii-table'

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 'dynamic-ascii-table'

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published