Class: Tmuxinator::Window

Inherits:
Object show all
Includes:
Util
Defined in:
lib/tmuxinator/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#exit!, #yes_no

Constructor Details

#initialize(window_yaml, index, project) ⇒ Window

Returns a new instance of Window.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tmuxinator/window.rb', line 7

def initialize(window_yaml, index, project)
  @name = if !window_yaml.keys.first.nil?
            window_yaml.keys.first.to_s.shellescape
          end
  @root = nil
  @panes = []
  @layout = nil
  @pre = nil
  @project = project
  @index = index

  value = window_yaml.values.first

  if value.is_a?(Hash)
    @layout = value["layout"] ? value["layout"].shellescape : nil
    @pre = value["pre"] if value["pre"]
    @root = if value["root"]
              File.expand_path(value["root"]).shellescape
            elsif project.root?
              project.root
            end

    @panes = build_panes(value["panes"])
  else
    @commands = build_commands(tmux_window_command_prefix, value)
  end
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/tmuxinator/window.rb', line 5

def commands
  @commands
end

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/tmuxinator/window.rb', line 5

def index
  @index
end

#layoutObject (readonly)

Returns the value of attribute layout.



5
6
7
# File 'lib/tmuxinator/window.rb', line 5

def layout
  @layout
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/tmuxinator/window.rb', line 5

def name
  @name
end

#panesObject (readonly)

Returns the value of attribute panes.



5
6
7
# File 'lib/tmuxinator/window.rb', line 5

def panes
  @panes
end

#projectObject (readonly)

Returns the value of attribute project.



5
6
7
# File 'lib/tmuxinator/window.rb', line 5

def project
  @project
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/tmuxinator/window.rb', line 5

def root
  @root
end

Instance Method Details

#build_commands(_prefix, command_yml) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tmuxinator/window.rb', line 47

def build_commands(_prefix, command_yml)
  if command_yml.is_a?(Array)
    command_yml.map do |command|
      "#{tmux_window_command_prefix} #{command.shellescape} C-m" if command
    end.compact
  elsif command_yml.is_a?(String) && !command_yml.empty?
    ["#{tmux_window_command_prefix} #{command_yml.shellescape} C-m"]
  else
    []
  end
end

#build_panes(panes_yml) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tmuxinator/window.rb', line 35

def build_panes(panes_yml)
  Array(panes_yml).map.with_index do |pane_yml, index|
    if pane_yml.is_a?(Hash)
      pane_yml.map do |_name, commands|
        Tmuxinator::Pane.new(index, project, self, *commands)
      end
    else
      Tmuxinator::Pane.new(index, project, self, pane_yml)
    end
  end.flatten
end

#panes?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/tmuxinator/window.rb', line 71

def panes?
  panes.any?
end

#preObject



59
60
61
62
63
64
65
# File 'lib/tmuxinator/window.rb', line 59

def pre
  if @pre.is_a?(Array)
    @pre.join(" && ")
  elsif @pre.is_a?(String)
    @pre
  end
end

#root?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/tmuxinator/window.rb', line 67

def root?
  !root.nil?
end

#tmux_layout_commandObject



102
103
104
# File 'lib/tmuxinator/window.rb', line 102

def tmux_layout_command
  "#{project.tmux} select-layout -t #{tmux_window_target} #{layout}"
end

#tmux_new_window_commandObject



93
94
95
96
# File 'lib/tmuxinator/window.rb', line 93

def tmux_new_window_command
  path = root? ? "#{Tmuxinator::Config.default_path_option} #{root}" : nil
  "#{project.tmux} new-window #{path} -t #{tmux_window_target} #{tmux_window_name_option}"
end

#tmux_pre_window_commandObject



79
80
81
82
83
# File 'lib/tmuxinator/window.rb', line 79

def tmux_pre_window_command
  return unless project.pre_window

  "#{project.tmux} send-keys -t #{tmux_window_target} #{project.pre_window.shellescape} C-m"
end

#tmux_select_first_paneObject



106
107
108
# File 'lib/tmuxinator/window.rb', line 106

def tmux_select_first_pane
  "#{project.tmux} select-pane -t #{tmux_window_target}.#{panes.first.index + project.base_index}"
end

#tmux_tiled_layout_commandObject



98
99
100
# File 'lib/tmuxinator/window.rb', line 98

def tmux_tiled_layout_command
  "#{project.tmux} select-layout -t #{tmux_window_target} tiled"
end

#tmux_window_command_prefixObject



85
86
87
# File 'lib/tmuxinator/window.rb', line 85

def tmux_window_command_prefix
  "#{project.tmux} send-keys -t #{project.name}:#{index + project.base_index}"
end

#tmux_window_name_optionObject



89
90
91
# File 'lib/tmuxinator/window.rb', line 89

def tmux_window_name_option
  name ? "-n #{name}" : ""
end

#tmux_window_targetObject



75
76
77
# File 'lib/tmuxinator/window.rb', line 75

def tmux_window_target
  "#{project.name}:#{index + project.base_index}"
end