-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from mtrudel/master
Reintroduce ASCII rendering
- Loading branch information
Showing
2 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |