Sha256: e4bd589c98404fb70acc280679a4599a5e7752b59999c56dfab3f6bff905411f

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

module Teamocil
  class Layout

    attr_accessor :layout, :options

    def initialize(file, options) # {{{
      @layout = YAML.load_file(file)
      @options = options
    end # }}}

    def to_tmux # {{{
      commands = generate_commands
      execute_commands(commands)
    end # }}}

    def generate_commands # {{{
      output = []

      @layout["windows"].each_with_index do |window, window_index|

        if options.include?(:here) and window_index == 0
          output << "tmux rename-window #{window["name"]}"
        else
          output << "tmux new-window -n #{window["name"]}"
        end
        window["splits"].each_with_index do |split, index|
          unless index == 0
            if split.include?("width")
              output << "tmux split-window -h -p #{split["width"]}"
            else
              if split.include?("height")
                output << "tmux split-window -p #{split["height"]}"
              else
                output << "tmux split-window"
              end
            end
          end

          split["cmd"] = [split["cmd"]] unless split["cmd"].is_a? Array
          split["cmd"].each do |command|
            output << "tmux send-keys -t #{index} \"#{command}\""
            output << "tmux send-keys -t #{index} Enter"
          end
        end

      end

      output << "tmux select-pane -t 0"
    end # }}}

    def execute_commands(commands) # {{{
      `#{commands.join("; ")}`
    end # }}}

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
teamocil-0.1.5 lib/teamocil/layout.rb
teamocil-0.1.4 lib/teamocil/layout.rb
teamocil-0.1.3 lib/teamocil/layout.rb