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

@@ -10,7 +10,8 @@ import fp/[
] ]
import ../utils/printers import ../utils/printers
## Inline Block ## OrgInlineBlock.Type
type type
orgInlineBlockKind* = enum orgInlineBlockKind* = enum
orgRawText, orgRawText,
@@ -26,6 +27,7 @@ type
# Links # Links
orgLink, orgLink,
OrgInlineBlock* = ref object OrgInlineBlock* = ref object
children*: seq[OrgInlineBlock] children*: seq[OrgInlineBlock]
content*: string content*: string
@@ -47,6 +49,8 @@ type
linkUrl*: string linkUrl*: string
linkDescription*: Maybe[string] linkDescription*: Maybe[string]
## OrgInlineBlock.PrettyPrinters
proc `$`*(x: orgInlineBlockKind): string = proc `$`*(x: orgInlineBlockKind): string =
case x: case x:
of orgRawText: "Text (Raw)" of orgRawText: "Text (Raw)"
@@ -63,30 +67,36 @@ proc `$`*(x: orgInlineBlockKind): string =
# Links # Links
of orgLink: "Link" of orgLink: "Link"
func stringifySpecialFields(x: OrgInlineBlock): string = func pprint*(x: OrgInlineBlock, indent = 0): string =
let specialFields = case x.kind: let specialFields = case x.kind:
of orgLink: of orgLink:
&"""linkUrl: "{x.linkUrl}"""" & @[
x.linkDescription.map(y => &"\nlinkValue: \"{y}\"").getOrElse("") ("linkUrl", $x.linkUrl, true),
else: "" ("linkDescription", x.linkDescription.getOrElse(""), x.linkDescription.isSome()),
]
else: @[]
specialFields let fields = @[
.just() ("kind", $x.kind, true),
.notEmpty() ("content", $x.content, x.content.len != 0),
.map(x => x.indent(2)) ]
.map(x => "\n" & x & "\n") .concat(specialFields)
.getOrElse("") .stringifyFields()
proc `$`*(x: OrgInlineBlock): string = stringifyBlock(
&"""OrgInlineBlock( "OrgInlineBlock",
content: "{x.content}" indent,
kind: {x.kind}""" & fields,
stringifySpecialFields(x) & )
"""
)"""
## OrgBlock func `$`*(x: OrgInlineBlock): string = pprint(x)
## --------
func pprint*(xs: seq[OrgInlineBlock], indent = 0): string =
stringifySeq(xs, (x: OrgInlineBlock) => pprint(x, indent), indent)
func `$`*(xs: seq[OrgInlineBlock]): string = pprint(xs)
## OrgBlock.Type
type type
orgBlockKind* = enum orgBlockKind* = enum
@@ -102,6 +112,8 @@ type
headlineContent*: seq[OrgInlineBlock] headlineContent*: seq[OrgInlineBlock]
headlineChildrenText*: string headlineChildrenText*: string
## OrgBlock.PrettyPrinters
func pprint*(x: OrgBlock, indent = 0): string = func pprint*(x: OrgBlock, indent = 0): string =
let fields = @[ let fields = @[
("kind", $x.kind, true), ("kind", $x.kind, true),
@@ -109,7 +121,7 @@ func pprint*(x: OrgBlock, indent = 0): string =
("todo", $x.todo, x.todo.isSome()), ("todo", $x.todo, x.todo.isSome()),
("headlineContent", $x.headlineContent, x.headlineContent.len != 0), ("headlineContent", $x.headlineContent, x.headlineContent.len != 0),
("headlineChildrenText", $x.headlineChildrenText, x.headlineChildrenText.len != 0), ("headlineChildrenText", $x.headlineChildrenText, x.headlineChildrenText.len != 0),
("children", $x.children, true), ("children", $x.children, x.children.len != 0),
] ]
.stringifyFields() .stringifyFields()
@@ -122,22 +134,11 @@ func pprint*(x: OrgBlock, indent = 0): string =
func `$`*(x: OrgBlock): string = pprint(x) func `$`*(x: OrgBlock): string = pprint(x)
func pprint*(xs: seq[OrgBlock], indent = 0): string = func pprint*(xs: seq[OrgBlock], indent = 0): string =
let fieldIndent = indent + INDENT_SIZE stringifySeq(xs, (x: OrgBlock) => pprint(x, indent), indent)
@[
"@[",
xs
.mapIt(it.pprint())
.join(",\n")
.indent(fieldIndent),
"]",
]
.join("\n")
func `$`*(xs: seq[OrgBlock]): string = pprint(xs) func `$`*(xs: seq[OrgBlock]): string = pprint(xs)
## OrgDocuemnt ## OrgDocument.Type
## -----------
type type
OrgDocument* = ref object OrgDocument* = ref object
@@ -155,12 +156,23 @@ func pprint*(x: OrgDocument, indent = 0): string =
fields, fields,
) )
## OrgDocument.PrettyPrinters
func `$`*(xs: OrgDocument): string = pprint(xs) func `$`*(xs: OrgDocument): string = pprint(xs)
when isMainModule: when isMainModule:
echo OrgDocument( echo OrgDocument(
children: @[ children: @[
OrgBlock(kind: orgHeading), OrgBlock(
kind: orgHeading,
children: @[
OrgInlineBlock(
kind: orgLink,
linkUrl: "Foo",
linkDescription: "Bar".just(),
),
],
),
OrgBlock(kind: orgHeading), OrgBlock(kind: orgHeading),
OrgBlock(kind: orgHeading), OrgBlock(kind: orgHeading),
] ]

View File

@@ -1,5 +1,5 @@
import std/[ import std/[
sequtils, options,
sequtils, sequtils,
strformat, strformat,
strutils, strutils,
@@ -20,3 +20,25 @@ func stringifyFields*(
.filter(x => x.print) .filter(x => x.print)
.map(x => x.field & ": " & $x.value) .map(x => x.field & ": " & $x.value)
.join(",\n") .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")