ANSI color codes in compiled CLI

Viewed 123

I'm building a CLI utility in Elixir, and am putting a little extra effort into the UX because it will probably see a lot of reuse.

When I include lines like IO.puts(IO.ANSI.format([:yellow,"some output"])) and run the app by using iex -S mix and invoking the entry point directly, everything works as expected.

However, when I build the app using mix escript.build and then invoke it with ./{the_app_name}, the app runs just fine, but the ANSI codes are apparently stripped, since the colors are all gone.

Obviously not an absolutely critical issue, but it would go a long way to enhancing the usability of the app.

1 Answers

As it can be seen from the code, IO.ANSI.enabled?/0 is false by default. I believe (I have not tested it though,) that explicitly enabling it would do a trick.

Application.put_env(:elixir, :ansi_enabled, true)

[:yellow, "some output"]
|> IO.ANSI.format()
|> IO.puts()
Related