Class: FortMux::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/FortMux.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_file_path) ⇒ Config

Returns a new instance of Config.



9
10
11
12
# File 'lib/FortMux.rb', line 9

def initialize config_file_path
  @file = config_file_path
  @yml = YAML::load(File.open(@file))
end

Instance Method Details

#loadObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/FortMux.rb', line 13

def load
  msg = StringIO.open do |sio|
    sio.write "=== #{@file}\n"
    File.open @file do |f|
      sio.write f.read
    end
    sio.write "\n=== yml\n"
    PP.pp @yml,sio
    sio.string
  end
  FortMux::Log::logger msg

  cmd_out = `tmux start-server` # Assuming no harm if server is already running
  tmux_status  = Status.new
  unless @yml.has_key? "sessions"
    puts "No sessions to load"
    return
  end
  @yml["sessions"].each do |session|
    window_count = tmux_status.window_count session["name"]
    session["windows"].each do |window|
      if tmux_status.find session["name"], window["name"]
        puts "Window #{session['name']}:#{window['name']} already loaded"
      else
        window_count += 1
        if tmux_status.find session["name"]
          cmd_out = `tmux new-window -d -t#{session["name"]}:#{window_count} -n #{window["name"]}`
        else
          cmd_out = `tmux new-session -d -n #{window["name"]} -s #{session["name"]}`
        end
        puts "  window: #{window["name"]}"
        window["commands"].each do |command|
          cmd_out = %x[tmux send-keys -t#{session["name"]}:#{window["name"]}.0 '#{command}' C-m]
        end
        pane_count = 0
        window["panes"].each do |pane|
          pane_count += 1
          puts "  tmux split-window #{pane["options"]} -t#{session["name"]}:#{window["name"]}"
          cmd_out = `tmux split-window #{pane["options"]} -t#{session["name"]}:#{window["name"]}`
          cmd_out = %x[tmux select-pane -t #{session["name"]}:#{window["name"]}.#{pane_count}]
          pane["commands"].each do |command|
            cmd_out = %x[tmux send-keys -t#{session["name"]}:#{window["name"]}.#{pane_count} '#{command}' C-m]
          end
        end
      end
    end
  end
end