Skip to content

Commit 325d6fa

Browse files
committed
feat: add column wrapping to table format in cli
Signed-off-by: Tomás Migone <[email protected]>
1 parent 606a8e7 commit 325d6fa

File tree

3 files changed

+76
-23
lines changed

3 files changed

+76
-23
lines changed

packages/indexer-cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"graphql-tag": "2.12.6",
3939
"isomorphic-fetch": "3.0.0",
4040
"table": "6.7.5",
41-
"yaml": "1.10.2"
41+
"yaml": "1.10.2",
42+
"wrap-ansi": "9.0.0"
4243
},
4344
"devDependencies": {
4445
"@types/isomorphic-fetch": "0.0.36",

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
// parameters array and returns the result of that.
2828

2929
import { table, getBorderCharacters } from 'table'
30+
import wrapAnsi from 'wrap-ansi'
3031

3132
export enum OutputFormat {
3233
Table = 'table',
@@ -99,47 +100,63 @@ export function pickFields(
99100
}
100101
}
101102
for (const key of drop) {
102-
delete obj[key]
103+
delete obj[key]
103104
}
104105
return obj
105106
}
106107

107-
export function displayObjectData(outputFormat: OutputFormat, data: object): string {
108-
return outputFormat === OutputFormat.Json
109-
? JSON.stringify(data, null, 2)
110-
: outputFormat === OutputFormat.Yaml
111-
? yaml.stringify(data).trim()
112-
: table([Object.keys(data), Object.values(data)], {
113-
border: getBorderCharacters('norc'),
114-
}).trim()
108+
export function displayObjectData(outputFormat: OutputFormat, data: object, wrapWidth: number): string {
109+
if (outputFormat === OutputFormat.Json) {
110+
return JSON.stringify(data, null, 2)
111+
} else if (outputFormat === OutputFormat.Yaml) {
112+
return yaml.stringify(data).trim()
113+
} else {
114+
const keys = Object.keys(data)
115+
const values = Object.values(data).map(value => wrapCell(value, wrapWidth))
116+
117+
return table([keys, values], {
118+
border: getBorderCharacters('norc'),
119+
}).trim()
120+
}
115121
}
116122

117123
export function displayObjectArrayData(
118124
outputFormat: OutputFormat,
119125
data: object[],
126+
wrapWidth: number,
120127
): string {
121-
return outputFormat === OutputFormat.Json
122-
? JSON.stringify(data, null, 2)
123-
: outputFormat === OutputFormat.Yaml
124-
? yaml.stringify(data).trim()
125-
: data.length === 0
126-
? 'No items found'
127-
: table([Object.keys(data[0]), ...data.map(item => Object.values(item))], {
128-
border: getBorderCharacters('norc'),
129-
}).trim()
128+
if (outputFormat === OutputFormat.Json) {
129+
return JSON.stringify(data, null, 2)
130+
} else if (outputFormat === OutputFormat.Yaml) {
131+
return yaml.stringify(data).trim()
132+
} else if (data.length === 0) {
133+
return 'No items found'
134+
} else {
135+
const keys = Object.keys(data[0])
136+
137+
const tableData = [
138+
keys,
139+
...data.map(item => keys.map(key => wrapCell((item as any)[key], wrapWidth))),
140+
]
141+
142+
return table(tableData, {
143+
border: getBorderCharacters('norc'),
144+
}).trim()
145+
}
130146
}
131147

132148
export function printObjectOrArray(
133149
print: GluegunPrint,
134150
outputFormat: OutputFormat,
135151
data: object | object[],
136152
keys: string[],
153+
wrapWidth: number = 0,
137154
): void {
138155
if (Array.isArray(data)) {
139156
const formatted = data.map(item => pickFields(item, keys))
140-
print.info(displayObjectArrayData(outputFormat, formatted))
157+
print.info(displayObjectArrayData(outputFormat, formatted, wrapWidth))
141158
} else if (data) {
142-
print.info(displayObjectData(outputFormat, pickFields(data, keys)))
159+
print.info(displayObjectData(outputFormat, pickFields(data, keys), wrapWidth))
143160
} else {
144161
print.error(`No items returned`)
145162
}
@@ -251,3 +268,10 @@ export function requireProtocolNetworkOption(options: { [key: string]: any }): s
251268
}
252269
return protocolNetwork
253270
}
271+
272+
273+
export function wrapCell(value: unknown, wrapWidth: number): string {
274+
return wrapWidth > 0
275+
? wrapAnsi(String(value), wrapWidth, { hard: true })
276+
: String(value)
277+
}

