Class: Teamocil::Tmux::Window

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Window

Returns a new instance of Window.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/teamocil/tmux/window.rb', line 4

def initialize(object)
  super

  # Make sure paths like `~/foo/bar` work
  self.root = File.expand_path(root) if root

  self.panes ||= []
  self.panes = panes.each_with_index.map do |pane, index|
    # Support single command instead of `commands` key in Hash
    pane = { commands: [pane] } if pane.is_a?(String)

    # Panes need to know their position
    pane.merge! index: index

    # Panes need to know the window root directory
    pane.merge! root: root

    # Panes need to know the window layout
    pane.merge! layout: layout

    # Panes need to know the window name
    pane.merge! name: name

    Teamocil::Tmux::Pane.new(pane)
  end
end

Class Method Details

.window_base_indexObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/teamocil/tmux/window.rb', line 67

def self.window_base_index
  @window_base_index ||= begin
    base_index = Teamocil::Tmux.option('base-index', default: 0)
    current_window_count = Teamocil::Tmux.window_count

    # If `--here` is specified, treat the current window as a new one
    current_window_count -= 1 if Teamocil.options[:here]

    base_index + current_window_count
  end
end

Instance Method Details

#as_tmuxObject



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
# File 'lib/teamocil/tmux/window.rb', line 31

def as_tmux
  [].tap do |tmux|
    # Rename the current window or create a new one
    if Teamocil.options[:here] && first?
      if root
        first_pane_index = panes.first.internal_index
        tmux << Teamocil::Command::SendKeysToPane.new(index: first_pane_index, keys: %(cd "#{root}"))
        tmux << Teamocil::Command::SendKeysToPane.new(index: first_pane_index, keys: 'Enter')
      end

      tmux << Teamocil::Command::RenameWindow.new(name: name)
    else
      tmux << Teamocil::Command::NewWindow.new(name: name, root: root)
    end

    # Set window options
    if options
      tmux << options.map do |(option, value)|
        Teamocil::Command::SetWindowOption.new(name: name, option: option, value: value)
      end
    end

    # Execute all panes commands
    tmux << panes.map(&:as_tmux).flatten

    # Set the focus on the right pane or the first one
    focused_pane = panes.find(&:focus)
    focused_index = focused_pane ? focused_pane.internal_index : "#{name}.#{Teamocil::Tmux::Pane.pane_base_index}"
    tmux << Teamocil::Command::SelectPane.new(index: focused_index)
  end.flatten
end

#internal_indexObject



63
64
65
# File 'lib/teamocil/tmux/window.rb', line 63

def internal_index
  index + self.class.window_base_index
end