diff --git a/lib/teamocil/cli.rb b/lib/teamocil/cli.rb index 996d8d3..1aaa5d3 100644 --- a/lib/teamocil/cli.rb +++ b/lib/teamocil/cli.rb @@ -5,7 +5,7 @@ module Teamocil # This class handles interaction with the `tmux` utility. class CLI - attr_accessor :layout + attr_accessor :layout, :layouts # Initialize a new run of `tmux` # @@ -14,11 +14,18 @@ module Teamocil def initialize(argv, env) # {{{ bail "You must be in a tmux session to use teamocil" unless env["TMUX"] - parse_options! + parse_options! argv + layout_path = File.join("#{env["HOME"]}", ".teamocil") + + if @options.include?(:list) + @layouts = get_layouts(layout_path) + return print_layouts + end + if @options.include?(:layout) file = @options[:layout] else - file = ::File.join("#{env["HOME"]}/.teamocil", "#{argv[0]}.yml") + file = ::File.join(layout_path, "#{argv[0]}.yml") end if @options[:edit] @@ -34,7 +41,7 @@ module Teamocil end # }}} # Parse the command line options - def parse_options! # {{{ + def parse_options!(args) # {{{ @options = {} opts = ::OptionParser.new do |opts| opts.banner = "Usage: teamocil [options] @@ -49,12 +56,29 @@ module Teamocil @options[:edit] = true end - opts.on("--layout [LAYOUT]", "Use a specific layout file, instead of ~/.teamocil/.yml") do |layout| + opts.on("--layout [LAYOUT]", "Use a specific layout file, instead of `~/.teamocil/.yml`") do |layout| @options[:layout] = layout end + opts.on("--list", "List all available layouts in `~/.teamocil/`") do + @options[:list] = true + end + end - opts.parse! + opts.parse! args + end # }}} + + # Return an array of available layouts + # + # @param path [String] the path used to look for layouts + def get_layouts(path) # {{{ + Dir.glob(File.join(path, "*.yml")).map { |file| File.basename(file).gsub(/\..+$/, "") }.sort + end # }}} + + # Print each layout on a single line + def print_layouts # {{{ + STDOUT.puts @layouts.join("\n") + exit 0 end # }}} # Print an error message and exit the utility diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index bc7354c..ca11a82 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -16,6 +16,11 @@ describe Teamocil::CLI do @cli.layout.session.windows.last.name.should == "bar" end # }}} + it "lists available layouts" do # {{{ + @cli = Teamocil::CLI.new(["--list"], @fake_env) + @cli.layouts.should == ["sample", "sample-2"] + end # }}} + end end diff --git a/spec/mock/cli.rb b/spec/mock/cli.rb new file mode 100644 index 0000000..3c7b1d7 --- /dev/null +++ b/spec/mock/cli.rb @@ -0,0 +1,20 @@ +module Teamocil + module Mock + module CLI + + def self.included(base) # {{{ + base.class_eval do + + # Do not print anything + def print_layouts + # Nothing + end + + end + end # }}} + + end + end +end + +Teamocil::CLI.send :include, Teamocil::Mock::CLI diff --git a/spec/mock/layout.rb b/spec/mock/layout.rb index c9ca5ce..b5c209a 100644 --- a/spec/mock/layout.rb +++ b/spec/mock/layout.rb @@ -4,10 +4,12 @@ module Teamocil def self.included(base) # {{{ base.class_eval do + # Do not execute anything def execute_commands(commands) # Nothing end + end end # }}} diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 95d35e4..f638106 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) require 'yaml' require 'teamocil' require File.join(File.dirname(__FILE__), "./mock/layout.rb") +require File.join(File.dirname(__FILE__), "./mock/cli.rb") module Helpers