From 92eec743102a9a1bf32fbf01eef155c2e321864a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81mi=20Pre=CC=81vost?= Date: Mon, 26 Sep 2011 08:37:13 -0400 Subject: [PATCH] Refactor classes structure and add doc --- .gitignore | 1 + .yardopts | 13 +++++++++ bin/teamocil | 42 +-------------------------- lib/teamocil.rb | 1 + lib/teamocil/cli.rb | 65 ++++++++++++++++++++++++++++++++++++++++++ lib/teamocil/layout.rb | 12 +++++++- 6 files changed, 92 insertions(+), 42 deletions(-) create mode 100644 .yardopts create mode 100644 lib/teamocil/cli.rb diff --git a/.gitignore b/.gitignore index d32b9e3..43c68bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.gem .rvmrc .local* +.yardoc diff --git a/.yardopts b/.yardopts new file mode 100644 index 0000000..90c201a --- /dev/null +++ b/.yardopts @@ -0,0 +1,13 @@ +--title "Teamocil" + +--no-cache +--protected +--no-private + +--markup "markdown" +--markup-provider "maruku" + +--format html + +"README.mkd" +"lib/**/*.rb" diff --git a/bin/teamocil b/bin/teamocil index 21ab95c..49d7385 100755 --- a/bin/teamocil +++ b/bin/teamocil @@ -1,48 +1,8 @@ #!/usr/bin/env ruby -def bail(msg); puts msg; exit(1); end - $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) require 'yaml' require 'teamocil' -require 'optparse' -require 'fileutils' -bail "You must be in a tmux session to use teamocil" unless ENV["TMUX"] - -options = {} -opts = OptionParser.new do |opts| - opts.banner = "Usage: teamocil [options] - -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/.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 +Teamocil::CLI.new(ARGV, ENV) diff --git a/lib/teamocil.rb b/lib/teamocil.rb index 7593a33..7ba3111 100644 --- a/lib/teamocil.rb +++ b/lib/teamocil.rb @@ -1,4 +1,5 @@ module Teamocil VERSION = '0.1.9' autoload :Layout, "teamocil/layout" + autoload :CLI, "teamocil/cli" end diff --git a/lib/teamocil/cli.rb b/lib/teamocil/cli.rb new file mode 100644 index 0000000..8791695 --- /dev/null +++ b/lib/teamocil/cli.rb @@ -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] + + 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/.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 diff --git a/lib/teamocil/layout.rb b/lib/teamocil/layout.rb index 67f1e0c..beb7019 100644 --- a/lib/teamocil/layout.rb +++ b/lib/teamocil/layout.rb @@ -1,18 +1,25 @@ module Teamocil + # This class act as a wrapper around a tmux YAML layout file. 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) # {{{ @layout = YAML.load_file(file) @options = options end # }}} + # Generate commands and sends them to tmux def to_tmux # {{{ commands = generate_commands execute_commands(commands) end # }}} + # Generate tmux commands based on the data found in the layout file def generate_commands # {{{ output = [] @@ -62,6 +69,9 @@ module Teamocil output << "tmux select-pane -t 0" end # }}} + # Execute each command in the shell + # + # @param commands [Array] an array of complete commands to send to the shell def execute_commands(commands) # {{{ `#{commands.join("; ")}` end # }}}