Writing scripts in Janet

· dghaehre


Scripting with janet #

Writing small scripts for my own use has always been fun, but I have always felt like I wanted a different language than bash or python. And now I have found it, Janet!

I have been using taskwarrior for a while, and with that comes small scripts and tweaks to improve my setup. Most of those scripts are oneliners in bash which is fine, that is what bash is good for. But sometimes a need a little bit more control.

My last tiny problem is that I needed to map a string of the following format "bar,foo" to "+bar +foo". I actually dont know how I would do that in bash, but it would for sure not be as fun as doing this in Janet.

 1#!/usr/bin/env janet
 2
 3(defn fromInput [args]
 4  "Either return stream from stdin or first argument given"
 5  (if (= (length args) 1)
 6    (do
 7      (def buf @"")
 8      (file/read stdin :line buf)
 9      buf)
10    (get args 1)))
11
12(defn main [& args]
13  (->>
14    (fromInput args)
15    (string/replace "\n" "")
16    (string/split ",")
17    (filter |(not= "" $0))
18    (map |(string "+" $0))
19    (|(string/join $0 " "))
20    (prin)))