Skip to content

Commit

Permalink
Adding in Default Colors to print outs
Browse files Browse the repository at this point in the history
  • Loading branch information
sirarthurmoney committed Feb 9, 2024
1 parent 71e355c commit 454697f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 55 deletions.
1 change: 1 addition & 0 deletions packages/io-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"test": "jest --ci"
},
"dependencies": {
"chalk": "^4.1.2",
"cli-table3": "^0.6.3",
"logform": "^2.6.0",
"prompts": "^2.4.2",
Expand Down
76 changes: 63 additions & 13 deletions packages/io-devtools/src/stdio/printer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import Table, { HorizontalTableRow } from 'cli-table3'
import Table, { HorizontalAlignment, HorizontalTableRow } from 'cli-table3'
import chalk from 'chalk'
import type { ZodError } from 'zod'

type Preprocess = (value: string) => string
export const COLORS = {
TRUE: chalk.rgb(0, 153, 0), // GREEN
FALSE: chalk.rgb(255, 0, 0), // RED
NOT_APPLICABLE: chalk.rgb(255, 128, 0), // ORANGE
DEFAULT_KEY: chalk.rgb(255, 255, 255), // WHITE
DEFAULT_VALUE: chalk.rgb(167, 125, 255), // MAGENTA
}

/**
* Returns a JSON-serialized version of a string, replacing all `BigInt`
* instances with strings
Expand Down Expand Up @@ -29,28 +39,41 @@ export const printJson = (obj: unknown, pretty = true): string =>
* @see {@link printRecord}
*
* @param value
* @param keyColor - color of keys in table (DEFAULT: WHITE)
* @param valueColor - color of values in table (DEFAULT: MAGENTA)
* @returns
*/
const printValue = (value: unknown): string => {
const printValue = (
value: unknown,
keyColor: Preprocess = COLORS.DEFAULT_KEY,
valueColor: Preprocess = COLORS.DEFAULT_VALUE
): string => {
switch (true) {
case value == null:
case value instanceof Date:
case typeof value !== 'object':
return String(value)
return valueColor(String(value))

default:
return printRecord(value)
return printRecord(value, undefined, keyColor, valueColor)
}
}

export const printRecord = <TRecord extends object>(obj: TRecord, title?: string | number): string => {
export const printRecord = <TRecord extends object>(
obj: TRecord,
title?: string | number,
keyColor: Preprocess = COLORS.DEFAULT_KEY,
valueColor: Preprocess = COLORS.DEFAULT_VALUE
): string => {
const table = new Table({
wordWrap: true,
wrapOnWordBoundary: false,
})

const headers: HorizontalTableRow[] = title == null ? [] : [[{ content: title, colSpan: 2 }]]
const rows = Object.entries(obj).map(([key, value]): HorizontalTableRow => [key, printValue(value)])
const rows = Object.entries(obj).map(
([key, value]): HorizontalTableRow => [keyColor(key), printValue(value, keyColor, valueColor)]
)

return table.push(...headers, ...rows), table.toString()
}
Expand All @@ -63,20 +86,39 @@ export const printRecord = <TRecord extends object>(obj: TRecord, title?: string
* the property name.
*
* @param {Record<string | number, unknown>} records
* @param {string[]} [header]
* @param {string[]} [header] - header row if provided (OPTIONAL)
* @param center - center text if true (OPTIONAL)
* @param keyColor - color of keys in table (DEFAULT: WHITE)
* @param valueColor - color of values in table (DEFAULT: MAGENTA)
* @returns {string}
*/
export const printCrossTable = <TRecord extends Record<string | number, unknown>>(
records: TRecord[],
header?: string[]
headers?: string[],
center?: boolean,
keyColor: chalk.Chalk = COLORS.DEFAULT_KEY,
valueColor: chalk.Chalk = COLORS.DEFAULT_VALUE
): string => {
const table = new Table({
head: header ?? [],
wordWrap: true,
wrapOnWordBoundary: false,
style: { head: ['reset'] },
})

// Set the colored headers if provided
const coloredHeaders = headers?.map((header) => keyColor(header)) ?? []
table.push(coloredHeaders)

if (center) {
// Keep property column 'left'
const colAligns: HorizontalAlignment[] = ['left']
// Center-align for all other columns
for (let i = 0; i < records.length; i++) {
colAligns.push('center')
}
// Set the alignment for the table
table.options.colAligns = colAligns
}

// We'll gather all the properties of all the records in this array
//
// We do this in case some of the objects were missing any properties -
Expand All @@ -94,10 +136,10 @@ export const printCrossTable = <TRecord extends Record<string | number, unknown>
propertiesLeft.delete(property)

// Get all the values and print them
const values = records.map((record) => record[property]).map(printValue)
const values = records.map((record) => printValue(record[property], keyColor, valueColor))

// Create a row with the property label first
const row = [property, ...values]
const row = [keyColor(property), ...values]

// And add to the table
table.push(row)
Expand All @@ -110,9 +152,17 @@ export const printCrossTable = <TRecord extends Record<string | number, unknown>
* Helper utility for printing out boolean values
*
* @param {boolean | null | undefined} value
* @param nullColor
* @param trueColor
* @param falseColor
* @returns {string}
*/
export const printBoolean = (value: boolean | null | undefined): string => (value == null ? '∅' : value ? '✓' : '⤫')
export const printBoolean = (
value: boolean | null | undefined,
nullColor: chalk.Chalk = COLORS.NOT_APPLICABLE,
trueColor: chalk.Chalk = COLORS.TRUE,
falseColor: chalk.Chalk = COLORS.FALSE
): string => (value == null ? nullColor('∅') : value ? trueColor('✓') : falseColor('⤫'))

export const printZodErrors = (error: ZodError<unknown>): string => {
// Here we will go through all the errors and prefix them with the name
Expand Down
9 changes: 5 additions & 4 deletions packages/ua-devtools-evm-hardhat/src/tasks/oapp/peers.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ const action: ActionType<TaskArgs> = async ({ oappConfig: oappConfigPath, logLev
})

console.log(
printCrossTable(peerNetworkMatrix, ['from → to', ...points.map(({ networkName }) => networkName)]),
`\n\t${printBoolean(true)} - Connected\n`,
`\t${printBoolean(false)} - Not Connected\n`,
`\t${printBoolean(undefined)} - Ignored`
printCrossTable(peerNetworkMatrix, ['from → to', ...points.map(({ networkName }) => networkName)], true) +
'\n',
`${printBoolean(true)} - Connected\n`,
`${printBoolean(false)} - Not Connected\n`,
`${printBoolean(undefined)} - Ignored`
)

return peers
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ exports[`task lz:oapp:peers:get should show all chains are connected after runni
"┌───────────┬─────────┬───────────┬───────┐
│ from → to │ britney │ vengaboys │ tango │
├───────────┼─────────┼───────────┼───────┤
│ britney │
│ britney │
├───────────┼─────────┼───────────┼───────┤
│ vengaboys │
│ vengaboys │
├───────────┼─────────┼───────────┼───────┤
│ tango │ ✓ │ ✓ │ ∅ │
└───────────┴─────────┴───────────┴───────┘",
"
✓ - Connected
│ tango │ ✓ │ ✓ │ ∅ │
└───────────┴─────────┴───────────┴───────┘
",
" ⤫ - Not Connected
"✓ - Connected
",
" ∅ - Ignored",
"⤫ - Not Connected
",
"∅ - Ignored",
]
`;

Expand All @@ -53,16 +53,16 @@ exports[`task lz:oapp:peers:get should show all chains are connected after runni
"┌───────────┬─────────┬───────────┐
│ from → to │ britney │ vengaboys │
├───────────┼─────────┼───────────┤
│ britney │
│ britney │
├───────────┼─────────┼───────────┤
│ vengaboys │
└───────────┴─────────┴───────────┘",
"
✓ - Connected
│ vengaboys │
└───────────┴─────────┴───────────┘
",
"✓ - Connected
",
" ⤫ - Not Connected
"⤫ - Not Connected
",
" ∅ - Ignored",
"∅ - Ignored",
]
`;

Expand All @@ -71,18 +71,18 @@ exports[`task lz:oapp:peers:get should show all chains are connected expect one
"┌───────────┬─────────┬───────────┬───────┐
│ from → to │ britney │ vengaboys │ tango │
├───────────┼─────────┼───────────┼───────┤
│ britney │
│ britney │
├───────────┼─────────┼───────────┼───────┤
│ vengaboys │
│ vengaboys │
├───────────┼─────────┼───────────┼───────┤
│ tango │
└───────────┴─────────┴───────────┴───────┘",
"
✓ - Connected
│ tango │
└───────────┴─────────┴───────────┴───────┘
",
"✓ - Connected
",
" ⤫ - Not Connected
"⤫ - Not Connected
",
" ∅ - Ignored",
"∅ - Ignored",
]
`;

Expand All @@ -91,18 +91,18 @@ exports[`task lz:oapp:peers:get should show all chains are not connected expect
"┌───────────┬─────────┬───────────┬───────┐
│ from → to │ britney │ vengaboys │ tango │
├───────────┼─────────┼───────────┼───────┤
│ britney │
│ britney │
├───────────┼─────────┼───────────┼───────┤
│ vengaboys │
│ vengaboys │
├───────────┼─────────┼───────────┼───────┤
│ tango │ ∅ │ ∅ │ ∅ │
└───────────┴─────────┴───────────┴───────┘",
"
✓ - Connected
│ tango │ ∅ │ ∅ │ ∅ │
└───────────┴─────────┴───────────┴───────┘
",
" ⤫ - Not Connected
"✓ - Connected
",
" ∅ - Ignored",
"⤫ - Not Connected
",
"∅ - Ignored",
]
`;

Expand Down Expand Up @@ -255,15 +255,15 @@ exports[`task lz:oapp:peers:get should show no chains are connected with two net
"┌───────────┬─────────┬───────────┐
│ from → to │ britney │ vengaboys │
├───────────┼─────────┼───────────┤
│ britney │
│ britney │
├───────────┼─────────┼───────────┤
│ vengaboys │
└───────────┴─────────┴───────────┘",
"
✓ - Connected
│ vengaboys │
└───────────┴─────────┴───────────┘
",
"✓ - Connected
",
" ⤫ - Not Connected
"⤫ - Not Connected
",
" ∅ - Ignored",
"∅ - Ignored",
]
`;

0 comments on commit 454697f

Please sign in to comment.