Sha256: 3336f07dba6ac7ddbd2cf942ef73cac7e75db224569a3550394017f0c1e17c11

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 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 = []

      if @layout["session"].nil?
        windows = @layout["windows"]
      else
        output << "tmux rename-session #{@layout["session"]["name"]}" if @layout["session"]["name"]
        windows = @layout["session"]["windows"]
      end

      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

          # Support single command splits, but treat it as an array nevertheless
          split["cmd"] = [split["cmd"]] unless split["cmd"].is_a? Array

          # If a `root` key exist, start each split in this directory
          split["cmd"] = ["cd \"#{window["root"]}\""] + split["cmd"] if window.include?("root")

          # Execute each split command
          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

1 entries across 1 versions & 1 rubygems

Version Path
teamocil-0.1.8 lib/teamocil/layout.rb