yarn.lock

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3289,7 +3289,7 @@ ansi-styles@^5.0.0:
32893289
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-5.2.0.tgz#07449690ad45777d1924ac2abb2fc8895dba836b"
32903290
integrity sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==
32913291

3292-
ansi-styles@^6.1.0:
3292+
ansi-styles@^6.1.0, ansi-styles@^6.2.1:
32933293
version "6.2.1"
32943294
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.2.1.tgz#0e62320cf99c21afff3b3012192546aacbfb05c5"
32953295
integrity sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==
@@ -4951,6 +4951,11 @@ emittery@^0.13.1:
49514951
resolved "https://registry.yarnpkg.com/emittery/-/emittery-0.13.1.tgz#c04b8c3457490e0847ae51fced3af52d338e3dad"
49524952
integrity sha512-DeWwawk6r5yR9jFgnDKYt4sLS0LmHJJi3ZOnb5/JdbYwj3nW+FxQnHIjhBKz8YLC7oRNPVM9NQ47I3CVx34eqQ==
49534953

4954+
emoji-regex@^10.3.0:
4955+
version "10.4.0"
4956+
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-10.4.0.tgz#03553afea80b3975749cfcb36f776ca268e413d4"
4957+
integrity sha512-EC+0oUMY1Rqm4O6LLrgjtYDvcVYTy7chDnM4Q7030tP4Kwj3u/pR6gP9ygnp2CJMK5Gq+9Q2oqmrFJAz01DXjw==
4958+
49544959
emoji-regex@^8.0.0:
49554960
version "8.0.0"
49564961
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
@@ -5894,6 +5899,11 @@ get-caller-file@^2.0.5:
58945899
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
58955900
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
58965901

5902+
get-east-asian-width@^1.0.0:
5903+
version "1.3.0"
5904+
resolved "https://registry.yarnpkg.com/get-east-asian-width/-/get-east-asian-width-1.3.0.tgz#21b4071ee58ed04ee0db653371b55b4299875389"
5905+
integrity sha512-vpeMIQKxczTD/0s2CdEWHcb0eeJe6TFjxb+J5xgX7hScxqrGuyjmv4c1D4A/gelKfyox0gJJwIHF+fLjeaM8kQ==
5906+
58975907
get-intrinsic@^1.2.5, get-intrinsic@^1.2.6, get-intrinsic@^1.3.0:
58985908
version "1.3.0"
58995909
resolved "https://registry.yarnpkg.com/get-intrinsic/-/get-intrinsic-1.3.0.tgz#743f0e3b6964a93a5491ed1bffaae054d7f98d01"
@@ -10371,6 +10381,15 @@ string-width@^5.0.1, string-width@^5.1.2:
1037110381
emoji-regex "^9.2.2"
1037210382
strip-ansi "^7.0.1"
1037310383

10384+
string-width@^7.0.0:
10385+
version "7.2.0"
10386+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-7.2.0.tgz#b5bb8e2165ce275d4d43476dd2700ad9091db6dc"
10387+
integrity sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==
10388+
dependencies:
10389+
emoji-regex "^10.3.0"
10390+
get-east-asian-width "^1.0.0"
10391+
strip-ansi "^7.1.0"
10392+
1037410393
string_decoder@^1.1.1:
1037510394
version "1.3.0"
1037610395
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.3.0.tgz#42f114594a46cf1a8e30b0a84f56c78c3edac21e"
@@ -10413,7 +10432,7 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1:
1041310432
dependencies:
1041410433
ansi-regex "^5.0.1"
1041510434

10416-
strip-ansi@^7.0.1:
10435+
strip-ansi@^7.0.1, strip-ansi@^7.1.0:
1041710436
version "7.1.0"
1041810437
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
1041910438
integrity sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==
@@ -11228,6 +11247,15 @@ workerpool@^6.5.1:
1122811247
string-width "^4.1.0"
1122911248
strip-ansi "^6.0.0"
1123011249

11250+
11251+
version "9.0.0"
11252+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-9.0.0.tgz#1a3dc8b70d85eeb8398ddfb1e4a02cd186e58b3e"
11253+
integrity sha512-G8ura3S+3Z2G+mkgNRq8dqaFZAuxfsxpBB8OCTGRTCtp+l/v9nbFNmCUP1BZMts3G1142MsZfn6eeUKrr4PD1Q==
11254+
dependencies:
11255+
ansi-styles "^6.2.1"
11256+
string-width "^7.0.0"
11257+
strip-ansi "^7.1.0"
11258+
1123111259
wrap-ansi@^6.0.1:
1123211260
version "6.2.0"
1123311261
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-6.2.0.tgz#e9393ba07102e6c91a3b221478f0257cd2856e53"

0 commit comments

Comments
 (0)