From 22e4bc1d43762b8301a76b478045a043b62072a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81mi=20Pre=CC=81vost?= Date: Sat, 3 Mar 2012 13:38:53 -0500 Subject: [PATCH] Use separate files for Teamocil::Layout subclasses --- lib/teamocil/layout.rb | 137 +-------------------------------- lib/teamocil/layout/session.rb | 31 ++++++++ lib/teamocil/layout/split.rb | 61 +++++++++++++++ lib/teamocil/layout/window.rb | 55 +++++++++++++ 4 files changed, 151 insertions(+), 133 deletions(-) create mode 100644 lib/teamocil/layout/session.rb create mode 100644 lib/teamocil/layout/split.rb create mode 100644 lib/teamocil/layout/window.rb diff --git a/lib/teamocil/layout.rb b/lib/teamocil/layout.rb index a9be37a..0c4f2d4 100644 --- a/lib/teamocil/layout.rb +++ b/lib/teamocil/layout.rb @@ -2,141 +2,12 @@ module Teamocil # This class act as a wrapper around a tmux YAML layout file class Layout + autoload :Session, "teamocil/layout/session" + autoload :Window, "teamocil/layout/window" + autoload :Split, "teamocil/layout/split" + attr_reader :session - # This class represents a session within tmux - class Session - attr_reader :options, :windows, :name - - # Initialize a new tmux session - # - # @param options [Hash] the options, mostly passed by the CLI - # @param attrs [Hash] the session data from the layout file - def initialize(options, attrs={}) # {{{ - @name = attrs["name"] - @windows = attrs["windows"].each_with_index.map { |window, window_index| Window.new(self, window_index, window) } - @options = options - end # }}} - - # Generate commands to send to tmux - # - # @return [Array] - def generate_commands # {{{ - commands = [] - commands << "tmux rename-session \"#{@name}\"" unless @name.nil? - commands << @windows.map(&:generate_commands) - commands << "tmux select-pane -t 0" - commands - end # }}} - - end - - # This class represents a window within tmux - class Window - attr_reader :filters, :root, :splits, :options, :index, :name - - # Initialize a new tmux window - # - # @param session [Session] the session where the window is initialized - # @param index [Fixnnum] the window index - # @param attrs [Hash] the window data from the layout file - def initialize(session, index, attrs={}) # {{{ - @name = attrs["name"] - @root = attrs["root"] - @options = attrs["options"] || {} - - @splits = attrs["splits"] || [] - @splits = @splits.each_with_index.map { |split, split_index| Split.new(self, split_index, split) } - - @filters = attrs["filters"] || {} - @filters["before"] ||= [] - @filters["after"] ||= [] - - @index = index - @session = session - end # }}} - - # Generate commands to send to tmux - # - # @return [Array] - def generate_commands # {{{ - commands = [] - - if @session.options.include?(:here) and @index == 0 - commands << "tmux rename-window \"#{@name}\"" - else - commands << "tmux new-window -n \"#{@name}\"" - end - - commands << @splits.map(&:generate_commands) - - @options.each_pair do |option, value| - value = "on" if value === true - value = "off" if value === false - commands << "tmux set-window-option #{option} #{value}" - end - - commands - end # }}} - - end - - # This class represents a split within a tmux window - class Split - attr_reader :width, :height, :cmd, :index, :target - - # Initialize a new tmux split - # - # @param session [Session] the window where the split is initialized - # @param index [Fixnnum] the split index - # @param attrs [Hash] the split data from the layout file - def initialize(window, index, attrs={}) # {{{ - @height = attrs["height"] - @width = attrs["width"] - @cmd = attrs["cmd"] - @target = attrs["target"] - - @window = window - @index = index - end # }}} - - # Generate commands to send to tmux - # - # @return [Array] - def generate_commands # {{{ - commands = [] - - # Is it a vertical or horizontal split? - init_command = "" - unless @index == 0 - if !@width.nil? - init_command = "tmux split-window -h -p #{@width}" - elsif !@height.nil? - init_command = "tmux split-window -p #{@height}" - else - init_command = "tmux split-window" - end - init_command << " -t #{@target}" unless @target.nil? - commands << init_command - end - - # Wrap all commands around filters - @cmd = [@window.filters["before"]] + [@cmd] + [@window.filters["after"]] - - # If a `root` key exist, start each split in this directory - @cmd.unshift "cd \"#{@window.root}\"" unless @window.root.nil? - - # Execute each split command - @cmd.flatten.compact.each do |command| - commands << "tmux send-keys -t #{@index} \"#{command}\"" - commands << "tmux send-keys -t #{@index} Enter" - end - - commands - end # }}} - - end - # Initialize a new layout from a hash # # @param layout [Hash] the parsed layout diff --git a/lib/teamocil/layout/session.rb b/lib/teamocil/layout/session.rb new file mode 100644 index 0000000..344b6d0 --- /dev/null +++ b/lib/teamocil/layout/session.rb @@ -0,0 +1,31 @@ +module Teamocil + class Layout + + # This class represents a session within tmux + class Session + attr_reader :options, :windows, :name + + # Initialize a new tmux session + # + # @param options [Hash] the options, mostly passed by the CLI + # @param attrs [Hash] the session data from the layout file + def initialize(options, attrs={}) # {{{ + @name = attrs["name"] + @windows = attrs["windows"].each_with_index.map { |window, window_index| Window.new(self, window_index, window) } + @options = options + end # }}} + + # Generate commands to send to tmux + # + # @return [Array] + def generate_commands # {{{ + commands = [] + commands << "tmux rename-session \"#{@name}\"" unless @name.nil? + commands << @windows.map(&:generate_commands) + commands << "tmux select-pane -t 0" + commands + end # }}} + + end + end +end diff --git a/lib/teamocil/layout/split.rb b/lib/teamocil/layout/split.rb new file mode 100644 index 0000000..be65f9f --- /dev/null +++ b/lib/teamocil/layout/split.rb @@ -0,0 +1,61 @@ +module Teamocil + class Layout + + # This class represents a split within a tmux window + class Split + attr_reader :width, :height, :cmd, :index, :target + + # Initialize a new tmux split + # + # @param session [Session] the window where the split is initialized + # @param index [Fixnnum] the split index + # @param attrs [Hash] the split data from the layout file + def initialize(window, index, attrs={}) # {{{ + @height = attrs["height"] + @width = attrs["width"] + @cmd = attrs["cmd"] + @target = attrs["target"] + + @window = window + @index = index + end # }}} + + # Generate commands to send to tmux + # + # @return [Array] + def generate_commands # {{{ + commands = [] + + # Is it a vertical or horizontal split? + init_command = "" + unless @index == 0 + if !@width.nil? + init_command = "tmux split-window -h -p #{@width}" + elsif !@height.nil? + init_command = "tmux split-window -p #{@height}" + else + init_command = "tmux split-window" + end + init_command << " -t #{@target}" unless @target.nil? + commands << init_command + end + + # Wrap all commands around filters + @cmd = [@window.filters["before"]] + [@cmd] + [@window.filters["after"]] + + # If a `root` key exist, start each split in this directory + @cmd.unshift "cd \"#{@window.root}\"" unless @window.root.nil? + + # Execute each split command + @cmd.flatten.compact.each do |command| + commands << "tmux send-keys -t #{@index} \"#{command}\"" + commands << "tmux send-keys -t #{@index} Enter" + end + + commands + end # }}} + + end + + end +end diff --git a/lib/teamocil/layout/window.rb b/lib/teamocil/layout/window.rb new file mode 100644 index 0000000..b4a8d75 --- /dev/null +++ b/lib/teamocil/layout/window.rb @@ -0,0 +1,55 @@ +module Teamocil + class Layout + + # This class represents a window within tmux + class Window + attr_reader :filters, :root, :splits, :options, :index, :name + + # Initialize a new tmux window + # + # @param session [Session] the session where the window is initialized + # @param index [Fixnnum] the window index + # @param attrs [Hash] the window data from the layout file + def initialize(session, index, attrs={}) # {{{ + @name = attrs["name"] + @root = attrs["root"] + @options = attrs["options"] || {} + + @splits = attrs["splits"] || [] + @splits = @splits.each_with_index.map { |split, split_index| Split.new(self, split_index, split) } + + @filters = attrs["filters"] || {} + @filters["before"] ||= [] + @filters["after"] ||= [] + + @index = index + @session = session + end # }}} + + # Generate commands to send to tmux + # + # @return [Array] + def generate_commands # {{{ + commands = [] + + if @session.options.include?(:here) and @index == 0 + commands << "tmux rename-window \"#{@name}\"" + else + commands << "tmux new-window -n \"#{@name}\"" + end + + commands << @splits.map(&:generate_commands) + + @options.each_pair do |option, value| + value = "on" if value === true + value = "off" if value === false + commands << "tmux set-window-option #{option} #{value}" + end + + commands + end # }}} + + end + + end +end