Class: RTmux::TmuxHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/rtmux.rb

Overview

tmux helper class

@note example:
  tmux = TmuxHelper.new("some_session_name")

  tmux.create_window("window1")
  tmux.create_window("window2")
  tmux.create_window("logs", cmd: "cd /var/log; ls")

  tmux.split("window1")
  tmux.focus("window1", pane: 2)

  tmux.split("window2", vertical: false, percentage: 30)

  tmux.attach

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_name) {|_self| ... } ⇒ TmuxHelper

initializer

Parameters:

  • session_name (String)

    session name (default: hostname)

Yields:

  • (_self)

Yield Parameters:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rtmux.rb', line 36

def initialize(session_name, &block)
  # check if tmux is installed or not
  if `which tmux`.empty?
    puts "* tmux is not installed on this machine"
    puts "> brew install tmux"
    puts "or"
    puts "> sudo apt-get install tmux"
    exit 1
  end

  @session_name = session_name || `hostname -s`.strip

  yield self if block_given?
end

Instance Attribute Details

#session_nameObject (readonly)

Returns the value of attribute session_name.



109
110
111
# File 'lib/rtmux.rb', line 109

def session_name
  @session_name
end

Instance Method Details

#attachObject

attach to current session



105
106
107
# File 'lib/rtmux.rb', line 105

def attach
  `tmux attach -t #{@session_name}`
end

#cmd(cmd, window, options = {pane: nil}) ⇒ Object

execute command

Parameters:

  • cmd (String)

    command

  • window (String)

    window

  • options (Hash) (defaults to: {pane: nil})

    options



85
86
87
# File 'lib/rtmux.rb', line 85

def cmd(cmd, window, options = {pane: nil})
  `tmux send-keys -t #{@session_name}:#{window}#{options && options[:pane] ? ".#{options[:pane]}" : ""} '#{cmd}' C-m`
end

#create_window(name = nil, options = {cmd: nil}) ⇒ Object

create window with given name and options

Parameters:

  • name (String) (defaults to: nil)

    window name

  • options (Hash) (defaults to: {cmd: nil})

    options



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rtmux.rb', line 68

def create_window(name = nil, options = {cmd: nil})
  if session_created?
    if !window_created?(name)
      `tmux new-window -t #{@session_name} #{name.nil? ? "" : "-n #{name}"}`
    else
      return  # don't create duplicated windows
    end
  else
    `tmux new-session -s #{@session_name} #{name.nil? ? "" : "-n #{name}"} -d`
  end
  cmd(options[:cmd], name) if options && options[:cmd]
end

#focus(window, options = {pane: nil}) ⇒ Object

focus on given window

Parameters:

  • window (String)

    window

  • options (Hash) (defaults to: {pane: nil})

    options



92
93
94
95
# File 'lib/rtmux.rb', line 92

def focus(window, options = {pane: nil})
  `tmux select-window -t #{@session_name}:#{window}`
  `tmux select-pane -t #{options[:pane]}` if options && options[:pane]
end

#session_created?true, false

check if session is already created or not

Returns:

  • (true, false)


53
54
55
56
# File 'lib/rtmux.rb', line 53

def session_created?
  `tmux has-session -t #{@session_name} 2> /dev/null`
  $?.exitstatus != 1
end

#split(window, options = {vertical: true, percentage: 50, pane: nil}) ⇒ Object

split given window

Parameters:

  • window (String)

    window

  • options (Hash) (defaults to: {vertical: true, percentage: 50, pane: nil})

    options



100
101
102
# File 'lib/rtmux.rb', line 100

def split(window, options = {vertical: true, percentage: 50, pane: nil})
  `tmux split-window #{options && options[:vertical] ? "-h" : "-v"} -p #{options[:percentage]} -t #{@session_name}:#{window}#{options && options[:pane] ? ".#{options[:pane]}" : ""}`
end

#window_created?(window_name) ⇒ true, false

check if window is already created or not

Parameters:

  • window_name (String)

    window name

Returns:

  • (true, false)


61
62
63
# File 'lib/rtmux.rb', line 61

def window_created?(window_name)
  window_name ? `tmux list-windows -t #{@session_name} -F \"\#{window_name}\" 2> /dev/null` =~ /^#{window_name}$/ : false
end