Refactor classes structure and add doc

This commit is contained in:
Rémi Prévost 2011-09-26 08:37:13 -04:00
parent e7d8f58fec
commit 92eec74310
6 changed files with 92 additions and 42 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.gem *.gem
.rvmrc .rvmrc
.local* .local*
.yardoc

13
.yardopts Normal file
View File

@ -0,0 +1,13 @@
--title "Teamocil"
--no-cache
--protected
--no-private
--markup "markdown"
--markup-provider "maruku"
--format html
"README.mkd"
"lib/**/*.rb"

View File

@ -1,48 +1,8 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
def bail(msg); puts msg; exit(1); end
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'yaml' require 'yaml'
require 'teamocil' require 'teamocil'
require 'optparse'
require 'fileutils'
bail "You must be in a tmux session to use teamocil" unless ENV["TMUX"] Teamocil::CLI.new(ARGV, ENV)
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: teamocil [options] <layout>
Options:
"
opts.on("--here", "Set up the first window in the current window") do
options[:here] = true
end
opts.on("--edit", "Edit the YAML layout file instead of using it") do
options[:edit] = true
end
opts.on("--layout [LAYOUT]", "Use a specific layout file, instead of ~/.teamocil/<layout>.yml") do |layout|
options[:layout] = layout
end
end
opts.parse!
if options.include?(:layout)
file = options[:layout]
else
file = File.join("#{ENV["HOME"]}/.teamocil", "#{ARGV[0]}.yml")
end
if options[:edit]
FileUtils.touch file unless File.exists?(file)
system("$EDITOR \"#{file}\"")
else
bail "There is no file \"#{file}\"" unless File.exists?(file)
layout = Teamocil::Layout.new(file, options)
layout.to_tmux
end

View File

@ -1,4 +1,5 @@
module Teamocil module Teamocil
VERSION = '0.1.9' VERSION = '0.1.9'
autoload :Layout, "teamocil/layout" autoload :Layout, "teamocil/layout"
autoload :CLI, "teamocil/cli"
end end

65
lib/teamocil/cli.rb Normal file
View File

@ -0,0 +1,65 @@
require 'optparse'
require 'fileutils'
module Teamocil
# This class handles interaction with the `tmux` utility.
class CLI
# Initialize a new run of `tmux`
#
# @param argv [Hash] the command line parameters hash (usually `ARGV`).
# @param env [Hash] the environment variables hash (usually `ENV`).
def initialize(argv, env) # {{{
bail "You must be in a tmux session to use teamocil" unless env["TMUX"]
parse_options!
if @options.include?(:layout)
file = options[:layout]
else
file = ::File.join("#{env["HOME"]}/.teamocil", "#{argv[0]}.yml")
end
if @options[:edit]
::FileUtils.touch file unless File.exists?(file)
system("$EDITOR \"#{file}\"")
else
bail "There is no file \"#{file}\"" unless File.exists?(file)
layout = Teamocil::Layout.new(file, @options)
layout.to_tmux
end
end # }}}
# Parse the command line options
def parse_options! # {{{
@options = {}
opts = ::OptionParser.new do |opts|
opts.banner = "Usage: teamocil [options] <layout>
Options:
"
opts.on("--here", "Set up the first window in the current window") do
@options[:here] = true
end
opts.on("--edit", "Edit the YAML layout file instead of using it") do
@options[:edit] = true
end
opts.on("--layout [LAYOUT]", "Use a specific layout file, instead of ~/.teamocil/<layout>.yml") do |layout|
@options[:layout] = layout
end
end
opts.parse!
end # }}}
# Print an error message and exit the utility
#
# @param msg [Mixed] something to print before exiting.
def bail(msg) # {{{
puts "[teamocil] #{msg}"
exit 1
end # }}}
end
end

View File

@ -1,18 +1,25 @@
module Teamocil module Teamocil
# This class act as a wrapper around a tmux YAML layout file.
class Layout class Layout
attr_accessor :layout, :options attr_accessor :options
# Initialize a new layout from a file
#
# @param file [String] the complete layout file path
# @param options [Hash]
def initialize(file, options) # {{{ def initialize(file, options) # {{{
@layout = YAML.load_file(file) @layout = YAML.load_file(file)
@options = options @options = options
end # }}} end # }}}
# Generate commands and sends them to tmux
def to_tmux # {{{ def to_tmux # {{{
commands = generate_commands commands = generate_commands
execute_commands(commands) execute_commands(commands)
end # }}} end # }}}
# Generate tmux commands based on the data found in the layout file
def generate_commands # {{{ def generate_commands # {{{
output = [] output = []
@ -62,6 +69,9 @@ module Teamocil
output << "tmux select-pane -t 0" output << "tmux select-pane -t 0"
end # }}} end # }}}
# Execute each command in the shell
#
# @param commands [Array] an array of complete commands to send to the shell
def execute_commands(commands) # {{{ def execute_commands(commands) # {{{
`#{commands.join("; ")}` `#{commands.join("; ")}`
end # }}} end # }}}