Nim is so flexible!
I recently was doing some text processing and decided to try awk, which really does amazingly well in its domain, ie taking some tabular data and crunching it and putting out some kind of text format.
I had tried to do it in Nim first but there was too much boilerplate- that is, until I realized you can bolt strformat onto stdtmpl without bothering to use a proc, and have something similarly concise to awk scripts. This script expects TSV input with two columns and left-aligns the first, right aligns the second, creating a markdown table.
#? stdtmpl(emit="stdout.write &")
# import strformat, streams, parsecsv
# var x: CsvParser
# open(x, newFileStream(stdin), "-", '\t')
My Report
=========
{"":-<20} {"":-<20}
# while x.readRow():
{x.row[0]:<20} {x.row[1]:>20}
# end
{"":-<20} {"":-<20}
foo\t1
bar\t2
fuz\t3
buz}t4
It is also a very nice, self-contained and general enough example that could be added to source code filters documentation. All other examples start with a proc and are used through an include. This actually makes it clear that Source code filters can be standalone and used for scripting. It definitely improved my knowledge on what Source Code Filters are!
Thanks for sharing!
etc...
If you have boilerplate, just use templates+macros, no need to bake in a sublanguage into nim. SCF is a misfeature that needs to be deprecated.
import strformat, streams, parsecsv
var x: CsvParser
open(x, newFileStream(stdin), "-", '\t')
template p(a: string) = echo &(a)
p """
My Report
=========
{"":-<20} {"":-<20}"""
while x.readRow():
p """{x.row[0]:<20} {x.row[1]:>20}"""
p """{"":-<20} {"":-<20}"""