Skip to content

Commit

Permalink
Refactor live stream theme parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jan 10, 2025
1 parent 5a89632 commit ab8b02b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
3 changes: 2 additions & 1 deletion lib/asciinema/colors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ defmodule Asciinema.Colors do
end

def hex(r, g, b), do: "##{hex(r)}#{hex(g)}#{hex(b)}"
def hex({r, g, b}), do: hex(r, g, b)
def hex([r, g, b]), do: hex(r, g, b)

def hex(int) do
def hex(int) when is_integer(int) do
int
|> Integer.to_string(16)
|> String.pad_leading(2, "0")
Expand Down
13 changes: 9 additions & 4 deletions lib/asciinema/streaming/live_stream_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Asciinema.Streaming.LiveStreamServer do
use GenServer, restart: :temporary
alias Asciinema.Recordings.Snapshot
alias Asciinema.Streaming.ViewerTracker
alias Asciinema.{PubSub, Streaming, Vt}
alias Asciinema.{Colors, PubSub, Streaming, Vt}
require Logger

defmodule Update do
Expand Down Expand Up @@ -296,10 +296,15 @@ defmodule Asciinema.Streaming.LiveStreamServer do
defp theme_fields(nil), do: [theme_fg: nil, theme_bg: nil, theme_palette: nil]

defp theme_fields(theme) do
palette =
theme.palette
|> Enum.map(&Colors.hex/1)
|> Enum.join(":")

[
theme_fg: theme.fg,
theme_bg: theme.bg,
theme_palette: Enum.join(theme.palette, ":")
theme_fg: Colors.hex(theme.fg),
theme_bg: Colors.hex(theme.bg),
theme_palette: palette
]
end

Expand Down
53 changes: 23 additions & 30 deletions lib/asciinema/streaming/parser/alis.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
defmodule Asciinema.Streaming.Parser.Alis do
alias Asciinema.Colors

@behaviour Asciinema.Streaming.Parser

@theme_absent 0x00
@theme_present 0x01

def init, do: %{status: :new}

def parse({:binary, "ALiS\x01"}, %{status: :new} = state) do
Expand All @@ -24,38 +19,36 @@ defmodule Asciinema.Streaming.Parser.Alis do
cols::little-16,
rows::little-16,
time::little-float-32,
@theme_absent::8,
init_len::little-32,
init::binary-size(init_len)
palette_len::8,
rest::binary
>>
},
%{status: status} = state
)
when status in [:init, :offline] do
{:ok, [reset: %{size: {cols, rows}, init: init, time: time}], %{state | status: :online}}
end
palette_len =
case palette_len do
0 -> 0
# TODO: legacy, used by RC CLIs, remove after release of final CLI 3.0
1 -> 16
8 -> 8
16 -> 16
end

def parse(
{
:binary,
<<
0x01,
cols::little-16,
rows::little-16,
time::little-float-32,
@theme_present::8,
theme::binary-size(18 * 3),
init_len::little-32,
init::binary-size(init_len)
>>
},
%{status: status} = state
)
when status in [:init, :offline] do
theme_len = (2 + palette_len) * 3
<<theme::binary-size(theme_len), init_len::little-32, init::binary-size(init_len)>> = rest
theme = parse_theme(theme)

{:ok, [reset: %{size: {cols, rows}, init: init, time: time, theme: theme}],
%{state | status: :online}}
commands = [
reset: %{
size: {cols, rows},
init: init,
time: time,
theme: theme
}
]

{:ok, commands, %{state | status: :online}}
end

def parse(
Expand Down Expand Up @@ -97,7 +90,7 @@ defmodule Asciinema.Streaming.Parser.Alis do
end

defp parse_theme(theme) do
colors = for <<r::8, g::8, b::8 <- theme>>, do: Colors.hex(r, g, b)
colors = for <<r::8, g::8, b::8 <- theme>>, do: {r, g, b}

%{
fg: Enum.at(colors, 0),
Expand Down
15 changes: 12 additions & 3 deletions lib/asciinema/streaming/parser/json.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Asciinema.Streaming.Parser.Json do
alias Asciinema.Colors

@behaviour Asciinema.Streaming.Parser

def init, do: %{first: true}
Expand Down Expand Up @@ -69,10 +71,17 @@ defmodule Asciinema.Streaming.Parser.Json do
defp parse_theme(nil), do: nil

defp parse_theme(%{"fg" => fg, "bg" => bg, "palette" => palette}) do
palette =
palette
|> String.split(":")
|> Enum.map(&Colors.parse/1)

true = length(palette) == 8 or length(palette) == 16

%{
fg: fg,
bg: bg,
palette: String.split(palette, ":")
fg: Colors.parse(fg),
bg: Colors.parse(bg),
palette: palette
}
end
end
8 changes: 3 additions & 5 deletions lib/asciinema_web/live_stream_consumer_socket.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule AsciinemaWeb.LiveStreamConsumerSocket do
alias Asciinema.{Accounts, Authorization, Colors, Streaming}
alias Asciinema.{Accounts, Authorization, Streaming}
alias Asciinema.Streaming.{LiveStreamServer, ViewerTracker}
alias AsciinemaWeb.Endpoint
require Logger
Expand Down Expand Up @@ -184,7 +184,7 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do
rows::little-16,
time::little-float-32,
theme_presence::8,
theme::binary-size(18 * 3),
theme::binary,
init_len::little-32,
init::binary
>>
Expand Down Expand Up @@ -224,9 +224,7 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do
end

defp encode_theme(%{fg: fg, bg: bg, palette: palette}) do
for color <- [fg, bg | palette], into: <<>> do
{r, g, b} = Colors.parse(color)

for {r, g, b} <- [fg, bg | palette], into: <<>> do
<<r::8, g::8, b::8>>
end
end
Expand Down

0 comments on commit ab8b02b

Please sign in to comment.