Module: Lab419::Tmux::Command

Includes:
Options::Api
Defined in:
lib/lab419/tmux/command.rb

Instance Method Summary collapse

Methods included from Options::Api

#has_tmux_session?, #tmux_need_existing_dir

Instance Method Details

#tmux(cmd) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/lab419/tmux/command.rb', line 14

def tmux cmd
  if @options.dry_run
    puts "tmux #{cmd}"
  else
    system "tmux #{cmd}"
  end
end

#tmux_current_windowObject



22
23
24
25
# File 'lib/lab419/tmux/command.rb', line 22

def tmux_current_window
  window = [@options.session_name, tmux_current_window_number].join(":")
  "-t #{window}"
end

#tmux_current_window_command(cmd, *args) ⇒ Object



27
28
29
30
31
# File 'lib/lab419/tmux/command.rb', line 27

def tmux_current_window_command cmd, *args
  args = args.compact
  arguments = args.empty? ? "" : " #{args.join(" ")}"
  tmux "#{cmd} #{tmux_current_window}#{arguments}"
end

#tmux_current_window_numberObject



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

def tmux_current_window_number
  @__tmux_current_window_number__ ||= 0
end

#tmux_current_window_number=(new_val) ⇒ Object



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

def tmux_current_window_number= new_val
  @__tmux_current_window_number__ = new_val
end

#tmux_execute_window_commandsObject



41
42
43
44
45
# File 'lib/lab419/tmux/command.rb', line 41

def tmux_execute_window_commands
  ( @tmux_window_precommands || [] ).each do | blk |
    blk.()
  end
end

#tmux_increase_current_window_number(by = 1) ⇒ Object



47
48
49
# File 'lib/lab419/tmux/command.rb', line 47

def tmux_increase_current_window_number by=1
  self.tmux_current_window_number += by
end

#tmux_new_session(params = nil) ⇒ Object

tmux new-session -s $session_name -n console -d



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lab419/tmux/command.rb', line 52

def tmux_new_session params=nil
  if has_tmux_session?
    tmux "attach -t #{@options.session_name}"
    exit 0
  end
  suffix = params ? " #{params}" : ""
  session_name = @options.session_name
  tmux "new-session -s #{session_name} -d#{suffix}"
  return if @options.no_source_file || @options.tmux_source_file.nil?
  tmux "source-file #{@options.tmux_source_file}"
  tmux "set-window-option -g automatic-rename off"
  tmux_project_home
  at_exit do
    tmux "attach -t #{session_name}"
  end
end

#tmux_new_window(name = nil, dir = nil) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/lab419/tmux/command.rb', line 69

def tmux_new_window name=nil, dir=nil
  tmux_increase_current_window_number
  suffix = name ? %{ -n "#{name}"} : nil
  tmux "new-window -t #{@options.session_name}#{suffix}"
  tmux_set_window_options "automatic-rename off", "allow-rename off"
  tmux_project_home dir
  tmux_execute_window_commands
end

#tmux_open_irb(options = {}) ⇒ Object



87
88
89
90
# File 'lib/lab419/tmux/command.rb', line 87

def tmux_open_irb options={}
  lib = options.delete( :lib ){ "./lib" }
  tmux_send_keys "irb -I#{lib}"
end

#tmux_open_vim(options = {}) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/lab419/tmux/command.rb', line 78

def tmux_open_vim options={}
  dir = options.delete( :dir ){ "." }
  tmux_send_keys "vi #{dir}"
  options.each do | cmd, params |
    command = ":#{[cmd,params].compact.join(" ")}"
  tmux_send_keys command
  end
end

#tmux_prefixed_command(cmd, eol = true) ⇒ Object



92
93
94
# File 'lib/lab419/tmux/command.rb', line 92

def tmux_prefixed_command cmd, eol=true
  tmux_send_keys [@options.cmd_prefix, cmd].mk_string, eol
end

#tmux_project_home(dir = nil) ⇒ Object



96
97
98
99
# File 'lib/lab419/tmux/command.rb', line 96

def tmux_project_home dir=nil
  dir = File.join @options.dir, dir if dir && %r{\A[^/]} === dir
  tmux_send_keys "cd #{dir || @options.dir}" 
end

#tmux_register_window_command(noexec = false, &blk) ⇒ Object



101
102
103
104
105
# File 'lib/lab419/tmux/command.rb', line 101

def tmux_register_window_command noexec=false, &blk
  blk.() unless noexec
  @tmux_window_precommands ||= []
  @tmux_window_precommands << blk
end

#tmux_send_keys(keys, eol = true) ⇒ Object



107
108
109
110
# File 'lib/lab419/tmux/command.rb', line 107

def tmux_send_keys keys, eol=true
  suffix = eol ? " C-m" : nil 
  tmux_current_window_command "send-keys", %{"#{keys}"}, suffix
end

#tmux_set_window_option(option) ⇒ Object



112
113
114
# File 'lib/lab419/tmux/command.rb', line 112

def tmux_set_window_option option
  tmux_current_window_command "set-window-option", option
end

#tmux_set_window_options(*options) ⇒ Object



116
117
118
119
120
# File 'lib/lab419/tmux/command.rb', line 116

def tmux_set_window_options *options
  options.each do | option |
    tmux_set_window_option option
  end
end