New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

dynamic-ascii-table

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dynamic-ascii-table

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

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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 )
KeyTypeDefaultDescriptionRequired
silentbooleanfalseWhether 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 } )
KeyTypeDescriptionRequired
columnNamesarray of stringsArray of column names. Example [ 'nr', 'test' ]Yes
columnLengthsarray of numbersArray of column lengths. [ 10, 10 ]No
columnAlignmentsarray of stringsArray of column alignments ['left', 'right', 'center'].No
headerAlignmentstringAlignment 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()
KeyTypeDescriptionRequired
NoneNo 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 } )
KeyTypeDescriptionRequired
rowIndexnumberIndex of the row where the value will be set.Yes
columnNamestringName of the column where the value will be set.Yes
valueanyThe value to be set in the specified cell.Yes
strictbooleanDetermines 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 } ) 
KeyTypeDescriptionRequired
rowIndexnumberIndex of the row from which to retrieve the value.Yes
columnNamestringName 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() 
KeyTypeDescriptionRequired
NoneNo 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 } )
KeyTypeDescriptionRequired
configobjectAn 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()
KeyTypeDescriptionRequired
NoneNo 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.

Keywords

FAQs

Package last updated on 04 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc