/
asciicast_view.ex
468 lines (385 loc) · 10.8 KB
/
asciicast_view.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
defmodule AsciinemaWeb.AsciicastView do
use AsciinemaWeb, :view
import Scrivener.HTML
alias Asciinema.Asciicasts
alias Asciinema.FileStore
alias AsciinemaWeb.Endpoint
alias AsciinemaWeb.Router.Helpers.Extra, as: Routes
alias AsciinemaWeb.UserView
import UserView, only: [theme_options: 0]
def player(src, opts \\ [])
def player(src, opts) when is_binary(src) do
opts = Keyword.merge([id: "player", src: src, preload: true], opts)
opts = Ext.Keyword.rename(opts, t: :"start-at", size: :"font-size")
content_tag :"asciinema-player", opts, do: []
end
def player(asciicast, opts) do
opts =
Keyword.merge([
cols: asciicast.terminal_columns,
rows: asciicast.terminal_lines,
theme: theme_name(asciicast),
poster: base64_poster(asciicast),
title: title(asciicast),
author: author_username(asciicast),
"author-url": author_profile_url(asciicast),
"author-img-url": author_avatar_url(asciicast)
], opts)
player(file_url(asciicast), opts)
end
def embed_script(asciicast) do
src = asciicast_url(Endpoint, :show, asciicast) <> ".js"
id = "asciicast-#{Phoenix.Param.to_param(asciicast)}"
content_tag(:script, [src: src, id: id, async: true], do: [])
end
defp file_url(asciicast) do
if path = Asciicasts.asciicast_file_path(asciicast) do
FileStore.url(path) || Routes.asciicast_file_url(asciicast)
end
end
defp asciicast_oembed_url(asciicast, format) do
oembed_url(
Endpoint,
:show,
url: asciicast_url(Endpoint, :show, asciicast),
format: format
)
end
defp short_text_description(asciicast) do
if asciicast.description do
asciicast.description
|> HtmlSanitizeEx.strip_tags()
|> String.replace(~r/[\r\n]+/, " ")
|> truncate(200)
else
"Recorded by #{author_username(asciicast)}"
end
end
defp truncate(text, length) do
if String.length(text) > length do
String.slice(text, 0, length - 3) <> "..."
else
text
end
end
defp alternate_link_type(asciicast) do
case asciicast.version do
1 -> "application/asciicast+json"
2 -> "application/x-asciicast"
_ -> nil
end
end
def download_filename(asciicast) do
case asciicast.version do
1 -> "#{asciicast.id}.json"
2 -> "#{asciicast.id}.cast"
_ -> nil
end
end
defp base64_poster(asciicast) do
encoded =
asciicast
|> Map.get(:snapshot)
|> Jason.encode!(escape: :unicode_safe)
|> Base.encode64()
"data:application/json;base64," <> encoded
end
def description(asciicast) do
desc = String.trim("#{asciicast.description}")
if present?(desc) do
{:safe, HtmlSanitizeEx.basic_html(Earmark.as_html!(desc))}
end
end
def os_info(asciicast) do
os_from_user_agent(asciicast) || os_from_uname(asciicast)
end
defp os_from_user_agent(asciicast) do
if ua = asciicast.user_agent do
if match = Regex.run(~r{^asciinema/\d(\.\d+)+ [^/\s]+/[^/\s]+ (.+)$}, ua) do
[_, _, os] = match
os
|> String.replace("-", "/")
|> String.split("/")
|> List.first()
|> String.replace(~r/Darwin/i, "macOS")
end
end
end
defp os_from_uname(asciicast) do
if uname = asciicast.uname do
cond do
uname =~ ~r/Linux/i -> "Linux"
uname =~ ~r/Darwin/i -> "macOS"
true -> uname |> String.split(~r/[\s-]/) |> List.first()
end
end
end
def shell_info(asciicast) do
Path.basename("#{asciicast.shell}")
end
def term_info(asciicast) do
asciicast.terminal_type
end
def views_count(asciicast) do
asciicast.views_count
end
def active_link(title, active?, opts) do
opts = if active? do
class = Keyword.get(opts, :class, "") <> " active"
Keyword.put(opts, :class, class)
else
opts
end
link(title, opts)
end
def nav_link(title, path, active?) do
content_tag :li, class: "nav-item" do
active_link(title, active?, to: path, class: "nav-link")
end
end
def title(asciicast) do
cond do
present?(asciicast.title) ->
asciicast.title
present?(asciicast.command) && asciicast.command != asciicast.shell ->
asciicast.command
asciicast.private ->
"untitled"
true ->
"asciicast:#{asciicast.id}"
end
end
def duration(asciicast) do
if d = asciicast.duration do
d = round(d)
minutes = div(d, 60)
seconds = rem(d, 60)
:io_lib.format("~2..0B:~2..0B", [minutes, seconds])
end
end
def theme_name(asciicast) do
asciicast.theme_name || default_theme_name(asciicast)
end
def default_theme_name(asciicast) do
UserView.theme_name(asciicast.user) || "asciinema"
end
def author_username(asciicast) do
UserView.username(asciicast.user)
end
def author_avatar_url(asciicast) do
UserView.avatar_url(asciicast.user)
end
def author_profile_path(asciicast) do
profile_path(asciicast.user)
end
def author_profile_url(asciicast) do
profile_url(asciicast.user)
end
def class(%{} = attrs) do
attrs
|> Enum.map(&class/1)
|> Enum.join(" ")
end
def class({"fg", fg}) when is_integer(fg), do: "fg-#{fg}"
def class({"bg", bg}) when is_integer(bg), do: "bg-#{bg}"
def class({"bold", true}), do: "bright"
def class({"underline", true}), do: "underline"
def class(_), do: nil
def style(attrs) do
styles =
[]
|> add_style("color", attrs["fg"])
|> add_style("background-color", attrs["bg"])
case styles do
[] -> nil
_ -> Enum.join(styles, ";")
end
end
defp add_style(styles, attr, [r, g, b]) do
["#{attr}:rgb(#{r},#{g},#{b})" | styles]
end
defp add_style(styles, _, _), do: styles
def thumbnail(asciicast, width \\ 80, height \\ 15) do
lines = asciicast.snapshot || []
lines
|> drop_trailing_blank_lines
|> fill_to_height(height)
|> take_last(height)
|> adjust_colors
|> split_chunks
|> crop_to_width(width)
|> group_chunks
end
def drop_trailing_blank_lines(lines) do
lines
|> Enum.reverse
|> Enum.drop_while(&blank_line?/1)
|> Enum.reverse
end
def fill_to_height(lines, height) do
if height - Enum.count(lines) > 0 do
enums = [lines, Stream.cycle([[]])]
enums
|> Stream.concat
|> Enum.take(height)
else
lines
end
end
def take_last(lines, height) do
lines
|> Enum.reverse
|> Enum.take(height)
|> Enum.reverse
end
def blank_line?(line) do
Enum.all?(line, &blank_chunk?/1)
end
def blank_chunk?(["", _attrs]), do: true
def blank_chunk?([text, attrs]) do
text = String.trim(text)
text == "" && attrs == %{}
end
def adjust_colors(lines) do
Enum.map(lines, &adjust_line_colors/1)
end
def adjust_line_colors(chunks) do
Enum.map(chunks, &adjust_chunk_colors/1)
end
def adjust_chunk_colors([text, attrs]) do
{text, adjust_attrs_colors(attrs)}
end
def adjust_attrs_colors(attrs) do
attrs
|> adjust_fg()
|> adjust_bg()
|> invert_colors()
end
def adjust_fg(%{"bold" => true, "fg" => fg} = attrs)
when is_integer(fg) and fg < 8 do
Map.put(attrs, "fg", fg + 8)
end
def adjust_fg(attrs), do: attrs
def adjust_bg(%{"blink" => true, "bg" => bg} = attrs)
when is_integer(bg) and bg < 8 do
Map.put(attrs, "bg", bg + 8)
end
def adjust_bg(attrs), do: attrs
@default_fg_code 7
@default_bg_code 0
def invert_colors(%{"inverse" => true} = attrs) do
fg = attrs["bg"] || @default_bg_code
bg = attrs["fg"] || @default_fg_code
Map.merge(attrs, %{"fg" => fg, "bg" => bg})
end
def invert_colors(attrs), do: attrs
def split_chunks(lines) do
Enum.map(lines, fn line ->
Enum.flat_map(line, &split_chunk/1)
end)
end
def split_chunk({text, attrs}) do
text
|> String.codepoints()
|> Enum.map(&{&1, attrs})
end
def crop_to_width(lines, width) do
Enum.map(lines, &crop_line_to_width(&1, width))
end
def crop_line_to_width(chunks, width) do
Enum.take(chunks, width)
end
def group_chunks(lines) do
Enum.map(lines, &group_line_chunks/1)
end
def group_line_chunks([]), do: []
def group_line_chunks([first_chunk | chunks]) do
{chunks, last_chunk} = Enum.reduce(chunks, {[], first_chunk},
fn {text, attrs}, {chunks, {prev_text, prev_attrs}} ->
if attrs == prev_attrs do
{chunks, {prev_text <> text, attrs}}
else
{[{prev_text, prev_attrs} | chunks], {text, attrs}}
end
end
)
Enum.reverse([last_chunk | chunks])
end
def render("show.svg", %{asciicast: asciicast} = params) do
cols = asciicast.terminal_columns
rows = asciicast.terminal_lines
lines = adjust_colors(asciicast.snapshot || [])
bg_lines = add_coords(lines)
text_lines =
lines
|> split_chunks()
|> add_coords()
|> remove_blank_chunks()
render(
"_terminal.svg",
cols: cols,
rows: rows,
bg_lines: bg_lines,
text_lines: text_lines,
font_family: params[:font_family],
theme_name: theme_name(asciicast)
)
end
defp add_coords(lines) do
for {chunks, y} <- Enum.with_index(lines) do
{_, chunks} = Enum.reduce(chunks, {0, []}, fn {text, attrs}, {x, chunks} ->
width = String.length(text)
chunk = %{text: text, attrs: attrs, x: x, width: width}
{x + width, [chunk | chunks]}
end)
chunks = Enum.reverse(chunks)
%{y: y, chunks: chunks}
end
end
defp remove_blank_chunks(lines) do
for line <- lines do
chunks = Enum.reject(line.chunks, & String.trim(&1.text) == "")
%{line | chunks: chunks}
end
end
def svg_text_class(%{} = attrs) do
classes =
attrs
|> Enum.reject(fn {attr, _} -> attr == "bg" end)
|> Enum.map(&svg_text_class/1)
|> Enum.filter(& &1)
case classes do
[] -> nil
_ -> Enum.join(classes, " ")
end
end
def svg_text_class({"fg", fg}) when is_integer(fg), do: "c-#{fg}"
def svg_text_class({"bold", true}), do: "br"
def svg_text_class({"italic", true}), do: "it"
def svg_text_class({"underline", true}), do: "un"
def svg_text_class(_), do: nil
def svg_rect_style(%{"bg" => [_r, _g, _b] = c}), do: "fill:#{hex(c)}"
def svg_rect_style(_), do: nil
def svg_rect_class(%{"bg" => bg}) when is_integer(bg), do: "c-#{bg}"
def svg_rect_class(_), do: nil
def svg_text_style(attrs) do
case attrs["fg"] do
[_r, _g, _b] = c -> "fill:#{hex(c)}"
_ -> nil
end
end
defp hex([r, g, b]) do
"##{hex(r)}#{hex(g)}#{hex(b)}"
end
defp hex(int) do
int
|> Integer.to_string(16)
|> String.pad_leading(2, "0")
end
def percent(float) do
"#{Decimal.round(Decimal.new(float), 3)}%"
end
def asciicast_gc_days do
Asciinema.Asciicasts.gc_days()
end
end