Module: MuxTf::Tmux

Extended by:
PiotrbCliUtils::Util
Defined in:
lib/mux_tf/tmux.rb

Class Method Summary collapse

Class Method Details

.attach(name, cc: false) ⇒ Object



53
54
55
# File 'lib/mux_tf/tmux.rb', line 53

def attach(name, cc: false)
  tmux %(#{(cc && '-CC') || ''} attach -t #{name.inspect}), raise_on_error: false
end

.find_pane(name) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/mux_tf/tmux.rb', line 18

def find_pane(name)
  panes = `tmux list-panes -F "\#{pane_id},\#{pane_title}"`.strip.split("\n").map { |row|
    x = row.split(",")
    { id: x[0], name: x[1] }
  }
  panes.find { |pane| pane[:name] == name }
end

.kill_pane(pane_id) ⇒ Object



57
58
59
# File 'lib/mux_tf/tmux.rb', line 57

def kill_pane(pane_id)
  tmux %(kill-pane -t #{pane_id.inspect})
end

.kill_session(name) ⇒ Object



14
15
16
# File 'lib/mux_tf/tmux.rb', line 14

def kill_session(name)
  tmux(%(kill-session -t #{name.inspect}))
end

.list_windowsObject



26
27
28
29
30
31
# File 'lib/mux_tf/tmux.rb', line 26

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) ⇒ Object



33
34
35
# File 'lib/mux_tf/tmux.rb', line 33

def new_session(name)
  tmux %(new-session -s #{name.inspect} -d)
end

.select_pane(name) ⇒ Object



37
38
39
# File 'lib/mux_tf/tmux.rb', line 37

def select_pane(name)
  tmux %(select-pane -T #{name.inspect})
end

.send_keys(cmd, enter: false) ⇒ Object



61
62
63
64
# File 'lib/mux_tf/tmux.rb', line 61

def send_keys(cmd, enter: false)
  tmux %(send-keys #{cmd.inspect})
  tmux %(send-keys Enter) if enter
end

.session_running?(name) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/mux_tf/tmux.rb', line 10

def session_running?(name)
  tmux("has-session -t #{name.inspect} 2>/dev/null", raise_on_error: false)
end

.set(var, value) ⇒ Object



45
46
47
# File 'lib/mux_tf/tmux.rb', line 45

def set(var, value)
  tmux %(set #{var.inspect} #{value.inspect})
end

.set_hook(hook_name, cmd) ⇒ Object



41
42
43
# File 'lib/mux_tf/tmux.rb', line 41

def set_hook(hook_name, cmd)
  tmux %(set-hook #{hook_name.inspect} #{cmd.inspect})
end

.split_window(mode, target_pane, cwd: nil, cmd: nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mux_tf/tmux.rb', line 66

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!Object



49
50
51
# File 'lib/mux_tf/tmux.rb', line 49

def tile!
  tmux "select-layout tiled"
end