class JustTmux

2019 Nov 06: Bug fix: The sleep variable is now sleepx 2019 Nov 04: Bug fix: The sleep variable is now explicitly set to a float 2018 Aug 05: Minor modification: Changed to using Dynarex#to_a instead of Dynarex#to_h. 2017 Jan 07: Added post_shell to allow a command to execute after the

new shell has been created e.g. rvm  use 2.4.0

Attributes

to_s[R]

Public Class Methods

new(dx, debug: false) click to toggle source
example raw dynarex file:

config=<<EOF
<?dynarex schema="tmux[session, shell, default_dir]/window(command, name, dir)"?>
session: fun
shell: bash
default_dir: ~

lis
pwd  home  /home/james/ruby

EOF

# File lib/just-tmux.rb, line 33
def initialize(dx, debug: false)
  
  @debug = debug

  dynarex = dx.is_a?(Dynarex) ? dx : Dynarex.new(dx)

  puts ('dynarex.summary: ' + dynarex.summary.inspect).debug if @debug
  
  shell = if dynarex.summary[:shell].nil? or 
      dynarex.summary[:shell].empty? then
  
    dynarex.summary[:shell] = 'bash' 
    
  else
    dynarex.summary[:shell]
  end
  
  puts 'shell: ' + shell.inspect if @debug
  post_shell = CGI.unescape(dynarex.summary[:post_shell].to_s) || ''
  default_dir = File.expand_path dynarex.summary[:default_dir] || '~'
  session_name = dynarex.summary[:session] || '0'
  @detach = dynarex.summary[:detach].to_s.downcase == 'false' ? \
      false : true
  puts ('@detach: ' + @detach.inspect).debug if @debug

  h = dynarex.to_a.map do |x| 
    x[:name]  = shell        if x[:name].nil?  or  x[:name].empty?
    x[:dir]   = default_dir  if x[:dir].nil?   or  x[:dir].empty?
    x[:sleepx] = 0            if x[:sleepx].nil?
    x
  end
  
  @to_s = new_session(session_name, h[0][:name], shell, post_shell) +  
      send_keys("cd #{h[0][:dir]} && " + h[0][:command]) + 
      wait(h[0][:sleepx]) + 
    h[1..-1].map do |x| 
      new_window(x[:name],  shell, post_shell) + 
          send_keys("cd #{x[:dir]} && " + x[:command]) + wait(x[:sleepx]) 
      end.join

end

Private Instance Methods

new_session(session_name, window_name, shell, command) click to toggle source
# File lib/just-tmux.rb, line 77
def new_session(session_name, window_name, shell, command)
  
  d = @detach ? '-d' : ''
  "tmux new-session %s -s %s -n %s '%s'\n%s" % 
    [d, session_name, window_name, shell, send_keys(command)]
end
new_window(window_name, shell, command) click to toggle source
# File lib/just-tmux.rb, line 84
def new_window(window_name, shell, command)
  "tmux new-window -n %s %s\n%s" % [window_name, shell, send_keys(command)]
end
send_keys(command) click to toggle source
# File lib/just-tmux.rb, line 88
def send_keys(command)  "tmux send-keys '%s' Enter\n" % command   end
wait(duration) click to toggle source
# File lib/just-tmux.rb, line 89
def wait(duration)      "sleep %s\n"             % duration.to_f  end