From a33dce136e66ba0db89e5354426131d90e604d42 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Wed, 4 May 2022 17:00:00 +0200 Subject: [PATCH] Extract stringifying functions --- src/org/org_types.nim | 26 ++++++++++---------------- src/utils/printers.nim | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 src/utils/printers.nim diff --git a/src/org/org_types.nim b/src/org/org_types.nim index 56d3697..510280c 100644 --- a/src/org/org_types.nim +++ b/src/org/org_types.nim @@ -8,6 +8,7 @@ import std/[ import fp/[ maybe, ] +import ../utils/printers ## Inline Block type @@ -102,22 +103,15 @@ type const INDENT_SIZE = 2; func stringifySpecialFields(x: OrgBlock, indent = 0): string = - ( - case x.kind: - of orgHeading: - @[ - ("kind", $x.kind, true), - ("level", $x.level, true), - ("todo", $x.todo, x.todo.isSome()), - ("headlineContent", $x.headlineContent, x.headlineContent.len != 0), - ("headlineChildrenText", $x.headlineChildrenText, x.headlineChildrenText.len != 0), - ("children", $x.children, true), - ] - else: @[]) - - .filter(x => x[2]) - .map(x => x[0] & ": " & x[1]) - .join(",\n") + @[ + ("kind", $x.kind, true), + ("level", $x.level, true), + ("todo", $x.todo, x.todo.isSome()), + ("headlineContent", $x.headlineContent, x.headlineContent.len != 0), + ("headlineChildrenText", $x.headlineChildrenText, x.headlineChildrenText.len != 0), + ("children", $x.children, true), + ] + .stringifyFields() proc `$`*(x: OrgBlock, indent = 0): string = let fieldIndent = indent + INDENT_SIZE diff --git a/src/utils/printers.nim b/src/utils/printers.nim new file mode 100644 index 0000000..92a02a4 --- /dev/null +++ b/src/utils/printers.nim @@ -0,0 +1,22 @@ +import std/[ + sequtils, + 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")