Generating of snapshot from stdout stream

ex-snapshot
Marcin Kulik 7 years ago
parent c9a3bf044f
commit c5fd094b91

@ -1,6 +1,6 @@
defmodule Asciinema.Asciicasts do
import Ecto.Query, warn: false
alias Asciinema.{Repo, Asciicast, FileStore, StringUtils}
alias Asciinema.{Repo, Asciicast, FileStore, StringUtils, Vt}
alias Asciinema.Asciicasts.PosterGenerator
def get_asciicast!(id) when is_integer(id) do
@ -172,4 +172,26 @@ defmodule Asciinema.Asciicasts do
[delay_s, bytes_s] = line |> String.trim_trailing |> String.split(" ")
{String.to_float(delay_s), String.to_integer(bytes_s)}
end
def generate_snapshot(stdout_stream, width, height, secs) do
frames =
stdout_stream
|> Stream.scan(&to_absolute_time/2)
|> Stream.take_while(&frame_before_or_at?(&1, secs))
{:ok, %{"lines" => lines}} = Vt.with_vt(width, height, fn vt ->
Enum.each(frames, fn {_, text} -> Vt.feed(vt, text) end)
Vt.dump_screen(vt)
end)
lines
end
defp to_absolute_time({curr_time, data}, {prev_time, _}) do
{prev_time + curr_time, data}
end
defp frame_before_or_at?({time, _}, secs) do
time <= secs
end
end

@ -139,4 +139,12 @@ defmodule Asciinema.AsciicastsTest do
assert [{1.234567, "xxżó"}, {0.123456, "łć"}, {2.0, "xx"}] == Enum.take(stream, 3)
end
end
describe "generate_snapshot/2" do
test "returns list of screen lines" do
stdout_stream = [{1.0, "a"}, {0.5, "b"}, {2.0, "c"}]
snapshot = Asciicasts.generate_snapshot(stdout_stream, 4, 2, 2.5)
assert snapshot == [[["ab ", %{}]], [[" ", %{}]]]
end
end
end

Loading…
Cancel
Save