1 | 'use strict'
|
2 |
|
3 | let cli = require('..')
|
4 | let util = require('util')
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function styledJSON (obj) {
|
18 | let json = JSON.stringify(obj, null, 2)
|
19 | if (cli.color.enabled) {
|
20 | let cardinal = require('cardinal')
|
21 | let theme = require('cardinal/themes/jq')
|
22 | cli.log(cardinal.highlight(json, {json: true, theme: theme}))
|
23 | } else {
|
24 | cli.log(json)
|
25 | }
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function styledHeader (header) {
|
38 | cli.log(cli.color.dim('=== ') + cli.color.bold(header))
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function styledObject (obj, keys) {
|
55 | let keyLengths = Object.keys(obj).map(key => key.toString().length)
|
56 | let maxKeyLength = Math.max.apply(Math, keyLengths) + 2
|
57 | function pp (obj) {
|
58 | if (typeof obj === 'string' || typeof obj === 'number') {
|
59 | return obj
|
60 | } else if (typeof obj === 'object') {
|
61 | return Object.keys(obj).map(k => k + ': ' + util.inspect(obj[k])).join(', ')
|
62 | } else {
|
63 | return util.inspect(obj)
|
64 | }
|
65 | }
|
66 | function logKeyValue (key, value) {
|
67 | cli.log(`${key}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value))
|
68 | }
|
69 | for (var key of (keys || Object.keys(obj).sort())) {
|
70 | let value = obj[key]
|
71 | if (Array.isArray(value)) {
|
72 | if (value.length > 0) {
|
73 | logKeyValue(key, value[0])
|
74 | for (var e of value.slice(1)) {
|
75 | cli.log(' '.repeat(maxKeyLength) + pp(e))
|
76 | }
|
77 | }
|
78 | } else if (value !== null && value !== undefined) {
|
79 | logKeyValue(key, value)
|
80 | }
|
81 | }
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | function styledNameValues (nameValues) {
|
98 | let keys = nameValues.map(nameValue => nameValue.name)
|
99 | let keyLengths = keys.map(name => name.toString().length)
|
100 | let maxKeyLength = Math.max.apply(Math, keyLengths) + 2
|
101 | function pp (obj) {
|
102 | if (typeof obj === 'string' || typeof obj === 'number') {
|
103 | return obj
|
104 | } else if (typeof obj === 'object') {
|
105 | return Object.keys(obj).map(k => k + ': ' + cli.inspect(obj[k])).join(', ')
|
106 | } else {
|
107 | return cli.inspect(obj)
|
108 | }
|
109 | }
|
110 | function logKeyValue (key, value) {
|
111 | cli.log(`${key}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value))
|
112 | }
|
113 | for (var nameValue of nameValues) {
|
114 | let value = nameValue.values
|
115 | if (Array.isArray(value)) {
|
116 | if (value.length > 0) {
|
117 | logKeyValue(nameValue.name, value[0])
|
118 | for (var e of value.slice(1)) {
|
119 | cli.log(' '.repeat(maxKeyLength) + pp(e))
|
120 | }
|
121 | }
|
122 | } else if (value !== null && value !== undefined) {
|
123 | logKeyValue(nameValue.name, value)
|
124 | }
|
125 | }
|
126 | }
|
127 |
|
128 | module.exports.styledJSON = styledJSON
|
129 | module.exports.styledHeader = styledHeader
|
130 | module.exports.styledObject = styledObject
|
131 | module.exports.styledNameValues = styledNameValues
|