How to do CLI Tab Completion?

I want to write a CLI that does tab completion. For a comparable I studied IEx.Server.

It looks to me like this code takes input using IO.gets (on line 336). How does it trigger on the tab character? I can’t figure it out!

I think IO.gets traps the tab character when running under iex but not when running in elixir. Could that be right? Here’s a script test.exs that I used for testing:

#!/usr/bin/env elixir

IO.puts """
Try running this test two ways:
  > elixir text.exs
  > iex test.exs

`IO.gets` handles tab characters differently under elixir vs iex.  WHY??
"""

IO.gets("input prompt > ") |> IO.inspect(label: "RESULT")

How to write a CLI that has tab completion? Any tips appreciated!!

3 Likes