Module: MuxTf::Tmux
- Extended by:
- PiotrbCliUtils::Util
- Defined in:
- lib/mux_tf/tmux.rb
Class Method Summary collapse
- .attach(name, cc: false, control: false) ⇒ Object
- .attach_control(name, on_line:, on_spawn:) ⇒ Object
- .find_pane(name) ⇒ Object
- .kill_pane(pane_id) ⇒ Object
- .kill_session(name) ⇒ Object
- .list_panes ⇒ Object
- .list_windows ⇒ Object
- .new_session(name, cwd: nil, cmd: nil) ⇒ Object
- .select_pane(name) ⇒ Object
- .send_keys(cmd, enter: false) ⇒ Object
- .session_running?(name) ⇒ Boolean
- .set(var, value) ⇒ Object
- .set_hook(hook_name, cmd) ⇒ Object
- .split_window(mode, target_pane, cwd: nil, cmd: nil) ⇒ Object
- .tile! ⇒ Object
Class Method Details
.attach(name, cc: false, control: false) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/mux_tf/tmux.rb', line 77 def attach(name, cc: false, control: false) parts = [ cc ? "-CC" : nil, control ? "-C" : nil, "attach", "-t #{name.inspect}" ].compact tmux parts.join(" "), raise_on_error: false end |
.attach_control(name, on_line:, on_spawn:) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/mux_tf/tmux.rb', line 68 def attach_control(name, on_line:, on_spawn:) parts = [ "-C", "attach", "-t #{name.inspect}" ].compact tmux parts.join(" "), raise_on_error: false, mode: :popen, on_line: on_line, on_spawn: on_spawn end |
.find_pane(name) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/mux_tf/tmux.rb', line 19 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
87 88 89 |
# File 'lib/mux_tf/tmux.rb', line 87 def kill_pane(pane_id) tmux %(kill-pane -t #{pane_id.inspect}) end |
.kill_session(name) ⇒ Object
15 16 17 |
# File 'lib/mux_tf/tmux.rb', line 15 def kill_session(name) tmux(%(kill-session -t #{name.inspect})) end |
.list_panes ⇒ Object
27 28 29 30 31 32 |
# File 'lib/mux_tf/tmux.rb', line 27 def list_panes `tmux list-panes -F "\#{pane_id},\#{pane_index},\#{pane_title}"`.strip.split("\n").map do |row| x = row.split(",") { id: x[0], index: x[1], name: x[2] } end end |
.list_windows ⇒ Object
34 35 36 37 38 39 |
# File 'lib/mux_tf/tmux.rb', line 34 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, cwd: nil, cmd: nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mux_tf/tmux.rb', line 41 def new_session(name, cwd: nil, cmd: nil) parts = [ "new-session", "-s #{name.inspect}", "-d", cwd ? "-c #{cwd}" : nil, cmd&.inspect ].compact tmux parts.join(" ") end |
.select_pane(name) ⇒ Object
52 53 54 |
# File 'lib/mux_tf/tmux.rb', line 52 def select_pane(name) tmux %(select-pane -T #{name.inspect}) end |
.send_keys(cmd, enter: false) ⇒ Object
91 92 93 94 |
# File 'lib/mux_tf/tmux.rb', line 91 def send_keys(cmd, enter: false) tmux %(send-keys #{cmd.inspect}) tmux %(send-keys Enter) if enter end |
.session_running?(name) ⇒ Boolean
11 12 13 |
# File 'lib/mux_tf/tmux.rb', line 11 def session_running?(name) tmux("has-session -t #{name.inspect} 2>/dev/null", raise_on_error: false) end |
.set(var, value) ⇒ Object
60 61 62 |
# File 'lib/mux_tf/tmux.rb', line 60 def set(var, value) tmux %(set #{var.inspect} #{value.inspect}) end |
.set_hook(hook_name, cmd) ⇒ Object
56 57 58 |
# File 'lib/mux_tf/tmux.rb', line 56 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mux_tf/tmux.rb', line 96 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}" : nil, mode_part, "-t #{target_pane.inspect}", cmd&.inspect ].compact tmux parts.join(" ") end |
.tile! ⇒ Object
64 65 66 |
# File 'lib/mux_tf/tmux.rb', line 64 def tile! tmux "select-layout tiled" end |