Skip to content

Commit

Permalink
Fix theme name validation, add Nord to theme options
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jul 20, 2023
1 parent bf4a55a commit 803f2dd
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 20 deletions.
5 changes: 2 additions & 3 deletions lib/asciinema/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ defmodule Asciinema.Accounts do
import Ecto.Query, warn: false
import Ecto, only: [assoc: 2, build_assoc: 2]
alias Asciinema.Accounts.{User, ApiToken}
alias Asciinema.Repo
alias Asciinema.{Media, Repo}
alias Ecto.Changeset

@valid_email_re ~r/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i
@valid_username_re ~r/^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$/
@valid_theme_names ["asciinema", "tango", "solarized-dark", "solarized-light", "monokai"]

def fetch_user(id) do
case get_user(id) do
Expand Down Expand Up @@ -67,7 +66,7 @@ defmodule Asciinema.Accounts do
|> validate_format(:email, @valid_email_re)
|> validate_format(:username, @valid_username_re)
|> validate_length(:username, min: 2, max: 16)
|> validate_inclusion(:theme_name, @valid_theme_names)
|> validate_inclusion(:theme_name, Media.themes())
|> add_contraints()
end

Expand Down
7 changes: 6 additions & 1 deletion lib/asciinema/recordings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,12 @@ defmodule Asciinema.Recordings do

def update_asciicast(asciicast, attrs \\ %{}) do
changeset =
Asciicast.update_changeset(asciicast, attrs, Media.custom_terminal_font_families())
Asciicast.update_changeset(
asciicast,
attrs,
Media.custom_terminal_font_families(),
Media.themes()
)

with {:ok, asciicast} <- Repo.update(changeset) do
if stale_snapshot?(changeset) do
Expand Down
3 changes: 2 additions & 1 deletion lib/asciinema/recordings/asciicast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule Asciinema.Recordings.Asciicast do
|> generate_secret_token
end

def update_changeset(struct, attrs, custom_terminal_font_families \\ []) do
def update_changeset(struct, attrs, custom_terminal_font_families \\ [], themes \\ []) do
struct
|> changeset(attrs)
|> cast(attrs, [
Expand All @@ -107,6 +107,7 @@ defmodule Asciinema.Recordings.Asciicast do
|> validate_number(:cols_override, greater_than: 0, less_than: 1024)
|> validate_number(:rows_override, greater_than: 0, less_than: 512)
|> validate_number(:idle_time_limit, greater_than_or_equal_to: 0.5)
|> validate_inclusion(:theme_name, themes)
|> validate_number(:terminal_line_height,
greater_than_or_equal_to: 1.0,
less_than_or_equal_to: 2.0
Expand Down
4 changes: 2 additions & 2 deletions lib/asciinema_web/controllers/live_stream_html.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
defmodule AsciinemaWeb.LiveStreamHTML do
use AsciinemaWeb, :html
alias AsciinemaWeb.{PlayerView, RecordingView, UserView}
alias AsciinemaWeb.{PlayerView, RecordingView}

embed_templates "live_stream/*"

defdelegate author_username(stream), to: PlayerView
defdelegate author_avatar_url(stream), to: PlayerView
defdelegate author_profile_path(stream), to: PlayerView
defdelegate theme_name(stream), to: PlayerView
defdelegate theme_options, to: PlayerView
defdelegate default_theme_name(stream), to: PlayerView
defdelegate terminal_font_family_options, to: PlayerView
defdelegate theme_options, to: UserView

def player_src(stream) do
%{
Expand Down
5 changes: 3 additions & 2 deletions lib/asciinema_web/templates/doc/embedding.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ to a theme set by the asciicast author (or to "asciinema" if not set by the
author). The available themes are:

* asciinema
* tango
* monokai
* nord
* solarized-dark
* solarized-light
* monokai
* tango

### **cols**

Expand Down
22 changes: 22 additions & 0 deletions lib/asciinema_web/templates/recording/_svg_theme_nord.css.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- nord theme -->

.default-text-fill {fill: #eceff4}
.default-bg-fill {fill: #2e3440}

.c-0 {fill: #3b4252}
.c-1 {fill: #bf616a}
.c-2 {fill: #a3be8c}
.c-3 {fill: #ebcb8b}
.c-4 {fill: #81a1c1}
.c-5 {fill: #b48ead}
.c-6 {fill: #88c0d0}
.c-7 {fill: #eceff4}
.c-8 {fill: #3b4252}
.c-9 {fill: #bf616a}
.c-10 {fill: #a3be8c}
.c-11 {fill: #ebcb8b}
.c-12 {fill: #81a1c1}
.c-13 {fill: #b48ead}
.c-14 {fill: #88c0d0}
.c-15 {fill: #eceff4}
.c-8, .c-9, .c-10, .c-11, .c-12, .c-13, .c-14, .c-15 {font-weight: bold}
11 changes: 11 additions & 0 deletions lib/asciinema_web/views/player_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ defmodule AsciinemaWeb.PlayerView do
profile_url(user)
end

def theme_options do
[
{"asciinema", "asciinema"},
{"Monokai", "monokai"},
{"Nord", "nord"},
{"Tango", "tango"},
{"Solarized Dark", "solarized-dark"},
{"Solarized Light", "solarized-light"}
]
end

def theme_name(medium) do
medium.theme_name || default_theme_name(medium)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/asciinema_web/views/recording_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ defmodule AsciinemaWeb.RecordingView do
alias AsciinemaWeb.Endpoint
alias AsciinemaWeb.Router.Helpers.Extra, as: RoutesX
alias AsciinemaWeb.{PlayerView, UserView}
import UserView, only: [theme_options: 0]

defdelegate author_username(asciicast), to: PlayerView
defdelegate author_avatar_url(stream), to: PlayerView
defdelegate author_profile_path(stream), to: PlayerView
defdelegate theme_name(stream), to: PlayerView
defdelegate theme_options, to: PlayerView
defdelegate default_theme_name(stream), to: PlayerView
defdelegate terminal_font_family_options, to: PlayerView
defdelegate username(user), to: UserView
Expand Down
12 changes: 2 additions & 10 deletions lib/asciinema_web/views/user_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule AsciinemaWeb.UserView do
import Scrivener.HTML
alias Asciinema.Gravatar

defdelegate theme_options, to: AsciinemaWeb.PlayerView

def avatar_url(user) do
username = username(user)
email = user.email || "#{username}+#{user.id}@asciinema.org"
Expand All @@ -27,16 +29,6 @@ defmodule AsciinemaWeb.UserView do
user.theme_name
end

def theme_options do
[
{"asciinema", "asciinema"},
{"Tango", "tango"},
{"Solarized Dark", "solarized-dark"},
{"Solarized Light", "solarized-light"},
{"Monokai", "monokai"}
]
end

def active_tokens(api_tokens) do
api_tokens
|> Enum.reject(& &1.revoked_at)
Expand Down
11 changes: 11 additions & 0 deletions lib/media.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,16 @@ defmodule Asciinema.Media do
"JetBrainsMono Nerd Font"
]

@themes [
"asciinema",
"monokai",
"nord",
"solarized-dark",
"solarized-light",
"tango"
]

def custom_terminal_font_families, do: @custom_terminal_font_families

def themes, do: @themes
end

0 comments on commit 803f2dd

Please sign in to comment.