Files
org-parser/src_v2/utils/printers.nim
Florian Schroedl 892eec10d5 Started refactor
2022-08-09 22:12:50 +02:00

45 lines
783 B
Nim

import std/[
options,
sequtils,
strformat,
strutils,
sugar,
]
const INDENT_SIZE* = 2;
func stringifyFields*(
xs: seq[tuple[
field: string,
value: string,
print: bool
]],
indent = 0,
): string =
xs
.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")