mose/shellplay

View on GitHub
bin/shellrecord

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'paint'
require 'open3'

lib = File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

SHELL = ENV['SHELL']

require 'shellplay'

continue = true

session = Shellplay::Session.new
prompt = "\e[36mrecord >\e[0m"
customprompt = nil

while continue do
  printf("\e[36mrecord >\e[33m %d > \e[0m", session.pointer)
  command = STDIN.gets.strip
  displaycommand = true
  playprompt = true
  clearscreen = false
  fakeit = false
  case command
  when "q"
    session.save
    puts "\nRecord ended.\n"
    continue = false
  when 'x'
    puts "\nRecord cancelled.\n"
    continue = false
  when "d"
    if session.pointer == 0
      puts "There is no screen to delete."
    else
      session.drop_last_screen
    end
  when ""
    screen = Shellplay::Screen.new
    session.add_screen({
      'displaycommand' => false,
      'playprompt' => false,
      'clearscreen' => true,
      'customprompt' => nil,
      'stdin' => '',
      'stdout' => '',
      'stderr' => '',
      'timespent' => 0
    })
  when /^>/
    command = command[1..-1].strip
    if command[0] == '>'
      customprompt = nil
    else
      print "New prompt: " + command.gsub(/\\e/,27.chr) + "\n"
      customprompt = command.gsub(/\\e/,27.chr)
    end
  else
    if command[0] == '?'
      command = command[1..-1].strip
      fakeit = true
    end
    if command[0] == '#'
      command = command[1..-1].strip
      displaycommand = false
      if command[0] == '#'
        command = command[1..-1].strip
        playprompt = false
      end
    end
    if command[0] == '/'
      command = command[1..-1].strip
      clearscreen = true
    end
    if fakeit
      go_on = true
      out = ""
      while go_on do
        line = STDIN.gets
        if line == ".\n"
          go_on = false
        else
          out << line
        end
      end
      screen = Shellplay::Screen.new
      session.add_screen({
        'displaycommand' => displaycommand,
        'playprompt' => playprompt,
        'clearscreen' => clearscreen,
        'customprompt' => customprompt,
        'stdin' => command,
        'stdout' => out.gsub(/\\e/,27.chr).chomp,
        'stderr' => '',
        'timespent' => 0
      })
    else
      start_time = Time.now
      Open3.popen3(SHELL,"-c", command) do |i, o, e, t|
        screen = Shellplay::Screen.new
        out = o.read
        err = e.read
        end_time = Time.now
        elapsed = end_time - start_time
        stdin = displaycommand ? command : ""
        session.add_screen({
          'displaycommand' => displaycommand,
          'playprompt' => playprompt,
          'clearscreen' => clearscreen,
          'customprompt' => customprompt,
          'stdin' => stdin,
          'stdout' => out.chomp,
          'stderr' => err.chomp,
          'timespent' => elapsed
          })
        out.split("\n").each do |line|
          puts line
        end
        err.split("\n").each do |line|
          puts Paint[line, :red]
        end
      end
      if /^cd (.*)$/.match command
        Dir.chdir($1)
      end
    end
    session.next
  end
end