Class: TmuxConnector::TmuxSession

Inherits:
Object
  • Object
show all
Defined in:
lib/tmux-connector/tmux_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ TmuxSession

Returns a new instance of TmuxSession.



19
20
21
22
23
24
# File 'lib/tmux-connector/tmux_handler.rb', line 19

def initialize(session)
  @session = session

  @name = session.name
  @commands = []
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



17
18
19
# File 'lib/tmux-connector/tmux_handler.rb', line 17

def commands
  @commands
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/tmux-connector/tmux_handler.rb', line 15

def name
  @name
end

#sessionObject (readonly)

Returns the value of attribute session.



16
17
18
# File 'lib/tmux-connector/tmux_handler.rb', line 16

def session
  @session
end

Instance Method Details

#send_commands(send_commands, server_regex, group_regex, window, opts = {}) ⇒ Object



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
# File 'lib/tmux-connector/tmux_handler.rb', line 39

def send_commands(send_commands, server_regex, group_regex, window, opts={})
  predicate = opts[:filter_predicate]

  count = 0
  each_pane do |window_index, pane_index, pane|
    if window
      matches = window == window_index.to_s
    else
      matches = server_regex.nil? && group_regex.nil?
      matches ||= !group_regex.nil? && pane.host.group_id.match(group_regex)
      matches ||= !group_regex.nil? && session.merge_rules[pane.host.group_id].match(group_regex)

      unless server_regex.nil?
        name_match = pane.host.ssh_name.match(server_regex)

        if predicate
          begin
            matches ||= name_match && predicate.call(pane.host.sort_value)
          rescue
            raise "error while using send predicate for '#{ pane.host.display_name }'; command already sent to #{ count } servers"
          end
        else
          matches ||= name_match
        end
      end
    end

    if matches
      system("tmux send-keys -t #{ name }:#{ window_index }.#{ pane_index } '#{ send_commands }' C-m")
      count += 1
    end
  end

  puts "command sent to #{ count } server[s]" if opts[:verbose]
end

#start_sessionObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tmux-connector/tmux_handler.rb', line 26

def start_session()
  create_session
  create_windows
  create_panes
  clear_panes

  connect

  attach_to_session

  execute
end