Default options for IO.inspect?

What is the preferred way to provide default options for IO.inspect? I am reading about Inspect.Opts but I can’t find a concrete example how to use it for existing things.

Also is there any way to add a file, something like the .iex.exs file with default options for the IO.inspect to pick up and use them?

My specific problem is that I would like to have colorized output for the inspected terms but I don’t want to add the options every time I call IO.inspect. I would prefer to create a file once and use it in every project.

I did create a ~/.iex.exs with this content for testing

IEx.configure(
  colors: [
    syntax_colors: [
      number: :light_yellow,
      atom: :light_cyan,
      string: :light_black,
      boolean: :red,
      nil: [:magenta, :bright]
    ],
    ls_directory: :cyan,
    ls_device: :yellow,
    doc_code: :green,
    doc_inline_code: :magenta,
    doc_headings: [:cyan, :underline],
    doc_title: [:cyan, :bright, :underline]
  ]
)

(taken from here)

but this has effect only in iex. When I run dev or test the output is still white on black.

I skimmed the code related to the inspect protocol and I can not find a way to set global defaults. You need to pass them explicitly all the time.

2 Likes

Thanks. I was googling for hours and thinking maybe there was a way documented somewhere and I somehow I couldn’t find it. It seems it isn’t so.

I would probably create a little helper module that you include in your projects, you could call it something short like I or IOP. Or I would create a snippet in my editor that includes the extra parameters I want to pass to IO.inspect

3 Likes

I’d be inclined to go with the helper module because if you ever need to change the ‘defaults’ you only need to edit the code in one place… the helper module.

4 Likes