Skip to content

Commit b147b9f

Browse files
committed
feat: format decimals to two places when printing to console
Signed-off-by: Tomás Migone <[email protected]>
1 parent 09898ce commit b147b9f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

packages/indexer-cli/src/allocations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SubgraphDeploymentID, formatGRT, commify } from '@graphprotocol/common-
22
import yaml from 'yaml'
33
import { GluegunPrint } from 'gluegun'
44
import { table, getBorderCharacters } from 'table'
5-
import { OutputFormat, parseOutputFormat, pickFields, wrapCell } from './command-helpers'
5+
import { OutputFormat, parseOutputFormat, pickFields, wrapCell, formatDecimals } from './command-helpers'
66
import { IndexerManagementClient } from '@graphprotocol/indexer-common'
77
import gql from 'graphql-tag'
88
import {
@@ -151,7 +151,7 @@ export const displayIndexerAllocations = (
151151
: table(
152152
[
153153
Object.keys(allocations[0]),
154-
...allocations.map(allocation => Object.values(allocation).map(value => wrapCell(value, wrapWidth))),
154+
...allocations.map(allocation => Object.values(allocation).map(formatDecimals).map(value => wrapCell(value, wrapWidth))),
155155
],
156156
{
157157
border: getBorderCharacters('norc'),
@@ -167,7 +167,7 @@ export const displayIndexerAllocation = (
167167
? JSON.stringify(allocation, null, 2)
168168
: outputFormat === OutputFormat.Yaml
169169
? yaml.stringify(allocation).trim()
170-
: table([Object.keys(allocation), Object.values(allocation).map(value => wrapCell(value, wrapWidth))], {
170+
: table([Object.keys(allocation), Object.values(allocation).map(formatDecimals).map(value => wrapCell(value, wrapWidth))], {
171171
border: getBorderCharacters('norc'),
172172
}).trim()
173173

packages/indexer-cli/src/command-helpers.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function displayObjectData(outputFormat: OutputFormat, data: object, wrap
112112
return yaml.stringify(data).trim()
113113
} else {
114114
const keys = Object.keys(data)
115-
const values = Object.values(data).map(value => wrapCell(value, wrapWidth))
115+
const values = Object.values(data).map(formatDecimals).map(value => wrapCell(value, wrapWidth))
116116

117117
return table([keys, values], {
118118
border: getBorderCharacters('norc'),
@@ -136,7 +136,7 @@ export function displayObjectArrayData(
136136

137137
const tableData = [
138138
keys,
139-
...data.map(item => keys.map(key => wrapCell((item as any)[key], wrapWidth))),
139+
...data.map(item => keys.map(key => wrapCell(formatDecimals((item as any)[key]), wrapWidth))),
140140
]
141141

142142
return table(tableData, {
@@ -274,4 +274,15 @@ export function wrapCell(value: unknown, wrapWidth: number): string {
274274
return wrapWidth > 0
275275
? wrapAnsi(String(value), wrapWidth, { hard: true })
276276
: String(value)
277+
}
278+
279+
export function formatDecimals(value: unknown): string {
280+
if (typeof value === 'string') {
281+
const sanitizedValue = value.replace(/,/g, '');
282+
const numberValue = parseFloat(sanitizedValue);
283+
if (!isNaN(numberValue)) {
284+
return Number.isInteger(numberValue) ? numberValue.toString() : numberValue.toFixed(2);
285+
}
286+
}
287+
return String(value);
277288
}

0 commit comments

Comments
 (0)