Skip to content

Commit ca45311

Browse files
authored
tools: make the output of v check-md . more informative (#20819)
1 parent 36470fe commit ca45311

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

cmd/tools/vcheck-md.v

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ const vexe = @VEXE
2626

2727
struct CheckResult {
2828
pub mut:
29+
files int
2930
warnings int
3031
errors int
3132
oks int
3233
}
3334

3435
fn (v1 CheckResult) + (v2 CheckResult) CheckResult {
3536
return CheckResult{
37+
files: v1.files + v2.files
3638
warnings: v1.warnings + v2.warnings
3739
errors: v1.errors + v2.errors
3840
oks: v1.oks + v2.oks
@@ -83,9 +85,7 @@ fn main() {
8385
if res.errors == 0 && show_progress {
8486
clear_previous_line()
8587
}
86-
if res.warnings > 0 || res.errors > 0 || res.oks > 0 {
87-
println('\nWarnings: ${res.warnings} | Errors: ${res.errors} | OKs: ${res.oks}')
88-
}
88+
println('Checked .md files: ${res.files} | OKs: ${res.oks} | Warnings: ${res.warnings} | Errors: ${res.errors}')
8989
if res.errors > 0 {
9090
exit(1)
9191
}
@@ -171,40 +171,53 @@ fn (mut f MDFile) progress(message string) {
171171
}
172172
}
173173

174+
struct CheckResultContext {
175+
path string
176+
line_number int
177+
line string
178+
}
179+
180+
fn (mut res CheckResult) wcheck(actual int, limit int, ctx CheckResultContext, msg_template string) {
181+
if actual > limit {
182+
wprintln(wline(ctx.path, ctx.line_number, ctx.line.len, msg_template.replace('@',
183+
limit.str())))
184+
wprintln(ctx.line)
185+
wprintln(ftext('-'.repeat(limit) + '^', term.gray))
186+
res.warnings++
187+
}
188+
}
189+
190+
fn (mut res CheckResult) echeck(actual int, limit int, ctx CheckResultContext, msg_template string) {
191+
if actual > limit {
192+
eprintln(eline(ctx.path, ctx.line_number, ctx.line.len, msg_template.replace('@',
193+
limit.str())))
194+
eprintln(ctx.line)
195+
eprintln(ftext('-'.repeat(limit) + '^', term.gray))
196+
res.errors++
197+
}
198+
}
199+
174200
fn (mut f MDFile) check() CheckResult {
175-
mut res := CheckResult{}
201+
mut res := CheckResult{
202+
files: 1
203+
}
176204
mut anchor_data := AnchorData{}
177205
for j, line in f.lines {
178206
// f.progress('line: $j')
179207
if !f.skip_line_length_check {
208+
ctx := CheckResultContext{f.path, j, line}
180209
if f.state == .vexample {
181-
if line.len > too_long_line_length_example {
182-
wprintln(wline(f.path, j, line.len, 'example lines must be less than ${too_long_line_length_example} characters'))
183-
wprintln(line)
184-
res.warnings++
185-
}
210+
res.wcheck(line.len, too_long_line_length_example, ctx, 'example lines must be less than @ characters')
186211
} else if f.state == .codeblock {
187-
if line.len > too_long_line_length_codeblock {
188-
wprintln(wline(f.path, j, line.len, 'code lines must be less than ${too_long_line_length_codeblock} characters'))
189-
wprintln(line)
190-
res.warnings++
191-
}
212+
res.wcheck(line.len, too_long_line_length_codeblock, ctx, 'code lines must be less than @ characters')
192213
} else if line.starts_with('|') {
193-
if line.len > too_long_line_length_table {
194-
wprintln(wline(f.path, j, line.len, 'table lines must be less than ${too_long_line_length_table} characters'))
195-
wprintln(line)
196-
res.warnings++
197-
}
214+
res.wcheck(line.len, too_long_line_length_table, ctx, 'table lines must be less than @ characters')
198215
} else if line.contains('http') {
199-
if line.all_after('https').len > too_long_line_length_link {
200-
wprintln(wline(f.path, j, line.len, 'link lines must be less than ${too_long_line_length_link} characters'))
201-
wprintln(line)
202-
res.warnings++
203-
}
204-
} else if line.len > too_long_line_length_other {
205-
eprintln(eline(f.path, j, line.len, 'must be less than ${too_long_line_length_other} characters'))
206-
eprintln(line)
207-
res.errors++
216+
// vfmt off
217+
res.wcheck(line.all_after('https').len, too_long_line_length_link, ctx, 'link lines must be less than @ characters')
218+
// vfmt on
219+
} else {
220+
res.echeck(line.len, too_long_line_length_other, ctx, 'must be less than @ characters')
208221
}
209222
}
210223
if f.state == .markdown {

0 commit comments

Comments
 (0)