defmodule Hull.Time.Progress do def print(enum, total \\ 0) do enum |> Stream.scan({nil, {now(), now(), 0}}, fn item, {_, {start, last, count}} -> next = count + 1 now = now() cond do now - last >= 1 -> diff = now - start IO.puts( "#{progress(count, total)} | Elapsed Time: #{diff}s #{remaining(count, total, diff)}" ) {item, {start, now, next}} true -> {item, {start, last, next}} end end) |> Stream.map(fn {item, _} -> item end) end defp progress(count, 0), do: "#{count} done" defp progress(count, total), do: "#{count} of #{total}" defp remaining(count, total, diff) when total != 0 and count != 0, do: "| #{round(diff / count * total - diff)}s remaining" defp remaining(_count, _total, _diff), do: "" defp now(), do: :os.system_time(:second) end