Class: Runcmd::Cli::PlayCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/runcmd/cli/play_command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/runcmd/cli/play_command.rb', line 13

def execute
  recording_input = File.new recording, "r"
  version = recording_input.readline
  cmd = recording_input.readline
  args = recording_input.readline.split(" ")

  stderr_reader, stderr_writer = IO.pipe

  env = {
    "LINES" => IO.console.winsize.first.to_s,
    "COLUMNS" => IO.console.winsize.last.to_s
  }

  stdout,stdin,pid = PTY.spawn(env, cmd, *args, err: stderr_writer.fileno)
  stderr_writer.close

  started_at = Time.now
  stdin_thr = Thread.new do
    loop do
      command = recording_input.getc
      case command
      when "c"
        c = recording_input.getc
        time = []
        loop do
          t = recording_input.getc
          break if t == ":"
          time << t
        end
        sleep time.join("").to_f
        case c
        when "\u0003"
          # control+c
          stdin.print c
        else
          stdin.print c
        end
        sleep delay
      when "a"
        assert_chars = []
        loop do
          assert_c = recording_input.getc
          case assert_c
          when "\u0003"
            break
          else
            assert_chars << assert_c
          end
        end

        assert_string = assert_chars.join("")
        output_chars = []
        loop do
          if $output.join("").match? assert_string
            $output = []
            break
          end
          sleep 0.1
        end
      end
    end

    stdin.close
  end

  $output = []
  stdout_thr = Thread.new do
    while c = stdout.getc
      $output << c
      print c
    end
  end

  stderr_thr = Thread.new do
    while c = stderr_reader.getc
      print c
    end
  end

  stdout_thr.join
  stdin_thr.kill

  stdin_thr.join
  stderr_thr.join

  recording_input.close
end