Class: RTmux::TmuxHelper
- Inherits:
-
Object
- Object
- RTmux::TmuxHelper
- Defined in:
- lib/rtmux.rb
Overview
tmux helper class
@note example:
tmux = TmuxHelper.new("some_session_name")
tmux.create_window("window1")
tmux.create_window("window2")
tmux.create_window("logs", cmd: "cd /var/log; ls")
tmux.split("window1")
tmux.focus("window1", pane: 2)
tmux.split("window2", vertical: false, percentage: 30)
tmux.attach
Instance Attribute Summary collapse
-
#session_name ⇒ Object
readonly
Returns the value of attribute session_name.
Instance Method Summary collapse
-
#attach ⇒ Object
attach to current session.
-
#cmd(cmd, window, options = {pane: nil}) ⇒ Object
execute command.
-
#create_window(name = nil, options = {cmd: nil}) ⇒ Object
create window with given name and options.
-
#focus(window, options = {pane: nil}) ⇒ Object
focus on given window.
-
#initialize(session_name) {|_self| ... } ⇒ TmuxHelper
constructor
initializer.
-
#session_created? ⇒ true, false
check if session is already created or not.
-
#split(window, options = {vertical: true, percentage: 50, pane: nil}) ⇒ Object
split given window.
-
#window_created?(window_name) ⇒ true, false
check if window is already created or not.
Constructor Details
#initialize(session_name) {|_self| ... } ⇒ TmuxHelper
initializer
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rtmux.rb', line 36 def initialize(session_name, &block) # check if tmux is installed or not if `which tmux`.empty? puts "* tmux is not installed on this machine" puts "> brew install tmux" puts "or" puts "> sudo apt-get install tmux" exit 1 end @session_name = session_name || `hostname -s`.strip yield self if block_given? end |
Instance Attribute Details
#session_name ⇒ Object (readonly)
Returns the value of attribute session_name.
109 110 111 |
# File 'lib/rtmux.rb', line 109 def session_name @session_name end |
Instance Method Details
#attach ⇒ Object
attach to current session
105 106 107 |
# File 'lib/rtmux.rb', line 105 def attach `tmux attach -t #{@session_name}` end |
#cmd(cmd, window, options = {pane: nil}) ⇒ Object
execute command
85 86 87 |
# File 'lib/rtmux.rb', line 85 def cmd(cmd, window, = {pane: nil}) `tmux send-keys -t #{@session_name}:#{window}#{ && [:pane] ? ".#{[:pane]}" : ""} '#{cmd}' C-m` end |
#create_window(name = nil, options = {cmd: nil}) ⇒ Object
create window with given name and options
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rtmux.rb', line 68 def create_window(name = nil, = {cmd: nil}) if session_created? if !window_created?(name) `tmux new-window -t #{@session_name} #{name.nil? ? "" : "-n #{name}"}` else return # don't create duplicated windows end else `tmux new-session -s #{@session_name} #{name.nil? ? "" : "-n #{name}"} -d` end cmd([:cmd], name) if && [:cmd] end |
#focus(window, options = {pane: nil}) ⇒ Object
focus on given window
92 93 94 95 |
# File 'lib/rtmux.rb', line 92 def focus(window, = {pane: nil}) `tmux select-window -t #{@session_name}:#{window}` `tmux select-pane -t #{[:pane]}` if && [:pane] end |
#session_created? ⇒ true, false
check if session is already created or not
53 54 55 56 |
# File 'lib/rtmux.rb', line 53 def session_created? `tmux has-session -t #{@session_name} 2> /dev/null` $?.exitstatus != 1 end |
#split(window, options = {vertical: true, percentage: 50, pane: nil}) ⇒ Object
split given window
100 101 102 |
# File 'lib/rtmux.rb', line 100 def split(window, = {vertical: true, percentage: 50, pane: nil}) `tmux split-window #{ && [:vertical] ? "-h" : "-v"} -p #{[:percentage]} -t #{@session_name}:#{window}#{ && [:pane] ? ".#{[:pane]}" : ""}` end |
#window_created?(window_name) ⇒ true, false
check if window is already created or not
61 62 63 |
# File 'lib/rtmux.rb', line 61 def window_created?(window_name) window_name ? `tmux list-windows -t #{@session_name} -F \"\#{window_name}\" 2> /dev/null` =~ /^#{window_name}$/ : false end |