Pretty - inspect values with syntax colors despite your remote console

Pretty addresses two surprises you’ll encounter trying to dump data to the remote console
like you did during development with iex -S mix:

  • IO.inspect/1 et al work fine at the iex> prompt but somehow not when called from a :telemetry handler function or other troubleshooting mechanism… unless you think to look at the log output

  • The syntax colors aren’t working like you think they should, either

  • The inspection width is 80… just like iex, now that you think of it

You don’t need Pretty to deal with this—it’s enough to know to call Process.group_leader/0 to get a device to hand to IO.puts/2, for example—but by the time you solve all three problems it’s chunky enough you might plausibly add a dependency rather than paste a snippet:

bind = fn opts ->
  device = Process.group_leader()
  width = with {:ok, n} <- :io.columns(device), do: n, else: (_ -> %Inspect.Opts{}.width)
  opts = Keyword.merge(, IEx.Config, :inspect_opts, []), opts)
  opts = Keyword.merge(opts, pretty: true, width: width)
  reset = Enum.find_value(Keyword.get(opts, :syntax_colors, []), [], fn _ -> IO.ANSI.reset() end)
  fn term -> IO.puts(device, [Kernel.inspect(term, opts), reset]); term end

If nothing else, y’all might enjoy the explanation of what’s going on.


Pretty good, I’ll use that, thank you.