class Shatty

Public Instance Methods

execute() click to toggle source
# File bin/shatty, line 24
def execute
  start = Time.now
  if output.nil?
    self.output = "http://shatty.semicomplete.com:8200/s/#{UUIDTools::UUID.random_create}"
    puts "Sending output to: #{output}"
    puts "View commands"
    puts "  wget -qO- #{output}"
    puts "  curl -Lso- #{output}"
    puts "  shatty play #{output}"
  end

  if output =~ /^https?:/
    agent = FTW::Agent.new
    stream, out = IO::pipe
    Thread.new { 
      response = agent.post!(output, :body => stream)
      # TODO(sissel): Shouldn't get here...
    }
  else
    # no http/https, assume file output.
    out = File.new(output, "w")
  end

  # binary mode.
  buffer = ""

  STDOUT.sync = true
  terminal, keyboard, pid = PTY.spawn(*command)
  system("stty raw -echo") # yeah, perhaps we should use termios instead.

  # Dump stdin to the tty's keyboard
  # We could use reopen here, but the 'keyboard' io has mode read/write.
  Thread.new { STDIN.each_char { |c| keyboard.syswrite(c) } }

  while true
    # Read from the terminal output and compute the time offset
    begin
      terminal.sysread(16834, buffer)
    rescue Errno::EIO => e
      Process.waitpid(pid)
      puts "Command exited with code: #{$?.exitstatus}"
      break
    end

    time_offset = Time.now - start

    # for each chunk of text read from tmux, record
    # the timestamp (duration since 'start' of recording)
    out.syswrite([time_offset.to_f, buffer.length, buffer].pack("GNA#{buffer.length}"))
    $stdout.syswrite(buffer) unless headless?
  end

  system("stty sane")
end