DEV Community

Sushant Bajracharya
Sushant Bajracharya

Posted on

Let IEx greet you

  • IEx is an interactive shell for elixir
  • It can be configured with .iex.exs file
  • .exs extension is for interpreted code, meaning you don't have to recompile it every time you make changes to it
  • IEx looks for the configuration file (.iex.exs) in your current working directory and then global (~/.iex.exs)

To let IEx greet you with Welcome to IEx <your username>, you have to create a .iex.exs file and add

import IO.ANSI, only: [green: 0, default_color: 0]

defmodule Helper do
  def camelize(<<first::utf8, rest::binary>>) do
    String.upcase(<<first::utf8>>) <> String.downcase(rest)
  end
end

{name, _} = System.cmd("whoami", [])

IO.puts("""

Welcome to IEx #{green()} #{Helper.camelize(name)} #{default_color()}
""")

To print to IEx, we can IO.puts but we need to get the username of the system, so we use System.cmd and pass in whoami shell command.

We want to upcase only the first letter of the word, so we will create a helper function called Helper.camelize/1. The function pattern matches the string using bitstring.

Now the username is camelized, I want it to have a color as well. We can use elixir's IO.ANSI lib for this, so we import it and only require it's two functions (green/0 and default_color/0).

We call green/0 before the username and call default_color/0 after it because we only want the username to stay green and not the whole iex shell.

Top comments (0)