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, use_return_for_newline) ⇒ 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
21
# File 'lib/ttytest/tmux/session.rb', line 8

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

  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



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

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



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

def send_backspace
  send_keys_exact(%(BSpace))
end

#send_backspaces(number_of_times) ⇒ Object



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

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

#send_clearObject



232
233
234
235
236
237
238
239
# File 'lib/ttytest/tmux/session.rb', line 232

def send_clear
  send_keys_one_at_a_time(TTYtest::CLEAR)
  if @use_return_for_newline
    send_return
    return
  end
  send_newline
end

#send_deleteObject



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

def send_delete
  send_keys_exact(%(DC))
end

#send_deletes(number_of_times) ⇒ Object



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

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

#send_down_arrowObject



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

def send_down_arrow
  send_keys(TTYtest::DOWN_ARROW)
end

#send_down_arrows(number_of_times) ⇒ Object



209
210
211
212
213
214
# File 'lib/ttytest/tmux/session.rb', line 209

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

#send_endObject



228
229
230
# File 'lib/ttytest/tmux/session.rb', line 228

def send_end
  send_keys_exact(TTYtest::END_KEY)
end

#send_escapeObject



241
242
243
# File 'lib/ttytest/tmux/session.rb', line 241

def send_escape
  send_keys_exact(%(Escape))
end

#send_escapes(number_of_times) ⇒ Object



245
246
247
248
249
250
# File 'lib/ttytest/tmux/session.rb', line 245

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

#send_homeObject



224
225
226
# File 'lib/ttytest/tmux/session.rb', line 224

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



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

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



220
221
222
# File 'lib/ttytest/tmux/session.rb', line 220

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



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

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



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

def send_left_arrow
  send_keys(TTYtest::LEFT_ARROW)
end

#send_left_arrows(number_of_times) ⇒ Object



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

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



60
61
62
63
64
65
66
67
# File 'lib/ttytest/tmux/session.rb', line 60

def send_line(line)
  send_keys_one_at_a_time(line)
  if @use_return_for_newline
    send_return unless ['\n', '\r'].include?(line[-1])
    return
  end
  send_newline unless line[-1] == '\n'
end

#send_line_exact(line) ⇒ Object



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

def send_line_exact(line)
  send_keys_exact(line)
  if @use_return_for_newline
    send_return unless ['\n', '\r'].include?(line[-1])
    return
  end
  send_newline unless line[-1] == '\n'
end

#send_line_exact_then_sleep(line, sleep_time) ⇒ Object

Send line then sleep for sleep_time



104
105
106
107
# File 'lib/ttytest/tmux/session.rb', line 104

def send_line_exact_then_sleep(line, sleep_time)
  send_line_exact(line)
  sleep sleep_time
end

#send_line_exact_then_sleep_and_repeat(*lines, sleep_time) ⇒ Object



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

def send_line_exact_then_sleep_and_repeat(*lines, sleep_time)
  lines.each do |line|
    send_line_exact_then_sleep(line, sleep_time)
  end
end

#send_line_then_sleep(line, sleep_time) ⇒ Object

Send line then sleep for sleep_time



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

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



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

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



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

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

#send_lines_exact(*lines) ⇒ Object



109
110
111
112
113
# File 'lib/ttytest/tmux/session.rb', line 109

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

#send_lines_exact_then_sleep(*lines, sleep_time) ⇒ Object



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

def send_lines_exact_then_sleep(*lines, sleep_time)
  lines.each do |line|
    send_line_exact(line)
  end
  sleep sleep_time
end

#send_lines_then_sleep(*lines, sleep_time) ⇒ Object



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

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

#send_newlineObject



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

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

#send_newlines(number_of_times) ⇒ Object



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

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

#send_returnObject



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

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

#send_returns(number_of_times) ⇒ Object



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

def send_returns(number_of_times)
  while number_of_times.positive?
    send_return
    number_of_times -= 1
  end
end

#send_right_arrowObject



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

def send_right_arrow
  send_keys(TTYtest::RIGHT_ARROW)
end

#send_right_arrows(number_of_times) ⇒ Object



176
177
178
179
180
181
# File 'lib/ttytest/tmux/session.rb', line 176

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

#send_tabObject



252
253
254
# File 'lib/ttytest/tmux/session.rb', line 252

def send_tab
  send_keys_exact(TTYtest::TAB)
end

#send_tabs(number_of_times) ⇒ Object



256
257
258
259
260
261
# File 'lib/ttytest/tmux/session.rb', line 256

def send_tabs(number_of_times)
  while number_of_times.positive?
    send_tab
    number_of_times -= 1
  end
end

#send_up_arrowObject



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

def send_up_arrow
  send_keys(TTYtest::UP_ARROW)
end

#send_up_arrows(number_of_times) ⇒ Object



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

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