module MuxTf::Tmux

Public Class Methods

attach(name, cc: false) click to toggle source
# File lib/mux_tf/tmux.rb, line 51
def attach(name, cc: false)
  tmux %(#{cc && "-CC" || ""} attach -t #{name.inspect}), raise_on_error: false
end
find_pane(name) click to toggle source
# File lib/mux_tf/tmux.rb, line 16
def find_pane(name)
  panes = `tmux list-panes -F "\#{pane_id},\#{pane_title}"`.strip.split("\n").map { |row|
    x = row.split(",")
    return {id: x[0], name: x[1]}
  }
  panes.find { |pane| pane[:name] == name }
end
kill_pane(pane_id) click to toggle source
# File lib/mux_tf/tmux.rb, line 55
def kill_pane(pane_id)
  tmux %(kill-pane -t #{pane_id.inspect})
end
kill_session(name) click to toggle source
# File lib/mux_tf/tmux.rb, line 12
def kill_session(name)
  tmux(%(kill-session -t #{name.inspect}))
end
list_windows() click to toggle source
# File lib/mux_tf/tmux.rb, line 24
def list_windows
  `tmux list-windows -F "\#{window_id},\#{window_index},\#{window_name}"`.strip.split("\n").map do |row|
    x = row.split(",")
    {id: x[0], index: x[1], name: x[2]}
  end
end
new_session(name) click to toggle source
# File lib/mux_tf/tmux.rb, line 31
def new_session(name)
  tmux %(new-session -s #{name.inspect} -d)
end
select_pane(name) click to toggle source
# File lib/mux_tf/tmux.rb, line 35
def select_pane(name)
  tmux %(select-pane -T #{name.inspect})
end
send_keys(cmd, enter: false) click to toggle source
# File lib/mux_tf/tmux.rb, line 59
def send_keys(cmd, enter: false)
  tmux %(send-keys #{cmd.inspect})
  tmux %(send-keys Enter) if enter
end
session_running?(name) click to toggle source
# File lib/mux_tf/tmux.rb, line 8
def session_running?(name)
  tmux("has-session -t #{name.inspect} 2>/dev/null", raise_on_error: false)
end
set(var, value) click to toggle source
# File lib/mux_tf/tmux.rb, line 43
def set(var, value)
  tmux %(set #{var.inspect} #{value.inspect})
end
set_hook(hook_name, cmd) click to toggle source
# File lib/mux_tf/tmux.rb, line 39
def set_hook(hook_name, cmd)
  tmux %(set-hook #{hook_name.inspect} #{cmd.inspect})
end
split_window(mode, target_pane, cwd: nil, cmd: nil) click to toggle source
# File lib/mux_tf/tmux.rb, line 64
def split_window(mode, target_pane, cwd: nil, cmd: nil)
  case mode
  when :horizontal
    mode_part = "-h"
  when :vertical
    mode_part = "-v"
  else
    raise ArgumentError, "invalid mode: #{mode.inspect}"
  end

  parts = [
    "split-window",
    cwd && "-c #{cwd}",
    mode_part,
    "-t #{target_pane.inspect}",
    cmd&.inspect
  ].compact
  tmux parts.join(" ")
end
tile!() click to toggle source
# File lib/mux_tf/tmux.rb, line 47
def tile!
  tmux "select-layout tiled"
end

Private Class Methods

tmux(cmd, raise_on_error: true, mode: :system) click to toggle source
# File lib/mux_tf/tmux.rb, line 90
def tmux(cmd, raise_on_error: true, mode: :system)
  case mode
  when :system
    # puts " => tmux #{cmd}"
    system("#{tmux_bin} #{cmd}")
    unless $CHILD_STATUS.success?
      if raise_on_error
        fail_with("`tmux #{cmd}' failed with code: #{$CHILD_STATUS.exitstatus}")
      end

      return false
    end
    true
  when :exec
    exec tmux_bin, *Shellwords.shellwords(cmd)
  end
end
tmux_bin() click to toggle source
# File lib/mux_tf/tmux.rb, line 86
def tmux_bin
  @tmux_bin ||= `which tmux`.strip
end