I wanted to experiment on writing command line tools with Swift, I wrote a simple command-line pomodoro timer. I had to find answers to quite a number of questions to achieve this, but Internet is great at finding answers to things. So here we go.
I had never written a progress bar on the command line before and this StackOverflow question was of great help to understand how they work.
To get started, I recommend this Swift Talk #22 - Command Line Tools with Swift video (a little over 17 minutes long). As I had trouble setting up dependencies right (the video is getting old and Swift changed in the meantime), this post about the Package Manager on Swift’s website, was also pretty useful to get a working Xcode project with the Commander dependency setup right.
Basically, everything you need to type in the terminal is:
swift package init --type executable swift package update # After setting up dependencies in `Package.swift` swift package generate-xcodeproj swift build .build/debug/<name-of-your-target>
print() is the easy answer by it doesn’t manage escaping characters right. So
FileHandle.standardOutput was what I was looking for to manage a proper terminal progress bar.
Try it here.