diff options
author | Alex Auvolat <alex@adnab.me> | 2018-07-04 13:17:26 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-07-04 13:27:36 +0200 |
commit | b234a360dafa0a65d797614aad5a4514570784f8 (patch) | |
tree | c636e7398fc8f3aba43b40c2fe67fc8264c56374 /lib/cli | |
parent | ece5faa69da724ddfdee7b92016bac73dbdbff70 (diff) | |
download | shard-b234a360dafa0a65d797614aad5a4514570784f8.tar.gz shard-b234a360dafa0a65d797614aad5a4514570784f8.zip |
Multiple chat rooms (no web UI yet)
Diffstat (limited to 'lib/cli')
-rw-r--r-- | lib/cli/cli.ex | 56 |
1 files changed, 48 insertions, 8 deletions
diff --git a/lib/cli/cli.ex b/lib/cli/cli.ex index 28ef5d0..0402b3f 100644 --- a/lib/cli/cli.ex +++ b/lib/cli/cli.ex @@ -1,30 +1,70 @@ defmodule SCLI do def run() do - str = "say: " |> IO.gets |> String.trim + 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(" ") - handle_command(command) - run() + pid2 = handle_command(pid, command) + run(pid2) true -> - SApp.Chat.send(str) - run() + GenServer.cast(pid, {:chat_send, str}) + run(pid) end end - def handle_command(["connect", ipstr, portstr]) do + def handle_command(pid, ["connect", ipstr, portstr]) do {:ok, ip} = :inet.parse_address (to_charlist ipstr) {port, _} = Integer.parse portstr SNet.Manager.add_peer(ip, port) + pid + end + + def handle_command(pid, ["list"]) do + IO.puts "List of known channels:" + list = GenServer.call(Shard.Manager, :list) + for {_chid, chpid} <- list do + {:chat, chan} = GenServer.call(chpid, :manifest) + IO.puts "##{chan}" + end + pid + end + + def handle_command(pid, ["join", qchan]) do + list = GenServer.call(Shard.Manager, :list) + list = for {_chid, chpid} <- list, + {:chat, chan} = GenServer.call(chpid, :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 - def handle_command(["nick", nick]) do + def handle_command(pid, ["nick", nick]) do Shard.Identity.set_nickname nick + pid end - def handle_command(_cmd) do + def handle_command(pid, _cmd) do IO.puts "Invalid command" + pid end end |