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



92
93
94
# File 'lib/ttytest/tmux/session.rb', line 92

def send_backspace
  send_keys_exact(%(BSpace))
end

#send_backspaces(number_of_times) ⇒ Object



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

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

#send_clearObject



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

def send_clear
  send_keys_one_at_a_time(TTYtest::CLEAR)
  send_newline
end

#send_deleteObject



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

def send_delete
  send_keys_exact(%(DC))
end

#send_deletes(number_of_times) ⇒ Object



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

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

#send_down_arrowObject



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

def send_down_arrow
  send_keys(TTYtest::DOWN_ARROW)
end

#send_down_arrows(number_of_times) ⇒ Object



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

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

#send_endObject



159
160
161
# File 'lib/ttytest/tmux/session.rb', line 159

def send_end
  send_keys_exact(TTYtest::END_KEY)
end

#send_escapeObject



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

def send_escape
  send_keys_exact(%(Escape))
end

#send_homeObject



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

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



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

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



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

def send_left_arrow
  send_keys(TTYtest::LEFT_ARROW)
end

#send_left_arrows(number_of_times) ⇒ Object



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

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_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_newlineObject



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

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

#send_newlines(number_of_times) ⇒ Object



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

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

#send_right_arrowObject



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

def send_right_arrow
  send_keys(TTYtest::RIGHT_ARROW)
end

#send_right_arrows(number_of_times) ⇒ Object



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

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

#send_up_arrowObject



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

def send_up_arrow
  send_keys(TTYtest::UP_ARROW)
end

#send_up_arrows(number_of_times) ⇒ Object



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

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