Richard Ashworth

Mar 31, 2019 1 min read

Interactive Kotlin with kscript and fswatch

Experimenting with snippets of code and getting rapid feedback is a good way to get up to speed with a new language or library. I’ve been using Kotlin and Λrrow at work this week, so have been looking for tools that help achieve this workflow.

Worksheets in IntelliJ provide some of this functionality, but personally I prefer the immediacy of a text editor and the command line for this kind of thing. It turns out that kscript adds rich functionality to Kotlin scripts (e.g. importing code from other files, jcenter artifacts, etc.), and by combining this with the fswatch tool, we can approximate the basic features of a worksheet without the need to spin up an entire project or IDE.

The following shell script (aliased to ktsrunner in the example below) will watch the current directory, running kscript <filename> on a file change:

fswatch -0 . |
  xargs -0 -n 1 -I {} sh -c "clear; echo '\033[0;32mRunning {}\033[0;37m'; echo; kscript {}"

An example of this in action:

comments powered by Disqus