Module: Heytmux::Tmux
- Defined in:
- lib/heytmux/tmux.rb
Overview
Tmux integration
Class Method Summary collapse
-
.capture(window_index, pane_index) ⇒ Object
Captures pane content.
- .create_window(window_name, pane_title, window_options) ⇒ Object
-
.kill(index) ⇒ Object
Kills window.
- .list ⇒ Object
- .paste(window_index, pane_index, keys) ⇒ Object
-
.query(*indexes, print_format) ⇒ Object
Queries tmux.
-
.select_layout(window_index, layout) ⇒ Object
Selects window layout.
-
.select_window(window_index) ⇒ Object
Selects window.
-
.set_pane_title(window_index, pane_index, title) ⇒ Object
Sets the title of the pane.
-
.set_window_options(window_index, opts) ⇒ Object
Applies a set of window options.
-
.split_window(window_index, pane_title) ⇒ Object
Splits the window and returns the index of the new pane.
-
.target(*indexes) ⇒ Object
Returns target identifier for tmux commands.
- .tmux(*args) ⇒ Object
Class Method Details
.capture(window_index, pane_index) ⇒ Object
Captures pane content
103 104 105 106 107 108 109 110 111 |
# File 'lib/heytmux/tmux.rb', line 103 def capture(window_index, pane_index) file = Tempfile.new('heytmux') file.close tmux('capture-pane', target(window_index, pane_index), ';', 'save-buffer', file.path) File.read(file.path).strip ensure file.unlink end |
.create_window(window_name, pane_title, window_options) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/heytmux/tmux.rb', line 33 def create_window(window_name, pane_title, ) tmux(%w[new-window -d -P -F #{window_index} -n], window_name).to_i.tap do |index| (index, ) if pane_title base_index = .fetch('pane-base-index', 0) set_pane_title(index, base_index, pane_title) end end end |
.kill(index) ⇒ Object
Kills window
98 99 100 |
# File 'lib/heytmux/tmux.rb', line 98 def kill(index) tmux('kill-window', target(index)) end |
.list ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/heytmux/tmux.rb', line 20 def list labels = %i[window_index window_name pane_index pane_title] delimiter = '::::' list = tmux(%w[list-panes -s -F], labels.map { |label| "\#{#{label}}" }.join(delimiter)) list.each_line.map do |line| Hash[labels.zip(line.chomp.split(delimiter))].tap do |h| h[:window_index] = h[:window_index].to_i h[:pane_index] = h[:pane_index].to_i end end end |
.paste(window_index, pane_index, keys) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/heytmux/tmux.rb', line 44 def paste(window_index, pane_index, keys) file = Tempfile.new('heytmux') file.puts keys file.close tmux(%w[load-buffer -b heytmux], file.path, %w[; paste-buffer -d -b heytmux], target(window_index, pane_index)) ensure file.unlink end |
.query(*indexes, print_format) ⇒ Object
Queries tmux
93 94 95 |
# File 'lib/heytmux/tmux.rb', line 93 def query(*indexes, print_format) tmux(%w[display-message -p -F], print_format, target(*indexes)) end |
.select_layout(window_index, layout) ⇒ Object
Selects window layout
83 84 85 |
# File 'lib/heytmux/tmux.rb', line 83 def select_layout(window_index, layout) tmux('select-layout', target(window_index), layout) end |
.select_window(window_index) ⇒ Object
Selects window
88 89 90 |
# File 'lib/heytmux/tmux.rb', line 88 def select_window(window_index) tmux('select-window', target(window_index)) end |
.set_pane_title(window_index, pane_index, title) ⇒ Object
Sets the title of the pane
75 76 77 78 79 80 |
# File 'lib/heytmux/tmux.rb', line 75 def set_pane_title(window_index, pane_index, title) # The space at the beginning is for preventing it from being added to # shell history paste(window_index, pane_index, %( sh -c "printf '\\033]2;#{title}\\033\\';clear")) end |
.set_window_options(window_index, opts) ⇒ Object
Applies a set of window options
57 58 59 60 61 62 63 |
# File 'lib/heytmux/tmux.rb', line 57 def (window_index, opts) args = opts.flat_map do |k, v| [';', 'set-window-option', target(window_index), k, { true => 'on', false => 'off' }.fetch(v, v)] end.drop(1) tmux(*args) if args.any? end |
.split_window(window_index, pane_title) ⇒ Object
Splits the window and returns the index of the new pane
66 67 68 69 70 71 72 |
# File 'lib/heytmux/tmux.rb', line 66 def split_window(window_index, pane_title) tmux(%w[split-window -P -F #{pane_index} -d], target(window_index)).to_i.tap do |pane_index| set_pane_title(window_index, pane_index, pane_title) tmux('select-pane', target(window_index, pane_index)) end end |
.target(*indexes) ⇒ Object
Returns target identifier for tmux commands
114 115 116 |
# File 'lib/heytmux/tmux.rb', line 114 def target(*indexes) ['-t', ':' + indexes.join('.')] end |
.tmux(*args) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/heytmux/tmux.rb', line 11 def tmux(*args) args = args.flatten.map { |a| Shellwords.escape(a.to_s) } command = "tmux #{args.join(' ')}" (@mutex ||= Mutex.new).synchronize do puts command if ENV['HEYTMUX_DEBUG'] `#{command}`.chomp end end |