Class: Spielbash::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/spielbash/model/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, output_path, context) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
16
# File 'lib/spielbash/model/session.rb', line 10

def initialize(name, output_path, context)
  @context = context
  @name = name
  @output_path = output_path

  ChildProcess.posix_spawn = true
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/spielbash/model/session.rb', line 7

def context
  @context
end

#last_stderrObject (readonly)

Returns the value of attribute last_stderr.



8
9
10
# File 'lib/spielbash/model/session.rb', line 8

def last_stderr
  @last_stderr
end

#last_stdoutObject (readonly)

Returns the value of attribute last_stdout.



8
9
10
# File 'lib/spielbash/model/session.rb', line 8

def last_stdout
  @last_stdout
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/spielbash/model/session.rb', line 7

def name
  @name
end

#output_pathObject

Returns the value of attribute output_path.



7
8
9
# File 'lib/spielbash/model/session.rb', line 7

def output_path
  @output_path
end

Instance Method Details

#close_sessionObject



28
29
30
31
32
33
# File 'lib/spielbash/model/session.rb', line 28

def close_session
  send_key("exit")
  send_key('C-m')

  wait
end

#new_sessionObject



18
19
20
21
22
# File 'lib/spielbash/model/session.rb', line 18

def new_session
  execute_with('resize', "-s #{context.height} #{context.width}", false, false, false)
  execute_tmux_with("new-session -s #{name} -d")
  wait
end

#send_key(key, count = 1) ⇒ Object



48
49
50
51
# File 'lib/spielbash/model/session.rb', line 48

def send_key(key, count=1)
  key = 'Space' if key == ' '
  execute_tmux_with("send-keys -t #{name} -N #{count} #{key}")
end

#start_recordingObject



53
54
55
56
57
58
# File 'lib/spielbash/model/session.rb', line 53

def start_recording
  execute_tmux_with('list-panes -F #{pane_pid}' + " -t #{name}", true)
  pid = last_stdout.strip
  execute_with('pgrep', "-P #{pid}", true)
  execute_with_exactly('asciinema', false, true, false, "rec", "-y", "-c", "tmux attach -t #{name}", "#{output_path}")
end

#stop_recordingObject



24
25
26
# File 'lib/spielbash/model/session.rb', line 24

def stop_recording
  send_key('C-d')
end

#waitObject

This will wait for tmux session pid to not have any child processes



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spielbash/model/session.rb', line 36

def wait
  execute_tmux_with('list-panes -F #{pane_pid}' + " -t #{name}", true)
  pid = last_stdout.strip
  return if pid.empty?

  loop do
    exec_wait_check_cmd(pid)
    children = last_stdout
    break if children.empty?
  end
end