Refactor classes structure and add doc
This commit is contained in:
parent
e7d8f58fec
commit
92eec74310
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
*.gem
|
||||
.rvmrc
|
||||
.local*
|
||||
.yardoc
|
||||
|
13
.yardopts
Normal file
13
.yardopts
Normal file
@ -0,0 +1,13 @@
|
||||
--title "Teamocil"
|
||||
|
||||
--no-cache
|
||||
--protected
|
||||
--no-private
|
||||
|
||||
--markup "markdown"
|
||||
--markup-provider "maruku"
|
||||
|
||||
--format html
|
||||
|
||||
"README.mkd"
|
||||
"lib/**/*.rb"
|
42
bin/teamocil
42
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] <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
|
||||
Teamocil::CLI.new(ARGV, ENV)
|
||||
|
@ -1,4 +1,5 @@
|
||||
module Teamocil
|
||||
VERSION = '0.1.9'
|
||||
autoload :Layout, "teamocil/layout"
|
||||
autoload :CLI, "teamocil/cli"
|
||||
end
|
||||
|
65
lib/teamocil/cli.rb
Normal file
65
lib/teamocil/cli.rb
Normal 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
|
@ -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 # }}}
|
||||
|
Loading…
Reference in New Issue
Block a user