Building an user friendly CLI UI

I’ve realized Command lines don’t have to be difficult

Have you ever been able to play with a command line application from moment zero? I bet not. You probably have to rely in things like --help and man just like I do. Well, for Coltrane, not anymore.

Coltrane is a library I created to make it easier querying for musical concepts. It abstracts you from the math computations that music requires us. Why not abstract command-line users from complexness of command line syntax as well?

Here’s how it works:

Notice we don’t lose the classic functionality of CLIs. You’re still ableto type in exactly what you want and jump all these menus right away. And Coltrane is nice enough to print you what you would have to type to reach current state. For example:

$ coltrane show chord chord:Cm7 chord_representation:Text

Just like URLs :-)

And the comparisons to the web won’t stop there. So let’s dive into the architecture of the new Coltrane CLI.

When you type coltrane you will eventually fall into this line:

response = {path: (path || ''), **params}
loop { response = app.flow(**response) }

Essentially, #flow sends a request to the Router, which will instantiate the appropriate view, which will regenerate a response back. Then we call the same method again using this response.

That was the turning point!

We use the same response as request, creating a non-stop flow.

Each view may have a list of questions that must be answered before it’s rendered:

If the params don’t include answers to these questions, it sends back to the App, which will render the questions using the amazing Shopify’s CLI/UI gem:

Questions are then converted to params and the request is done again.

But here is a trick: If the question is related to path: , the router will actually use that answer to trigger a different view. That way, views can actually serve as redirection points to other views:

This system allows us to plug anything instead of a CLI, as long as it’s ready the present the user questions and provide the views params (aka answers). Maybe we can have one day a Telegram bot?

That’s it for now. I may cover more about this subject in the future. If you like this article or have any questions, please let me know in the comments.