Sha256: 9c84f60293643d4ed27e0013900a56b5f6639e54a29e9467fd7b4a81245b7b3e

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

#!/usr/bin/env ruby

require 'paint'
require 'json'
require 'open3'


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

require 'shellplay/session'

@session = Shellplay::Session.new
@session.import(ARGV[0])

continue = true
@sleeptime = 1.0/48.0
@lastelapsed = 0

counter = 1
def usage
  puts "\nCommands: "
  puts "  h,?        show help"
  puts "  s          list screens"
  puts "  s#         jump to screen #"
  puts "  <enter>    jump to next screen"
  puts "  p          jump to previous screen"
  puts "  <command>  execute a subcommand in a bash -l -c"
  puts "  x,q        quit"
  puts
end

def display(screen)
  print @session.config.prompt
  sleep @sleeptime
  print screen.stdin
  STDIN.gets
  puts screen.stdout
  puts Paint[screen.stderr, :red]
  @lastelapsed = screen.timespent
end

def shownext
  if @session.current_screen and @session.current_screen.stdin
    display @session.current_screen
    @session.next
  else
    puts "You are at the end of the session."
    @lastelapsed = 0
  end
end

def show(index)
  if @session.show(index).stdin
    display @session.show(index)
    @session.next
  else
    puts "You are at the end of the session."
    @lastelapsed = 0
  end
end

while continue do
  printf "\e[36m>\e[0m "
  printf "\e[35melapsed: #{@lastelapsed}s\e[0m " unless @lastelapsed == 0
  command = STDIN.gets.strip
  case command
  when /^(?:q|x)$/
    puts "\nPlay ended.\n"
    continue = false
  when /^(?:\?|h)$/
    usage
  when /^s$/
    @session.sequence.map { |c| c.stdin }.each_with_index do |l, i|
      printf("   s%-3s %s\n", i, l)
    end
  when /s([0-9]+)/
    show($1)
  when ''
    shownext
  else
    Open3.popen3("bash","-l","-c",command.strip) do |i, o, e, t|
      o.read.split("\n").each do |line|
        puts line
        sleep sleeptime
      end
      e.read.split("\n").each do |line|
        puts Paint[line, :red]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shellplay-0.0.2 bin/shellplay