summaryrefslogtreecommitdiff
path: root/lib/chessh/utils.ex
blob: 20a8b96fb70f844f9394b2d500bb0e7d54b9ac64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule Chessh.Utils do
  @ascii_chars Application.compile_env!(:chessh, :ascii_chars_json_file)
               |> File.read!()
               |> Jason.decode!()

  @clear_codes [
    IO.ANSI.clear(),
    IO.ANSI.home()
  ]

  def ascii_chars(), do: @ascii_chars
  def clear_codes(), do: @clear_codes

  def center_rect({rect_width, rect_height}, {parent_width, parent_height}) do
    {
      div(parent_height - rect_height, 2),
      div(parent_width - rect_width, 2)
    }
  end

  def pid_to_str(pid) do
    pid
    |> :erlang.pid_to_list()
    |> List.delete_at(0)
    |> List.delete_at(-1)
    |> to_string()
  end

  def text_dim(text) do
    split = String.split(text, "\n")
    {Enum.reduce(split, 0, fn x, acc -> max(acc, String.length(x)) end), length(split)}
  end

  def wrap_around(index, delta, length) do
    calc = index + delta
    if(calc < 0, do: length, else: 0) + rem(calc, length)
  end
end