Class: TTYtest::Tmux::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/ttytest/tmux/session.rb

Overview

represents a tmux session and how to send output to the current tmux session

Instance Method Summary collapse

Constructor Details

#initialize(driver, name) ⇒ Session

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Session.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ttytest/tmux/session.rb', line 8

def initialize(driver, name)
  @id = SecureRandom.uuid
  @driver = driver
  @name = name

  ObjectSpace.define_finalizer(@id, proc {
    begin
      driver.tmux(*%W[kill-session -t #{name}])
    rescue ThreadError => _e # final session always throws during testing (running rake),
      # throws error 'ThreadError can't alloc new'
    end
  })
end

Instance Method Details

#captureObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ttytest/tmux/session.rb', line 22

def capture
  contents = driver.tmux(*%W[capture-pane -t #{name} -p])
  str = driver.tmux(*%W[display-message -t #{name} -p
                        #\{cursor_x},#\{cursor_y},#\{cursor_flag},#\{pane_width},#\{pane_height},#\{pane_dead},#\{pane_dead_status},])
  x, y, cursor_flag, width, height, pane_dead, pane_dead_status, _newline = str.split(',')

  if pane_dead == '1'
    raise Driver::TmuxError,
          "Tmux pane has died\nCommand exited with status: #{pane_dead_status}\nEntire screen:\n#{contents}"
  end

  TTYtest::Capture.new(
    contents.chomp("\n"),
    cursor_x: x.to_i,
    cursor_y: y.to_i,
    width: width.to_i,
    height: height.to_i,
    cursor_visible: (cursor_flag != '0')
  )
end

#send_backspaceObject



124
125
126
# File 'lib/ttytest/tmux/session.rb', line 124

def send_backspace
  send_keys_exact(%(BSpace))
end

#send_backspaces(number_of_times) ⇒ Object



128
129
130
131
132
133
# File 'lib/ttytest/tmux/session.rb', line 128

def send_backspaces(number_of_times)
  while number_of_times.positive?
    send_backspace
    number_of_times -= 1
  end
end

#send_clearObject



195
196
197
198
# File 'lib/ttytest/tmux/session.rb', line 195

def send_clear
  send_keys_one_at_a_time(TTYtest::CLEAR)
  send_newline
end

#send_deleteObject



113
114
115
# File 'lib/ttytest/tmux/session.rb', line 113

def send_delete
  send_keys_exact(%(DC))
end

#send_deletes(number_of_times) ⇒ Object



117
118
119
120
121
122
# File 'lib/ttytest/tmux/session.rb', line 117

def send_deletes(number_of_times)
  while number_of_times.positive?
    send_delete
    number_of_times -= 1
  end
end

#send_down_arrowObject



168
169
170
# File 'lib/ttytest/tmux/session.rb', line 168

def send_down_arrow
  send_keys(TTYtest::DOWN_ARROW)
end

#send_down_arrows(number_of_times) ⇒ Object



172
173
174
175
176
177
# File 'lib/ttytest/tmux/session.rb', line 172

def send_down_arrows(number_of_times)
  while number_of_times.positive?
    send_down_arrow
    number_of_times -= 1
  end
end

#send_endObject



191
192
193
# File 'lib/ttytest/tmux/session.rb', line 191

def send_end
  send_keys_exact(TTYtest::END_KEY)
end

#send_escapeObject



200
201
202
# File 'lib/ttytest/tmux/session.rb', line 200

def send_escape
  send_keys_exact(%(Escape))
end

#send_escapes(number_of_times) ⇒ Object



204
205
206
207
208
209
# File 'lib/ttytest/tmux/session.rb', line 204

def send_escapes(number_of_times)
  while number_of_times.positive?
    send_escape
    number_of_times -= 1
  end
end

#send_homeObject



187
188
189
# File 'lib/ttytest/tmux/session.rb', line 187

def send_home
  send_keys_exact(TTYtest::HOME_KEY)
end

#send_keys(*keys) ⇒ Object

Send the array of keys as a string literal to tmux. Will not be interpreted as send-keys values like Enter, Escape, DC for Delete, etc.

Parameters:

  • keys (%w())

    the keys to send to tmux



46
47
48
# File 'lib/ttytest/tmux/session.rb', line 46

def send_keys(*keys)
  driver.tmux(*%W[send-keys -t #{name} -l], *keys)
end

#send_keys_exact(keys) ⇒ Object

Useful to send send-keys commands to tmux without sending them as a string literal. So you can send Escape for escape key, DC for delete, etc. Uses the same key bindings as bind-key as well. C-c represents Ctrl + C keys, F1 is F1 key, etc.

Parameters:

  • keys (String)

    the keys to send to tmux



183
184
185
# File 'lib/ttytest/tmux/session.rb', line 183

def send_keys_exact(keys)
  driver.tmux(*%W[send-keys -t #{name}], keys)
end

#send_keys_one_at_a_time(keys) ⇒ Object

Send a string of keys one character at a time as literals to tmux.

Parameters:

  • keys (String)

    the keys to send one at a time to tmux



52
53
54
55
56
# File 'lib/ttytest/tmux/session.rb', line 52

def send_keys_one_at_a_time(keys)
  keys.split('').each do |key|
    driver.tmux(*%W[send-keys -t #{name} -l], key)
  end
end

#send_left_arrowObject



146
147
148
# File 'lib/ttytest/tmux/session.rb', line 146

def send_left_arrow
  send_keys(TTYtest::LEFT_ARROW)
end

#send_left_arrows(number_of_times) ⇒ Object



150
151
152
153
154
155
# File 'lib/ttytest/tmux/session.rb', line 150

def send_left_arrows(number_of_times)
  while number_of_times.positive?
    send_left_arrow
    number_of_times -= 1
  end
end

#send_line(line) ⇒ Object

Send line to tmux, no need to worry about newline character



59
60
61
62
# File 'lib/ttytest/tmux/session.rb', line 59

def send_line(line)
  send_keys_one_at_a_time(line)
  send_newline unless line[-1] == '\n'
end

#send_line_exact(line) ⇒ Object



76
77
78
79
# File 'lib/ttytest/tmux/session.rb', line 76

def send_line_exact(line)
  send_keys_exact(line)
  send_newline unless line[-1] == '\n'
end

#send_line_then_sleep(line, sleep_time) ⇒ Object

Send line then sleep for sleep_time



65
66
67
68
# File 'lib/ttytest/tmux/session.rb', line 65

def send_line_then_sleep(line, sleep_time)
  send_line(line)
  sleep sleep_time
end

#send_line_then_sleep_and_repeat(*lines, sleep_time) ⇒ Object



94
95
96
97
98
# File 'lib/ttytest/tmux/session.rb', line 94

def send_line_then_sleep_and_repeat(*lines, sleep_time)
  lines.each do |line|
    send_line_then_sleep(line, sleep_time)
  end
end

#send_lines(*lines) ⇒ Object



70
71
72
73
74
# File 'lib/ttytest/tmux/session.rb', line 70

def send_lines(*lines)
  lines.each do |line|
    send_line(line)
  end
end

#send_lines_exact(*lines) ⇒ Object



81
82
83
84
85
# File 'lib/ttytest/tmux/session.rb', line 81

def send_lines_exact(*lines)
  lines.each do |line|
    send_line_exact(line)
  end
end

#send_lines_then_sleep(*lines, sleep_time) ⇒ Object



87
88
89
90
91
92
# File 'lib/ttytest/tmux/session.rb', line 87

def send_lines_then_sleep(*lines, sleep_time)
  lines.each do |line|
    send_line(line)
  end
  sleep sleep_time
end

#send_newlineObject Also known as: send_enter



100
101
102
# File 'lib/ttytest/tmux/session.rb', line 100

def send_newline
  driver.tmux(*%W[send-keys -t #{name} -l], %(\n))
end

#send_newlines(number_of_times) ⇒ Object Also known as: send_enters



105
106
107
108
109
110
# File 'lib/ttytest/tmux/session.rb', line 105

def send_newlines(number_of_times)
  while number_of_times.positive?
    send_newline
    number_of_times -= 1
  end
end

#send_right_arrowObject



135
136
137
# File 'lib/ttytest/tmux/session.rb', line 135

def send_right_arrow
  send_keys(TTYtest::RIGHT_ARROW)
end

#send_right_arrows(number_of_times) ⇒ Object



139
140
141
142
143
144
# File 'lib/ttytest/tmux/session.rb', line 139

def send_right_arrows(number_of_times)
  while number_of_times.positive?
    send_right_arrow
    number_of_times -= 1
  end
end

#send_up_arrowObject



157
158
159
# File 'lib/ttytest/tmux/session.rb', line 157

def send_up_arrow
  send_keys(TTYtest::UP_ARROW)
end

#send_up_arrows(number_of_times) ⇒ Object



161
162
163
164
165
166
# File 'lib/ttytest/tmux/session.rb', line 161

def send_up_arrows(number_of_times)
  while number_of_times.positive?
    send_up_arrow
    number_of_times -= 1
  end
end