Abstract seq[T] printing

This commit is contained in:
Florian Schroedl
2022-05-04 17:00:00 +02:00
parent 4fee450324
commit a598092f7c
2 changed files with 70 additions and 36 deletions

View File

@@ -1,5 +1,5 @@
import std/[
sequtils,
options,
sequtils,
strformat,
strutils,
@@ -20,3 +20,25 @@ func stringifyFields*(
.filter(x => x.print)
.map(x => x.field & ": " & $x.value)
.join(",\n")
func stringifyBlock*(blockName: string, indent = 0, xs: varargs[string]): string =
let fieldIndent = indent + INDENT_SIZE
concat(
@[&"{blockName}("],
@xs.mapIt(it.indent(fieldIndent)),
@[")"],
)
.join("\n")
func stringifySeq*[T](xs: seq[T], stringifyFn: (T) -> string, indent = 0): string =
let fieldIndent = indent + INDENT_SIZE
@[
"@[",
xs
.mapIt(it.stringifyFn())
.join(",\n")
.indent(fieldIndent),
"]",
]
.join("\n")