Skip to content

Commit

Permalink
Move Inspect implementation insdie Matrex module.
Browse files Browse the repository at this point in the history
  • Loading branch information
versilov committed May 9, 2018
1 parent 946f5f4 commit 5b193b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/matrex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ defmodule Matrex do
),
do: {:ok, columns}

defimpl Inspect do
def inspect(%Matrex{} = matrex, %{width: screen_width}),
do: Matrex.Inspect.do_inspect(matrex, screen_width)
end

@doc """
Adds two matrices
"""
Expand Down
17 changes: 12 additions & 5 deletions lib/matrex/inspect.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
defimpl Inspect, for: Matrex do
def inspect(
defmodule Matrex.Inspect do
def inspect(%Matrex{} = matrex) do
IO.puts(do_inspect(matrex))
matrex
end

def do_inspect(matrex, screen_width \\ 80)

def do_inspect(
%Matrex{
data: <<
rows::unsigned-integer-little-32,
columns::unsigned-integer-little-32,
_rest::binary
>>
} = matrex,
%{width: screen_width} = _opts
screen_width
)
when columns < screen_width / 8 and rows <= 21 do
rows_as_strings =
Expand All @@ -30,15 +37,15 @@ defimpl Inspect, for: Matrex do
@element_byte_size 4
@element_chars_size 8

def inspect(
def do_inspect(
%Matrex{
data: <<
rows::unsigned-integer-little-32,
columns::unsigned-integer-little-32,
body::binary
>>
} = matrex,
%{width: screen_width} = _opts
screen_width
)
when columns >= screen_width / 8 or rows > 21 do
suffix_size = prefix_size = div(screen_width, 16)
Expand Down

0 comments on commit 5b193b9

Please sign in to comment.