Skip to content

Commit e7c81fe

Browse files
authored
feat: Add --indent option to CLI tool (#559)
1 parent f685e1c commit e7c81fe

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed

docs/09_cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Usage:
1616
Options:
1717
--help, -h Show this message.
1818
--json, -j Output JSON.
19+
--indent 2 Output pretty-printed data, indented by the given number of spaces.
1920

2021
Additional options for bare "yaml" command:
2122
--doc, -d Output pretty-printed JS Document objects.

src/cli.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Usage:
2424
Options:
2525
--help, -h Show this message.
2626
--json, -j Output JSON.
27+
--indent 2 Output pretty-printed data, indented by the given number of spaces.
2728
2829
Additional options for bare "yaml" command:
2930
--doc, -d Output pretty-printed JS Document objects.
@@ -55,6 +56,7 @@ export async function cli(
5556
options: {
5657
doc: { type: 'boolean', short: 'd' },
5758
help: { type: 'boolean', short: 'h' },
59+
indent: { type: 'string', short: 'i' },
5860
json: { type: 'boolean', short: 'j' },
5961
single: { type: 'boolean', short: '1' },
6062
strict: { type: 'boolean', short: 's' },
@@ -71,6 +73,8 @@ export async function cli(
7173
values: opt
7274
} = args
7375

76+
let indent = Number(opt.indent)
77+
7478
stdin.setEncoding('utf-8')
7579

7680
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
@@ -92,7 +96,7 @@ export async function cli(
9296
})
9397
stdin.on('end', () => {
9498
for (const tok of lexer.lex('', false)) add(tok)
95-
if (opt.json) console.log(JSON.stringify(data))
99+
if (opt.json) console.log(JSON.stringify(data, null, indent))
96100
done()
97101
})
98102
break
@@ -110,7 +114,7 @@ export async function cli(
110114
})
111115
stdin.on('end', () => {
112116
for (const tok of parser.parse('', false)) add(tok)
113-
if (opt.json) console.log(JSON.stringify(data))
117+
if (opt.json) console.log(JSON.stringify(data, null, indent))
114118
done()
115119
})
116120
break
@@ -159,7 +163,8 @@ export async function cli(
159163
} else {
160164
if (reqDocEnd) console.log('...')
161165
try {
162-
const str = String(doc)
166+
indent ||= 2
167+
const str = doc.toString({ indent })
163168
console.log(str.endsWith('\n') ? str.slice(0, -1) : str)
164169
} catch (error) {
165170
done(error as Error)
@@ -188,7 +193,7 @@ export async function cli(
188193
)
189194
}
190195
if (mode !== 'valid' && opt.json) {
191-
console.log(JSON.stringify(opt.single ? data[0] : data))
196+
console.log(JSON.stringify(opt.single ? data[0] : data, null, indent))
192197
}
193198
done()
194199
})

tests/cli.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,94 @@ const skip = Number(major) < 20
114114
['[{"hello":"world"},42]']
115115
)
116116
})
117+
describe('--indent', () => {
118+
ok(
119+
'basic',
120+
'hello:\n world: 2',
121+
['--indent', '3'],
122+
['hello:\n world: 2']
123+
)
124+
ok(
125+
'--json',
126+
'hello: world',
127+
['--json', '--indent', '2'],
128+
['[\n {\n "hello": "world"\n }\n]']
129+
)
130+
ok(
131+
'--single',
132+
'hello: world',
133+
['--json', '--indent', '2', '--single'],
134+
['{\n "hello": "world"\n}']
135+
)
136+
ok(
137+
'multiple',
138+
'hello: world\n---\n42',
139+
['--json', '--indent', '2'],
140+
['[\n {\n "hello": "world"\n },\n 42\n]']
141+
)
142+
ok(
143+
'Lexer',
144+
'hello: world',
145+
['lex', '--json', '--indent', '2'],
146+
['[\n "\\u0002",\n "\\u001f",\n "hello",\n ":",\n " ",\n "\\u001f",\n "world"\n]']
147+
)
148+
ok(
149+
'CST parser',
150+
'hello: world\n',
151+
['cst', '--json', '--indent', '2'],
152+
[JSON.stringify([
153+
{
154+
type: 'document',
155+
offset: 0,
156+
start: [],
157+
value: {
158+
type: 'block-map',
159+
offset: 0,
160+
indent: 0,
161+
items: [
162+
{
163+
start: [],
164+
key: {
165+
type: 'scalar',
166+
offset: 0,
167+
indent: 0,
168+
source: 'hello'
169+
},
170+
sep: [
171+
{
172+
type: 'map-value-ind',
173+
offset: 5,
174+
indent: 0,
175+
source: ':'
176+
},
177+
{
178+
type: "space",
179+
offset: 6,
180+
indent: 0,
181+
source: ' '
182+
}
183+
],
184+
value: {
185+
type: 'scalar',
186+
offset: 7,
187+
indent: 0,
188+
source: 'world',
189+
end: [
190+
{
191+
type: 'newline',
192+
offset: 12,
193+
indent: 0,
194+
source: '\n'
195+
}
196+
]
197+
}
198+
}
199+
]
200+
}
201+
}
202+
], null, 2)]
203+
)
204+
})
117205
describe('--doc', () => {
118206
ok('basic', 'hello: world', ['--doc'], [{ contents: { items: [{}] } }])
119207
ok(

0 commit comments

Comments
 (0)