Class: Teamocil::Layout

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

Overview

This class act as a wrapper around a tmux YAML layout file.

Instance Method Summary (collapse)

Constructor Details

- (Layout) initialize(file, options)

Initialize a new layout from a file

Parameters:

  • file (String)

    the complete layout file path

  • options (Hash)


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

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

Instance Method Details

- (Object) execute_commands(commands)

Execute each command in the shell

Parameters:

  • commands (Array)

    an array of complete commands to send to the shell



73
74
75
# File 'lib/teamocil/layout.rb', line 73

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

- (Object) generate_commands

Generate tmux commands based on the data found in the layout file



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
61
62
63
64
65
66
67
68
# File 'lib/teamocil/layout.rb', line 21

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")
          cmd = "tmux split-window -h -p #{split["width"]}"
        elsif split.include?("height")
          cmd = "tmux split-window -p #{split["height"]}"
        else
          cmd = "tmux split-window"
        end
        cmd << " -t #{split["target"]}" if split.include?("target")
        output << cmd
      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

- (Object) to_tmux

Generate commands and sends them to tmux



15
16
17
18
# File 'lib/teamocil/layout.rb', line 15

def to_tmux 
  commands = generate_commands
  execute_commands(commands)
end