Sha256: d06110bf8f3ad17853866cf998b9c1d04b1bae9b2e0f5744dcf349a40b66ac95

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

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)
        parsed_layout = YAML.load_file(file)
        layout = Teamocil::Layout.new(parsed_layout, @options)
        layout.compile!
        layout.execute_commands(layout.generate_commands)
      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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
teamocil-0.2.2 lib/teamocil/cli.rb
teamocil-0.2.1 lib/teamocil/cli.rb
teamocil-0.2 lib/teamocil/cli.rb