627 B · text History 2b51ebb
defmodule GitGud.Workflows.LogLine do
  @moduledoc """
  A log line as one plain screen line: no colour codes, no
  carriage-return progress bars (the last frame wins), nothing a CI wall
  card can't show. Shared by the web wall and the mobile API's, so a TV
  and a browser show a job the same way.
  """

  @max 400
  @ansi ~r/\e\[[0-9;?]*[ -\/]*[@-~]|\e\][^\a]*\a/

  @spec plain(term()) :: String.t()
  def plain(body) when is_binary(body) do
    body
    |> String.replace(@ansi, "")
    |> String.split("\r")
    |> List.last()
    |> String.trim_trailing()
    |> String.slice(0, @max)
  end

  def plain(_), do: ""
end