Class: Shelr::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/shelr/player.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/shelr/player.rb', line 30

def self.list
  (Dir[File.join(Shelr::DATA_DIR, "**")] - ['.', '..']).sort.each do |dir|
    begin
       = JSON.parse(IO.read(File.join(dir, 'meta')))
      puts "#{["recorded_at"]} : #{["title"]}"
    rescue Errno::ENOENT
      puts "Corrupted shellcast in #{dir}"
    end
  end
end

.play(options) ⇒ Object



11
12
13
# File 'lib/shelr/player.rb', line 11

def self.play(options)
  new.play(options)
end

.play_dump(file) ⇒ Object



24
25
26
27
28
# File 'lib/shelr/player.rb', line 24

def self.play_dump(file)
  json = File.read(file)
  parts = JSON.parse(json)
  play_parts_hash(parts)
end

.play_parts_hash(parts) ⇒ Object

TODO: refactore me!



42
43
44
45
46
47
48
49
# File 'lib/shelr/player.rb', line 42

def self.play_parts_hash(parts)
  Dir.mktmpdir do |dir|
    %w(typescript timing).each do |type|
      File.open(File.join(dir, type), 'w') { |f| f.puts(parts[type]) }
    end
    play(:record_dir => dir)
  end
end

.play_remote(url) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/shelr/player.rb', line 15

def self.play_remote(url)
  puts ".==> Fetching #{url}"
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  resp = http.request_get(uri.path).body
  play_parts_hash(JSON.parse(resp))
end

.scriptreplay(typescript_file, timing_file) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/shelr/player.rb', line 75

def self.scriptreplay(typescript_file, timing_file)
  typescript = File.open(typescript_file)
  timing = File.open(timing_file)
  frames = timing.read.split("\n").map { |line| line.split(" ") }
  frames.map! { |k,v| [k.to_f.abs, v.to_i] }
  typescript.gets # skip first line

  frames.each do |usec,length|
    sleep(usec)
    print typescript.read(length)
  end
end

Instance Method Details

#play(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/shelr/player.rb', line 51

def play(options = {})
  @options = options
  start_sound_player
  Shelr.terminal.puts_line
  self.class.scriptreplay record_file('typescript'), record_file('timing')
  Shelr.terminal.puts_line
  stop_sound_player
end

#start_sound_playerObject



60
61
62
63
64
65
66
67
# File 'lib/shelr/player.rb', line 60

def start_sound_player
  return unless File.exist?(record_file('sound.ogg'))
  # at_exit { system('stty echo') }
  STDOUT.puts "=> Starting sound player..."
  @sox_pid = fork do
    `play #{record_file('sound.ogg')} 2>&1`
  end
end

#stop_sound_playerObject



69
70
71
72
73
# File 'lib/shelr/player.rb', line 69

def stop_sound_player
  return unless File.exist?(record_file('sound.ogg'))
  STDOUT.puts "=> Stopping sound player..."
  Process.waitpid(@sox_pid)
end