Module: Mxup::Tmux
- Defined in:
- lib/mxup/tmux.rb
Overview
Thin wrapper over the tmux(1) CLI. Every method shells out; nothing here knows about mxup’s own config. Kept as a module so callers can reference Mxup::Tmux.list_panes(…) without instantiating anything.
Class Method Summary collapse
-
.break_pane(session, window, pane_index, new_window_name) ⇒ Object
Break a pane out into its own window.
-
.capture_pane(session, target, _lines = nil) ⇒ Object
Capture the pane’s entire scrollback (-S - is “start of history”) so callers can filter and tail as they see fit.
- .esc(val) ⇒ Object
-
.has_session?(name) ⇒ Boolean
— inspection ——————————————————–.
- .join_pane(session, src_window, dst_window) ⇒ Object
- .kill_session(name) ⇒ Object
- .kill_window(session, name) ⇒ Object
- .list_panes(session) ⇒ Object
- .list_windows(session) ⇒ Object
- .move_window(session, src_name, dst_index) ⇒ Object
-
.new_session(name, first_window_name, root) ⇒ Object
— mutation ———————————————————-.
- .new_window(session, name, root) ⇒ Object
- .pane_count(session, window) ⇒ Object
-
.pane_target(window, pane_index) ⇒ Object
Format a pane target string “window.index” for use in other tmux calls.
- .rename_window(session, old_name, new_name) ⇒ Object
- .select_layout(session, window, layout) ⇒ Object
- .send_interrupt(session, target) ⇒ Object
- .send_keys(session, target, keys) ⇒ Object
- .session_created(name) ⇒ Object
- .set_environment(session, key, value) ⇒ Object
- .set_pane_title(session, window, pane_index, title) ⇒ Object
-
.show_environment(session, key) ⇒ Object
Returns the value stored in the session’s environment, or nil if unset (tmux prefixes unset entries with ‘-’).
- .split_window(session, window, root, target_pane: nil) ⇒ Object
- .swap_window(session, idx_a, idx_b) ⇒ Object
Class Method Details
.break_pane(session, window, pane_index, new_window_name) ⇒ Object
Break a pane out into its own window. Without an explicit -t target, tmux places the new window in the *current client’s* session, which would send it to whichever session the user happens to be attached to when running tests. Always pin the destination to the source session.
83 84 85 86 |
# File 'lib/mxup/tmux.rb', line 83 def break_pane(session, window, pane_index, new_window_name) system("tmux break-pane -s #{esc(session)}:#{esc(window)}.#{pane_index} " \ "-t #{esc(session)}: -n #{esc(new_window_name)} -d") end |
.capture_pane(session, target, _lines = nil) ⇒ Object
Capture the pane’s entire scrollback (-S - is “start of history”) so callers can filter and tail as they see fit. We deliberately don’t pass -S -N here: if the interesting output scrolled past the visible window, a lower bound would silently hide it.
50 51 52 |
# File 'lib/mxup/tmux.rb', line 50 def capture_pane(session, target, _lines = nil) `tmux capture-pane -t #{esc(session)}:#{esc(target)} -p -S - 2>/dev/null` end |
.esc(val) ⇒ Object
139 140 141 |
# File 'lib/mxup/tmux.rb', line 139 def esc(val) Shellwords.escape(val.to_s) end |
.has_session?(name) ⇒ Boolean
— inspection ——————————————————–
14 15 16 |
# File 'lib/mxup/tmux.rb', line 14 def has_session?(name) system("tmux has-session -t #{esc(name)} 2>/dev/null") end |
.join_pane(session, src_window, dst_window) ⇒ Object
75 76 77 |
# File 'lib/mxup/tmux.rb', line 75 def join_pane(session, src_window, dst_window) system("tmux join-pane -s #{esc(session)}:#{esc(src_window)} -t #{esc(session)}:#{esc(dst_window)} -d") end |
.kill_session(name) ⇒ Object
112 113 114 |
# File 'lib/mxup/tmux.rb', line 112 def kill_session(name) system("tmux kill-session -t #{esc(name)}") end |
.kill_window(session, name) ⇒ Object
108 109 110 |
# File 'lib/mxup/tmux.rb', line 108 def kill_window(session, name) system("tmux kill-window -t #{esc(session)}:#{esc(name)}") end |
.list_panes(session) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mxup/tmux.rb', line 30 def list_panes(session) fmt = '#{window_index}|#{window_name}|#{pane_index}|#{pane_pid}|' \ '#{pane_current_path}|#{pane_current_command}|#{pane_title}' `tmux list-panes -t #{esc(session)} -s -F '#{fmt}'` .strip.split("\n").map do |line| win_idx, name, pane_idx, pid, cwd, fg, title = line.split('|', 7) { window_index: win_idx.to_i, name: name, pane_index: pane_idx.to_i, pid: pid.to_i, cwd: cwd, fg_cmd: fg, title: title.to_s } end end |
.list_windows(session) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/mxup/tmux.rb', line 22 def list_windows(session) `tmux list-windows -t #{esc(session)} -F '\#{window_index}|\#{window_name}'` .strip.split("\n").map do |line| idx, name = line.split('|', 2) { index: idx.to_i, name: name } end end |
.move_window(session, src_name, dst_index) ⇒ Object
124 125 126 |
# File 'lib/mxup/tmux.rb', line 124 def move_window(session, src_name, dst_index) system("tmux move-window -s #{esc(session)}:#{esc(src_name)} -t #{esc(session)}:#{dst_index} 2>/dev/null") end |
.new_session(name, first_window_name, root) ⇒ Object
— mutation ———————————————————-
56 57 58 |
# File 'lib/mxup/tmux.rb', line 56 def new_session(name, first_window_name, root) system("tmux new-session -d -s #{esc(name)} -n #{esc(first_window_name)} -c #{esc(root)}") end |
.new_window(session, name, root) ⇒ Object
60 61 62 |
# File 'lib/mxup/tmux.rb', line 60 def new_window(session, name, root) system("tmux new-window -t #{esc(session)} -n #{esc(name)} -c #{esc(root)}") end |
.pane_count(session, window) ⇒ Object
41 42 43 44 |
# File 'lib/mxup/tmux.rb', line 41 def pane_count(session, window) `tmux list-panes -t #{esc(session)}:#{esc(window)} 2>/dev/null` .strip.split("\n").size end |
.pane_target(window, pane_index) ⇒ Object
Format a pane target string “window.index” for use in other tmux calls.
135 136 137 |
# File 'lib/mxup/tmux.rb', line 135 def pane_target(window, pane_index) "#{window}.#{pane_index}" end |
.rename_window(session, old_name, new_name) ⇒ Object
88 89 90 |
# File 'lib/mxup/tmux.rb', line 88 def rename_window(session, old_name, new_name) system("tmux rename-window -t #{esc(session)}:#{esc(old_name)} #{esc(new_name)}") end |
.select_layout(session, window, layout) ⇒ Object
71 72 73 |
# File 'lib/mxup/tmux.rb', line 71 def select_layout(session, window, layout) system("tmux select-layout -t #{esc(session)}:#{esc(window)} #{esc(layout)}") end |
.send_interrupt(session, target) ⇒ Object
120 121 122 |
# File 'lib/mxup/tmux.rb', line 120 def send_interrupt(session, target) system("tmux send-keys -t #{esc(session)}:#{esc(target)} C-c") end |
.send_keys(session, target, keys) ⇒ Object
116 117 118 |
# File 'lib/mxup/tmux.rb', line 116 def send_keys(session, target, keys) system("tmux send-keys -t #{esc(session)}:#{esc(target)} #{esc(keys)} Enter") end |
.session_created(name) ⇒ Object
18 19 20 |
# File 'lib/mxup/tmux.rb', line 18 def session_created(name) `tmux display-message -t #{esc(name)} -p '\#{session_created}'`.strip end |
.set_environment(session, key, value) ⇒ Object
96 97 98 |
# File 'lib/mxup/tmux.rb', line 96 def set_environment(session, key, value) system("tmux set-environment -t #{esc(session)} #{esc(key)} #{esc(value)}") end |
.set_pane_title(session, window, pane_index, title) ⇒ Object
92 93 94 |
# File 'lib/mxup/tmux.rb', line 92 def set_pane_title(session, window, pane_index, title) system("tmux select-pane -t #{esc(session)}:#{esc(window)}.#{pane_index} -T #{esc(title)}") end |
.show_environment(session, key) ⇒ Object
Returns the value stored in the session’s environment, or nil if unset (tmux prefixes unset entries with ‘-’).
102 103 104 105 106 |
# File 'lib/mxup/tmux.rb', line 102 def show_environment(session, key) out = `tmux show-environment -t #{esc(session)} #{esc(key)} 2>/dev/null`.strip return nil if out.empty? || out.start_with?('-') out.split('=', 2).last end |
.split_window(session, window, root, target_pane: nil) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/mxup/tmux.rb', line 64 def split_window(session, window, root, target_pane: nil) target = target_pane \ ? "#{esc(session)}:#{esc(window)}.#{target_pane}" \ : "#{esc(session)}:#{esc(window)}" system("tmux split-window -t #{target} -c #{esc(root)} -d") end |
.swap_window(session, idx_a, idx_b) ⇒ Object
128 129 130 |
# File 'lib/mxup/tmux.rb', line 128 def swap_window(session, idx_a, idx_b) system("tmux swap-window -s #{esc(session)}:#{idx_a} -t #{esc(session)}:#{idx_b}") end |