123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import { cli, Table } from 'cli-ux'
- import chalk from 'chalk'
- import { NameValueObj } from '../Types'
- import { AccountId } from '@polkadot/types/interfaces'
- export function displayHeader(caption: string, placeholderSign = '_', size = 50) {
- const singsPerSide: number = Math.floor((size - (caption.length + 2)) / 2)
- let finalStr = ''
- for (let i = 0; i < singsPerSide; ++i) finalStr += placeholderSign
- finalStr += ` ${caption} `
- while (finalStr.length < size) finalStr += placeholderSign
- process.stdout.write('\n' + chalk.bold.blueBright(finalStr) + '\n\n')
- }
- export function displayNameValueTable(rows: NameValueObj[]) {
- cli.table(
- rows,
- {
- name: { minWidth: 30, get: (row) => chalk.bold.white(row.name) },
- value: { get: (row) => chalk.white(row.value) },
- },
- { 'no-header': true }
- )
- }
- export function displayCollapsedRow(row: { [k: string]: string | number }) {
- const collapsedRow: NameValueObj[] = Object.keys(row).map((name) => ({
- name,
- value: typeof row[name] === 'string' ? (row[name] as string) : row[name].toString(),
- }))
- displayNameValueTable(collapsedRow)
- }
- export function displayCollapsedTable(rows: { [k: string]: string | number }[]) {
- for (const row of rows) displayCollapsedRow(row)
- }
- export function displayTable(rows: { [k: string]: string | number }[], cellHorizontalPadding = 0) {
- if (!rows.length) {
- return
- }
- const maxLength = (columnName: string) =>
- rows.reduce((maxLength, row) => {
- const val = row[columnName]
- const valLength = typeof val === 'string' ? val.length : val.toString().length
- return Math.max(maxLength, valLength)
- }, columnName.length)
- const columnDef = (columnName: string) => ({
- get: (row: typeof rows[number]) => chalk.white(`${row[columnName]}`),
- minWidth: maxLength(columnName) + cellHorizontalPadding,
- })
- const columns: Table.table.Columns<{ [k: string]: string }> = {}
- Object.keys(rows[0]).forEach((columnName) => (columns[columnName] = columnDef(columnName)))
- cli.table(rows, columns)
- }
- export function toFixedLength(text: string, length: number, spacesOnLeft = false): string {
- if (text.length > length && length > 3) {
- return text.slice(0, length - 3) + '...'
- }
- while (text.length < length) {
- spacesOnLeft ? (text = ' ' + text) : (text += ' ')
- }
- return text
- }
- export function shortAddress(address: AccountId | string): string {
- return address.toString().substr(0, 6) + '...' + address.toString().substr(-6)
- }
|