#!/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 @noplayprompt = false counter = 1 def usage puts "\nCommands: " puts " h,? show help" puts " s list screens" puts " s# jump to screen #" puts " jump to next screen" puts " p jump to previous screen" puts " execute a subcommand in a bash -l -c" puts " x,q quit" puts end def display(screen) if screen.clearscreen if ENV['TERM_PROGRAM'] = 'iTerm.app' printf "\e]50;ClearScrollback\a" else printf "\e\143" end end if screen.displaycommand if screen.customprompt print screen.customprompt else print @session.config.prompt end sleep @sleeptime print screen.stdin STDIN.gets @lastelapsed = screen.timespent else @lastelapsed = 0 end print screen.stdout if screen.stderr != "" printf "\n#{Paint[screen.stderr, :red]}" end 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 print "\n\e[36m>\e[0m " if @playprompt print "\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| print line sleep sleeptime end e.read.split("\n").each do |line| print Paint[line, :red] end end end end