Add --list option to CLI class

This commit is contained in:
Rémi Prévost 2012-01-21 14:08:01 -05:00
parent 490df3bd99
commit 124bdb1961
5 changed files with 58 additions and 6 deletions

View File

@ -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] <layout>
@ -49,12 +56,29 @@ module Teamocil
@options[:edit] = true
end
opts.on("--layout [LAYOUT]", "Use a specific layout file, instead of ~/.teamocil/<layout>.yml") do |layout|
opts.on("--layout [LAYOUT]", "Use a specific layout file, instead of `~/.teamocil/<layout>.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

View File

@ -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

20
spec/mock/cli.rb Normal file
View File

@ -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

View File

@ -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 # }}}

View File

@ -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