diff options
Diffstat (limited to 'lib/chessh/ssh/client/trongle_chat.ex')
-rw-r--r-- | lib/chessh/ssh/client/trongle_chat.ex | 102 |
1 files changed, 70 insertions, 32 deletions
diff --git a/lib/chessh/ssh/client/trongle_chat.ex b/lib/chessh/ssh/client/trongle_chat.ex index 10df66d..4964965 100644 --- a/lib/chessh/ssh/client/trongle_chat.ex +++ b/lib/chessh/ssh/client/trongle_chat.ex @@ -3,6 +3,16 @@ defmodule Chessh.SSH.Client.TrongleChat do alias Chessh.{Player, Chat, Utils, Repo, PlayerSession} import Ecto.Query + @colors [ + IO.ANSI.light_blue(), + IO.ANSI.light_red(), + IO.ANSI.green(), + IO.ANSI.light_magenta(), + IO.ANSI.cyan(), + IO.ANSI.blue(), + IO.ANSI.yellow() + ] + defmodule State do defstruct client_pid: nil, message: "", @@ -26,36 +36,28 @@ defmodule Chessh.SSH.Client.TrongleChat do {:noreply, render(width, height, new_state)} end - defp get_initial_chats() do - from(c in Chat, - order_by: [desc: c.id], - limit: 100 - ) - |> Repo.all() - |> Repo.preload([:chatter]) - end - - defp get_player(%PlayerSession{player_id: player_id}), do: Repo.get!(Player, player_id) - - def init([%State{client_pid: client_pid, player_session: player_session} = state]) do + def init([ + %State{ + client_pid: client_pid, + player_session: %PlayerSession{player_id: player_id} = player_session + } = state + ]) do :syn.add_node_to_scopes([:chat]) :ok = :syn.join(:chat, {:tronglechat}, self()) send(client_pid, {:send_to_ssh, Utils.clear_codes()}) - chats = get_initial_chats() - {:ok, %State{ state - | chats: chats, - player_session: %PlayerSession{player_session | player: get_player(player_session)} + | chats: get_initial_chats(), + player_session: %PlayerSession{player_session | player: Repo.get!(Player, player_id)} }} end def render( - _width, - _height, + width, + height, %State{ client_pid: client_pid, chats: chats, @@ -64,38 +66,39 @@ defmodule Chessh.SSH.Client.TrongleChat do } = state ) do chat_msgs = - Enum.map(chats, fn %Chat{message: message, chatter: %Player{username: chat_username}} = - _chat -> - chat_username <> "> " <> message - end) + chats + |> Enum.slice(0, height - 1) + |> Enum.map(&format_chat/1) |> Enum.join("\r\n") - prompt = username <> "> " <> message + {prompt, prompt_len} = format_prompt(username, message) send( client_pid, {:send_to_ssh, [ Utils.clear_codes(), - prompt <> "\r\n" <> chat_msgs <> IO.ANSI.cursor(0, String.length(prompt)) + prompt <> + "\r\n" <> chat_msgs <> IO.ANSI.cursor(0, prompt_len + 1) ]} ) - state + %State{state | width: width, height: height} end def input( - width, - height, action, data, %State{ player_session: %PlayerSession{player: player}, - chats: chats, - message: message + message: message, + width: width, + height: height } = state ) do - appended_message = + safe_char_regex = ~r/[A-Za-z0-9._~()'!*:@,;+?-]/ + + appended_message_state = case action do :backspace -> %State{state | message: String.slice(message, 0..-2)} @@ -110,13 +113,48 @@ defmodule Chessh.SSH.Client.TrongleChat do end _ -> - if String.match?(data, ~r/[a-zA-Z \.!-]/) do + if String.match?(data, safe_char_regex) do %State{state | message: message <> data} else state end end - render(width, height, appended_message) + render(width, height, appended_message_state) + end + + defp get_initial_chats() do + from(c in Chat, + order_by: [desc: c.id], + limit: 100 + ) + |> Repo.all() + |> Repo.preload([:chatter]) + end + + defp username_color(username, colors \\ @colors) do + ind = + String.to_charlist(username) + |> Enum.sum() + |> rem(length(colors)) + + Enum.at(colors, ind) + end + + defp format_prompt(username, message) do + { + [ + IO.ANSI.format_fragment([username_color(username), username, IO.ANSI.default_color()]), + "> ", + message + ] + |> Enum.join(""), + String.length(username) + String.length(message) + 2 + } + end + + defp format_chat(%Chat{chatter: %Player{username: username}, message: message}) do + {prompt, _} = format_prompt(username, message) + prompt end end |