comparison lib/pmuxinator/window.rb @ 2:67a6071afec7

Rename tmuxinator -> pmuxinator $ zmv **/*tmuxinator* **/*pmuxinator* $ gsed -e 's/tmuxinator/pmuxinator/g' -i **/*.* $ gsed -e 's/Tmuxinator/Pmuxinator/g' -i **/*.*
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Mon, 21 Jul 2014 08:32:00 +0900
parents
children 4d8f8b2bb7a9
comparison
equal deleted inserted replaced
1:107d94e009cc 2:67a6071afec7
1 module Pmuxinator
2 class Window
3 include Pmuxinator::Util
4
5 attr_reader :name, :panes, :layout, :commands, :index, :project
6
7 def initialize(window_yaml, index, project)
8 @name = !window_yaml.keys.first.nil? ? window_yaml.keys.first.shellescape : nil
9 @panes = []
10 @layout = nil
11 @pre = nil
12 @project = project
13 @index = index
14
15 value = window_yaml.values.first
16
17 if value.is_a?(Hash)
18 @layout = value["layout"] ? value["layout"].shellescape : nil
19 @pre = value["pre"] if value["pre"]
20
21 @panes = build_panes(value["panes"])
22 else
23 @commands = build_commands(tmux_window_command_prefix, value)
24 end
25 end
26
27 def build_panes(panes_yml)
28 Array(panes_yml).map.with_index do |pane_yml, index|
29 if pane_yml.is_a?(Hash)
30 pane_yml.map do |name, commands|
31 Pmuxinator::Pane.new(index, project, self, *commands)
32 end
33 else
34 Pmuxinator::Pane.new(index, project, self, pane_yml)
35 end
36 end.flatten
37 end
38
39 def build_commands(prefix, command_yml)
40 if command_yml.is_a?(Array)
41 command_yml.map do |command|
42 "#{tmux_window_command_prefix} #{command.shellescape} C-m" if command
43 end.compact
44 elsif command_yml.is_a?(String) && !command_yml.empty?
45 ["#{tmux_window_command_prefix} #{command_yml.shellescape} C-m"]
46 else
47 []
48 end
49 end
50
51 def pre
52 if @pre.is_a?(Array)
53 @pre.join(" && ")
54 elsif @pre.is_a?(String)
55 @pre
56 else
57 ""
58 end
59 end
60
61 def panes?
62 panes.any?
63 end
64
65 def tmux_window_target
66 "#{project.name}:#{index + project.base_index}"
67 end
68
69 def tmux_pre_window_command
70 project.pre_window ? "#{project.tmux} send-keys -t #{tmux_window_target} #{project.pre_window.shellescape} C-m" : ""
71 end
72
73 def tmux_window_command_prefix
74 "#{project.tmux} send-keys -t #{project.name}:#{index + project.base_index}"
75 end
76
77 def tmux_new_window_command
78 path = project.root? ? "#{Pmuxinator::Config.default_path_option} #{File.expand_path(project.root).shellescape}" : nil
79 "#{project.tmux} new-window #{path} -t #{tmux_window_target} -n #{name}"
80 end
81
82 def tmux_layout_command
83 "#{project.tmux} select-layout -t #{tmux_window_target} #{layout}"
84 end
85
86 def tmux_select_first_pane
87 "#{project.tmux} select-pane -t #{tmux_window_target}.#{panes.first.index + project.base_index}"
88 end
89 end
90 end