You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
asciinema.org/app/decorators/asciicast_decorator.rb

87 lines
1.5 KiB
Ruby

class AsciicastDecorator < ApplicationDecorator
THUMBNAIL_WIDTH = 20
THUMBNAIL_HEIGHT = 10
decorates_association :user
def os
if user_agent.present?
os_from_user_agent
elsif uname.present?
os_from_uname
else
'unknown'
end
end
def terminal_type
model.terminal_type.presence || '?'
end
12 years ago
def shell
File.basename(model.shell.to_s)
end
12 years ago
def title
model.title.presence || model.command.presence || "asciicast:#{id}"
end
def thumbnail(width = THUMBNAIL_WIDTH, height = THUMBNAIL_HEIGHT)
snapshot = Snapshot.build(model.snapshot || [[]] * height)
thumbnail = SnapshotDecorator.new(snapshot.thumbnail(width, height))
h.render 'asciicasts/thumbnail', :thumbnail => thumbnail
end
12 years ago
def description
if model.description.present?
text = model.description.to_s
markdown(text)
end
end
def author_link
user.link
end
def author_img_link
user.img_link
end
def formatted_duration
duration = model.duration.to_i
minutes = duration / 60
seconds = duration % 60
"%02d:%02d" % [minutes, seconds]
end
def theme_name
(model.theme || user.theme || Theme.default).name
end
private
def os_from_user_agent
os_part = user_agent.split(' ')[2]
os = os_part.split('/').first
guess_os(os)
end
def os_from_uname
guess_os(uname)
end
def guess_os(text)
if text =~ /Linux/
'Linux'
elsif text =~ /Darwin/
'OS X'
else
text.split(' ', 2)[0]
end
end
end