Skip to content

Commit

Permalink
Merge pull request #11 from mtrudel/master
Browse files Browse the repository at this point in the history
Reintroduce ASCII rendering
  • Loading branch information
nthock authored Dec 15, 2019
2 parents c208e89 + a2763fa commit 1f547e5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/eqrcode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ defmodule EQRCode do
"""
defdelegate svg(matrix, options \\ []), to: EQRCode.SVG


@doc """
```elixir
qr_code_content
Expand All @@ -103,4 +102,13 @@ defmodule EQRCode do
By default, QR code size will be dynamically generated based on the input string.
"""
defdelegate png(matrix, options \\ []), to: EQRCode.PNG

@doc """
```elixir
qr_code_content
|> EQRCode.encode()
|> EQRCode.render()
```
"""
defdelegate render(matrix), to: EQRCode.Render
end
46 changes: 46 additions & 0 deletions lib/eqrcode/render.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule EQRCode.Render do
@moduledoc """
Render the QR Code matrix.
Taken essentially verbatim from https://github.com/sunboshan/qrcode
"""

@doc """
Render the QR Code to terminal.
"""
@spec render(EQRCode.Matrix.t()) :: :ok
def render(%EQRCode.Matrix{matrix: matrix}) do
Tuple.to_list(matrix)
|> Stream.map(fn e ->
Tuple.to_list(e)
|> Enum.map(&do_render/1)
end)
|> Enum.intersperse("\n")
|> IO.puts()
end

defp do_render(1), do: "\e[40m \e[0m"
defp do_render(0), do: "\e[0;107m \e[0m"
defp do_render(nil), do: "\e[0;106m \e[0m"
defp do_render(:data), do: "\e[0;102m \e[0m"
defp do_render(:reserved), do: "\e[0;104m \e[0m"

@doc """
Rotate the QR Code 90 degree clockwise and render to terminal.
"""
@spec render2(EQRCode.Matrix.t()) :: :ok
def render2(%EQRCode.Matrix{matrix: matrix}) do
for(e <- Tuple.to_list(matrix), do: Tuple.to_list(e))
|> Enum.reverse()
|> transform()
|> Stream.map(fn e ->
Enum.map(e, &do_render/1)
end)
|> Enum.intersperse("\n")
|> IO.puts()
end

defp transform(matrix) do
for e <- Enum.zip(matrix), do: Tuple.to_list(e)
end
end

0 comments on commit 1f547e5

Please sign in to comment.