defmodule SCLI do @moduledoc """ Small command line interface for the chat application """ def run() do run(nil) end defp run(pid) do nick = Shard.Identity.get_nickname prompt = case pid do nil -> "(no channel) #{nick}: " _ -> {:chat, chan} = GenServer.call(pid, :manifest) "##{chan} #{nick}: " end str = prompt |> IO.gets |> String.trim cond do str == "/quit" -> nil String.slice(str, 0..0) == "/" -> command = str |> String.slice(1..-1) |> String.split(" ") pid2 = handle_command(pid, command) run(pid2) true -> GenServer.cast(pid, {:chat_send, str}) run(pid) end end defp handle_command(pid, ["connect", ipstr, portstr]) do {:ok, ip} = :inet.parse_address (to_charlist ipstr) {port, _} = Integer.parse portstr Shard.Manager.add_peer(ip, port) pid end defp handle_command(pid, ["list"]) do IO.puts "List of known channels:" for {_chid, manifest, _chpid} <- :ets.tab2list(:shard_db) do {:chat, chan} = manifest IO.puts "##{chan}" end pid end defp handle_command(pid, ["join", qchan]) do list = for {_chid, manifest, chpid} <- :ets.tab2list(:shard_db), {:chat, chan} = manifest, do: {chan, chpid} case List.keyfind(list, qchan, 0) do nil -> {:ok, pid} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, qchan}) pid {_, pid} -> IO.puts "Switching to ##{qchan}" pid end end defp handle_command(pid, ["nick", nick]) do Shard.Identity.set_nickname nick pid end defp handle_command(pid, _cmd) do IO.puts "Invalid command" pid end end