Search code examples
elixirelixir-iex

Terminal friendly output for IEx.Helper.h while outputting to something not TTY


Following this Elixir Forum post, I created a shell script to view Elixir documentation by running IEx.Helpers.h:

elixir -e "require IEx.Helpers; IEx.Helpers.h($1)"

(where $1 is the command line argument to the script). (For example, the command

elixir -e "require IEx.Helpers; IEx.Helpers.h(List)"

gives documentation for the module List.)

This is super nice. No need to run iex to view documentation anymore. Default formatting for terminal works nice and beautifully. But if I pipe the output to pager (or more generally, if the output is not a TTY device), the formatting changes - plain ASCII output without ANSI colors.

Question: how do I configure or direct IEx.Helper.h to provide identical output in the non-TTY case?

(It looks like there is function IEx.configure that can be used to change the formatting, but I could not see anything about non-TTY output. Another way would be to use some suitable miniscule external program to create a pseudo-TTY, redirecting stdout to that before running the elixir commmand... but I would prefer solving the output problem from Elixir side.)


Solution

  • One might enforce colors no matter what output device is used by fooling IEx.Config.colors_enabled?/1 not to use a sophisticated check with IO.ANSI.enabled?/0:

    elixir -e "
      Application.put_env(:iex, :colors, [enabled: true]);
      require IEx.Helpers;
      IEx.Helpers.h(List)"