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

164 lines
3.1 KiB
Ruby

require 'base64'
class AsciicastDecorator < ApplicationDecorator
decorates :asciicast
decorates_association :user
THUMBNAIL_WIDTH = 20
THUMBNAIL_HEIGHT = 10
13 years ago
MAX_DELAY = 5.0
def as_json(*args)
data = model.as_json(*args)
data['escaped_stdout_data'] = escaped_stdout_data
13 years ago
data['stdout_timing_data'], saved_time = stdout_timing_data
data['duration'] = data['duration'] - saved_time
data
end
def escaped_stdout_data
if data = stdout.read
Base64.strict_encode64(data)
else
nil
end
end
def stdout_timing_data
13 years ago
saved_time = 0
if file = stdout_timing.file
f = IO.popen "bzip2 -d", "r+"
f.write file.read
f.close_write
lines = f.readlines
f.close
data = lines.map do |line|
delay, n = line.split
13 years ago
delay = delay.to_f
if time_compression && delay > MAX_DELAY
saved_time += (delay - MAX_DELAY)
delay = MAX_DELAY
end
[delay, n.to_i]
end
else
13 years ago
data = nil
end
13 years ago
[data, saved_time]
end
def os
return 'unknown' if uname.blank?
if uname =~ /Linux/
'Linux'
elsif uname =~ /Darwin/
'OSX'
else
uname.split(' ', 2)[0]
end
end
def terminal_type
asciicast.terminal_type || '?'
end
12 years ago
def shell
File.basename(asciicast.shell.to_s)
end
12 years ago
def title
if asciicast.title.present?
asciicast.title
elsif command.present?
"$ #{command}"
else
"##{id}"
end
end
def thumbnail(width = THUMBNAIL_WIDTH, height = THUMBNAIL_HEIGHT)
if @thumbnail.nil?
lines = model.snapshot.to_s.split("\n")
top_lines = lines[0...height]
top_text = prepare_lines(top_lines, width, height).join("\n")
bottom_lines = lines.reverse[0...height].reverse
bottom_text = prepare_lines(bottom_lines, width, height).join("\n")
if top_text.gsub(/\s+/, '').size > bottom_text.gsub(/\s+/, '').size
@thumbnail = top_text
else
@thumbnail = bottom_text
end
end
@thumbnail
end
12 years ago
def description
if asciicast.description.present?
text = asciicast.description.to_s
markdown(text)
else
h.content_tag :em, 'No description.'
end
end
def author_profile_link(options = {})
if asciicast.user
if options[:avatar]
img = avatar_img(asciicast.user) + " "
else
img = ""
end
text = img + author
12 years ago
path = h.profile_path(asciicast.user)
h.link_to text, path
else
author
end
end
def other_by_user
if asciicast.user
AsciicastDecorator.decorate(
asciicast.user.asciicasts.where('id <> ?', asciicast.id).limit(3)
)
else
[]
end
end
def author
if user
user.nickname
elsif asciicast.username
"~#{asciicast.username}"
else
'anonymous'
end
end
private
def prepare_lines(lines, width, height)
(height - lines.size).times { lines << '' }
lines.map do |line|
line = line[0...width]
line << ' ' * (width - line.size)
line
end
end
end