class RTmux::TmuxHelper

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

Attributes

session_name[R]

Public Class Methods

new(session_name) { |self| ... } click to toggle source

initializer @param session_name [String] session name (default: hostname)

# 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

Public Instance Methods

attach() click to toggle source

attach to current session

# File lib/rtmux.rb, line 105
def attach
  `tmux attach -t #{@session_name}`
end
cmd(cmd, window, options = {pane: nil}) click to toggle source

execute command @param cmd [String] command @param window [String] window @param options [Hash] options

# 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}) click to toggle source

create window with given name and options @param name [String] window name @param options [Hash] options

# 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}) click to toggle source

focus on given window @param window [String] window @param options [Hash] options

# 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?() click to toggle source

check if session is already created or not @return [true,false]

# 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}) click to toggle source

split given window @param window [String] window @param options [Hash] options

# 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) click to toggle source

check if window is already created or not @param window_name [String] window name @return [true,false]

# 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