Skip to content

Commit ab8b02b

Browse files
committed
Refactor live stream theme parsing
1 parent 5a89632 commit ab8b02b

File tree

5 files changed

+49
-43
lines changed

5 files changed

+49
-43
lines changed

lib/asciinema/colors.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ defmodule Asciinema.Colors do
1010
end
1111

1212
def hex(r, g, b), do: "##{hex(r)}#{hex(g)}#{hex(b)}"
13+
def hex({r, g, b}), do: hex(r, g, b)
1314
def hex([r, g, b]), do: hex(r, g, b)
1415

15-
def hex(int) do
16+
def hex(int) when is_integer(int) do
1617
int
1718
|> Integer.to_string(16)
1819
|> String.pad_leading(2, "0")

lib/asciinema/streaming/live_stream_server.ex

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Asciinema.Streaming.LiveStreamServer do
22
use GenServer, restart: :temporary
33
alias Asciinema.Recordings.Snapshot
44
alias Asciinema.Streaming.ViewerTracker
5-
alias Asciinema.{PubSub, Streaming, Vt}
5+
alias Asciinema.{Colors, PubSub, Streaming, Vt}
66
require Logger
77

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

298298
defp theme_fields(theme) do
299+
palette =
300+
theme.palette
301+
|> Enum.map(&Colors.hex/1)
302+
|> Enum.join(":")
303+
299304
[
300-
theme_fg: theme.fg,
301-
theme_bg: theme.bg,
302-
theme_palette: Enum.join(theme.palette, ":")
305+
theme_fg: Colors.hex(theme.fg),
306+
theme_bg: Colors.hex(theme.bg),
307+
theme_palette: palette
303308
]
304309
end
305310

lib/asciinema/streaming/parser/alis.ex

+23-30
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
defmodule Asciinema.Streaming.Parser.Alis do
2-
alias Asciinema.Colors
3-
42
@behaviour Asciinema.Streaming.Parser
53

6-
@theme_absent 0x00
7-
@theme_present 0x01
8-
94
def init, do: %{status: :new}
105

116
def parse({:binary, "ALiS\x01"}, %{status: :new} = state) do
@@ -24,38 +19,36 @@ defmodule Asciinema.Streaming.Parser.Alis do
2419
cols::little-16,
2520
rows::little-16,
2621
time::little-float-32,
27-
@theme_absent::8,
28-
init_len::little-32,
29-
init::binary-size(init_len)
22+
palette_len::8,
23+
rest::binary
3024
>>
3125
},
3226
%{status: status} = state
3327
)
3428
when status in [:init, :offline] do
35-
{:ok, [reset: %{size: {cols, rows}, init: init, time: time}], %{state | status: :online}}
36-
end
29+
palette_len =
30+
case palette_len do
31+
0 -> 0
32+
# TODO: legacy, used by RC CLIs, remove after release of final CLI 3.0
33+
1 -> 16
34+
8 -> 8
35+
16 -> 16
36+
end
3737

38-
def parse(
39-
{
40-
:binary,
41-
<<
42-
0x01,
43-
cols::little-16,
44-
rows::little-16,
45-
time::little-float-32,
46-
@theme_present::8,
47-
theme::binary-size(18 * 3),
48-
init_len::little-32,
49-
init::binary-size(init_len)
50-
>>
51-
},
52-
%{status: status} = state
53-
)
54-
when status in [:init, :offline] do
38+
theme_len = (2 + palette_len) * 3
39+
<<theme::binary-size(theme_len), init_len::little-32, init::binary-size(init_len)>> = rest
5540
theme = parse_theme(theme)
5641

57-
{:ok, [reset: %{size: {cols, rows}, init: init, time: time, theme: theme}],
58-
%{state | status: :online}}
42+
commands = [
43+
reset: %{
44+
size: {cols, rows},
45+
init: init,
46+
time: time,
47+
theme: theme
48+
}
49+
]
50+
51+
{:ok, commands, %{state | status: :online}}
5952
end
6053

6154
def parse(
@@ -97,7 +90,7 @@ defmodule Asciinema.Streaming.Parser.Alis do
9790
end
9891

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

10295
%{
10396
fg: Enum.at(colors, 0),

lib/asciinema/streaming/parser/json.ex

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Asciinema.Streaming.Parser.Json do
2+
alias Asciinema.Colors
3+
24
@behaviour Asciinema.Streaming.Parser
35

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

7173
defp parse_theme(%{"fg" => fg, "bg" => bg, "palette" => palette}) do
74+
palette =
75+
palette
76+
|> String.split(":")
77+
|> Enum.map(&Colors.parse/1)
78+
79+
true = length(palette) == 8 or length(palette) == 16
80+
7281
%{
73-
fg: fg,
74-
bg: bg,
75-
palette: String.split(palette, ":")
82+
fg: Colors.parse(fg),
83+
bg: Colors.parse(bg),
84+
palette: palette
7685
}
7786
end
7887
end

lib/asciinema_web/live_stream_consumer_socket.ex

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule AsciinemaWeb.LiveStreamConsumerSocket do
2-
alias Asciinema.{Accounts, Authorization, Colors, Streaming}
2+
alias Asciinema.{Accounts, Authorization, Streaming}
33
alias Asciinema.Streaming.{LiveStreamServer, ViewerTracker}
44
alias AsciinemaWeb.Endpoint
55
require Logger
@@ -184,7 +184,7 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do
184184
rows::little-16,
185185
time::little-float-32,
186186
theme_presence::8,
187-
theme::binary-size(18 * 3),
187+
theme::binary,
188188
init_len::little-32,
189189
init::binary
190190
>>
@@ -224,9 +224,7 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do
224224
end
225225

226226
defp encode_theme(%{fg: fg, bg: bg, palette: palette}) do
227-
for color <- [fg, bg | palette], into: <<>> do
228-
{r, g, b} = Colors.parse(color)
229-
227+
for {r, g, b} <- [fg, bg | palette], into: <<>> do
230228
<<r::8, g::8, b::8>>
231229
end
232230
end

0 commit comments

Comments
 (0)