

The picker, which is highly inspired by the select component in Nate Moore's clack library, presents a number of options and allows the user to select one using a combination of the arrow keys and the enter key. Package.swift // swift-tools-version: 5.7 import PackageDescription let package = Package ( name : "Picker", products : ) ], dependencies :, targets : ) ] ) Getting startedīefore we start writing some code, let's take a look at the picker component we'll be building in this article: These sequences are supported by terminals in Unix operating systems (like macOS) and Windows (only from Windows 10) and are used by many command-line tools to perform actions such as changing the text colour, modifying the cursor position, clearing the screen, etc.įor example, the following escape sequence changes the text colour to red: What are ANSI escape sequences?ĪNSI escape sequences are a way to perform operations in the terminal through a series of predefined characters. In fact, I like the library so much that I have started working on migrating it to Swift and I am going to document my progress in a series of posts.
#Ansi escape sequences cmd how to#
In this post, I will show you how to make an interactive picker for a command-line tool using ANSI escape sequences inspired by one of my favourite javascript libraries: clack by Nate Moore.

This sparked my interest in learning more about how such libraries work and investigating how hard it would be to make something similar using Swift.

I have been writing command-line tools with Swift for a while now and, while I find the developer experience to be great, I have always found the number of libraries to handle user input to be lacking compared to other languages such as Javascript. Release management by mobile engineers, for mobile engineers.
