Commit fb937b397412adf46b34804595365a07753ece2d

Thomas de Grivel 2022-02-13T12:26:03

wrap input lines

diff --git a/lib/exterm.ex b/lib/exterm.ex
index 0968767..7444b0f 100644
--- a/lib/exterm.ex
+++ b/lib/exterm.ex
@@ -146,19 +146,42 @@ defmodule Exterm do
     :gl.clear(:gl_const.gl_color_buffer_bit)
     :gl.loadIdentity()
     lines
+    |> wrap(80, 25)
     |> Enum.with_index()
     |> Enum.each(fn {line, i} ->
       :gl.pushMatrix()
       line
-      |> Enum.map(fn c when is_integer(c) ->
+      |> Enum.map(fn c ->
         Exterm.Font.load_glyph(font.regular, c)
       end)
       |> :gl.callLists()
       :gl.popMatrix()
-      :gl.translatef(0.0, 50.0, 0.0)
+      :gl.translatef(0.0, 35.0, 0.0)
     end)
   end
 
+  def wrap(input, w, h) when is_list(input) do
+    input
+    |> Enum.slice(0..h)
+    |> Enum.reverse()
+    |> wrap(w, [], 0, [])
+    |> Enum.slice(0..(h - 1))
+  end
+  def wrap([], _w, lines, _, _) do
+    lines
+  end
+  def wrap([[] | rest], w, lines, x, acc) do
+    line = acc |> Enum.reverse()
+    wrap(rest, w, [line | lines], 0, [])
+  end
+  def wrap(input, w, lines, x, acc) when x >= w do
+    line = acc |> Enum.reverse()
+    wrap(input, w, [line | lines], 0, [])
+  end
+  def wrap([[c | rest] | rest1], w, lines, x, acc) do
+    wrap([rest | rest1], w, lines, x + 1, [c | acc])
+  end
+
   defp render(%{canvas: canvas, font: font, lines: lines} = _state) do
     draw(font, lines)
     :wxGLCanvas.swapBuffers(canvas)