teamocil 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
data/bin/teamocil CHANGED
@@ -1,48 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- def bail(msg); puts msg; exit(1); end
4
-
5
3
  $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
6
4
 
7
5
  require 'yaml'
8
6
  require 'teamocil'
9
- require 'optparse'
10
- require 'fileutils'
11
-
12
- bail "You must be in a tmux session to use teamocil" unless ENV["TMUX"]
13
-
14
- options = {}
15
- opts = OptionParser.new do |opts|
16
- opts.banner = "Usage: teamocil [options] <layout>
17
-
18
- Options:
19
- "
20
- opts.on("--here", "Set up the first window in the current window") do
21
- options[:here] = true
22
- end
23
-
24
- opts.on("--edit", "Edit the YAML layout file instead of using it") do
25
- options[:edit] = true
26
- end
27
-
28
- opts.on("--layout [LAYOUT]", "Use a specific layout file, instead of ~/.teamocil/<layout>.yml") do |layout|
29
- options[:layout] = layout
30
- end
31
-
32
- end
33
- opts.parse!
34
-
35
- if options.include?(:layout)
36
- file = options[:layout]
37
- else
38
- file = File.join("#{ENV["HOME"]}/.teamocil", "#{ARGV[0]}.yml")
39
- end
40
7
 
41
- if options[:edit]
42
- FileUtils.touch file unless File.exists?(file)
43
- system("$EDITOR \"#{file}\"")
44
- else
45
- bail "There is no file \"#{file}\"" unless File.exists?(file)
46
- layout = Teamocil::Layout.new(file, options)
47
- layout.to_tmux
48
- end
8
+ Teamocil::CLI.new(ARGV, ENV)
data/lib/teamocil.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Teamocil
2
- VERSION = '0.1.9'
2
+ VERSION = '0.1.10'
3
3
  autoload :Layout, "teamocil/layout"
4
+ autoload :CLI, "teamocil/cli"
4
5
  end
@@ -0,0 +1,65 @@
1
+ require 'optparse'
2
+ require 'fileutils'
3
+
4
+ module Teamocil
5
+ # This class handles interaction with the `tmux` utility.
6
+ class CLI
7
+
8
+ # Initialize a new run of `tmux`
9
+ #
10
+ # @param argv [Hash] the command line parameters hash (usually `ARGV`).
11
+ # @param env [Hash] the environment variables hash (usually `ENV`).
12
+ def initialize(argv, env) # {{{
13
+ bail "You must be in a tmux session to use teamocil" unless env["TMUX"]
14
+
15
+ parse_options!
16
+ if @options.include?(:layout)
17
+ file = options[:layout]
18
+ else
19
+ file = ::File.join("#{env["HOME"]}/.teamocil", "#{argv[0]}.yml")
20
+ end
21
+
22
+ if @options[:edit]
23
+ ::FileUtils.touch file unless File.exists?(file)
24
+ system("$EDITOR \"#{file}\"")
25
+ else
26
+ bail "There is no file \"#{file}\"" unless File.exists?(file)
27
+ layout = Teamocil::Layout.new(file, @options)
28
+ layout.to_tmux
29
+ end
30
+ end # }}}
31
+
32
+ # Parse the command line options
33
+ def parse_options! # {{{
34
+ @options = {}
35
+ opts = ::OptionParser.new do |opts|
36
+ opts.banner = "Usage: teamocil [options] <layout>
37
+
38
+ Options:
39
+ "
40
+ opts.on("--here", "Set up the first window in the current window") do
41
+ @options[:here] = true
42
+ end
43
+
44
+ opts.on("--edit", "Edit the YAML layout file instead of using it") do
45
+ @options[:edit] = true
46
+ end
47
+
48
+ opts.on("--layout [LAYOUT]", "Use a specific layout file, instead of ~/.teamocil/<layout>.yml") do |layout|
49
+ @options[:layout] = layout
50
+ end
51
+
52
+ end
53
+ opts.parse!
54
+ end # }}}
55
+
56
+ # Print an error message and exit the utility
57
+ #
58
+ # @param msg [Mixed] something to print before exiting.
59
+ def bail(msg) # {{{
60
+ puts "[teamocil] #{msg}"
61
+ exit 1
62
+ end # }}}
63
+
64
+ end
65
+ end
@@ -1,18 +1,23 @@
1
1
  module Teamocil
2
+ # This class act as a wrapper around a tmux YAML layout file.
2
3
  class Layout
3
4
 
4
- attr_accessor :layout, :options
5
-
5
+ # Initialize a new layout from a file
6
+ #
7
+ # @param file [String] the complete layout file path
8
+ # @param options [Hash]
6
9
  def initialize(file, options) # {{{
7
10
  @layout = YAML.load_file(file)
8
11
  @options = options
9
12
  end # }}}
10
13
 
14
+ # Generate commands and sends them to tmux
11
15
  def to_tmux # {{{
12
16
  commands = generate_commands
13
17
  execute_commands(commands)
14
18
  end # }}}
15
19
 
20
+ # Generate tmux commands based on the data found in the layout file
16
21
  def generate_commands # {{{
17
22
  output = []
18
23
 
@@ -25,12 +30,21 @@ module Teamocil
25
30
 
26
31
  windows.each_with_index do |window, window_index|
27
32
 
28
- if options.include?(:here) and window_index == 0
33
+ # Create a new window unless we used the `--here` option
34
+ if @options.include?(:here) and window_index == 0
29
35
  output << "tmux rename-window \"#{window["name"]}\""
30
36
  else
31
37
  output << "tmux new-window -n \"#{window["name"]}\""
32
38
  end
33
39
 
40
+ # Make sure our filters return arrays
41
+ window["filters"] ||= {}
42
+ window["filters"]["before"] ||= []
43
+ window["filters"]["after"] ||= []
44
+ window["filters"]["before"] = [window["filters"]["before"]] unless window["filters"]["before"].is_a? Array
45
+ window["filters"]["after"] = [window["filters"]["after"]] unless window["filters"]["after"].is_a? Array
46
+
47
+ # Create splits
34
48
  window["splits"].each_with_index do |split, index|
35
49
  unless index == 0
36
50
  if split.include?("width")
@@ -47,11 +61,14 @@ module Teamocil
47
61
  # Support single command splits, but treat it as an array nevertheless
48
62
  split["cmd"] = [split["cmd"]] unless split["cmd"].is_a? Array
49
63
 
64
+ # Wrap all commands around filters
65
+ split["cmd"] = window["filters"]["before"] + split["cmd"] + window["filters"]["after"]
66
+
50
67
  # If a `root` key exist, start each split in this directory
51
68
  split["cmd"] = ["cd \"#{window["root"]}\""] + split["cmd"] if window.include?("root")
52
69
 
53
70
  # Execute each split command
54
- split["cmd"].each do |command|
71
+ split["cmd"].compact.each do |command|
55
72
  output << "tmux send-keys -t #{index} \"#{command}\""
56
73
  output << "tmux send-keys -t #{index} Enter"
57
74
  end
@@ -62,6 +79,9 @@ module Teamocil
62
79
  output << "tmux select-pane -t 0"
63
80
  end # }}}
64
81
 
82
+ # Execute each command in the shell
83
+ #
84
+ # @param commands [Array] an array of complete commands to send to the shell
65
85
  def execute_commands(commands) # {{{
66
86
  `#{commands.join("; ")}`
67
87
  end # }}}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teamocil
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 9
10
- version: 0.1.9
9
+ - 10
10
+ version: 0.1.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - "R\xC3\xA9mi Pr\xC3\xA9vost"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-25 00:00:00 Z
18
+ date: 2011-10-08 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Teamocil helps you set up window and splits layouts for tmux using YAML configuration files.
@@ -27,9 +27,9 @@ extensions: []
27
27
  extra_rdoc_files: []
28
28
 
29
29
  files:
30
+ - lib/teamocil/cli.rb
30
31
  - lib/teamocil/layout.rb
31
32
  - lib/teamocil.rb
32
- - README.mkd
33
33
  - LICENSE
34
34
  - bin/teamocil
35
35
  homepage: http://github.com/remiprev/teamocil
data/README.mkd DELETED
@@ -1,69 +0,0 @@
1
- # Teamocil
2
-
3
- Teamocil is a tool used to automatically create sessions, windows and splits in `tmux` with Ruby and YAML. Like [tmuxinator](https://github.com/aziz/tmuxinator), but with splits, not just windows.
4
-
5
- ## Usage
6
-
7
- $ gem install teamocil
8
- $ mkdir ~/.teamocil
9
- $ touch ~/.teamocil/sample.yml
10
- $ tmux
11
- $ teamocil sample
12
-
13
- ## Options
14
-
15
- * `--here` opens the session in the current window, it doesn’t create an empty first window.
16
- * `--layout` takes a custom file path to a YAML layout file.
17
- * `--edit` opens the layout file (whether or not `--layout` is used) with `$EDITOR`.
18
-
19
- ## Layout example
20
-
21
- # ~/.teamocil/sample.yml
22
-
23
- session:
24
- name: sample-session
25
- windows:
26
- - name: sample-window
27
- root: ~/Code/sample/www
28
- splits:
29
- - cmd:
30
- - ls -la
31
- - git status
32
- - cmd: rails server --port 3000
33
- width: 50
34
- - cmd: memcached -p 11211 -vv
35
- height: 25
36
- target: bottom-right
37
-
38
- Running `$ teamocil sample` will rename the session to `sample-session` and create a new window named `sample-window` with a layout like this:
39
-
40
- .------------------.------------------.
41
- | (0) | (1) |
42
- | | |
43
- | | |
44
- | | |
45
- | | |
46
- | | |
47
- | |------------------|
48
- | | (2) |
49
- | | |
50
- '------------------'------------------'
51
-
52
- ## Extras
53
-
54
- ### Zsh autocompletion
55
-
56
- To get autocompletion when typing `teamocil <Tab>` in a zsh session, add this line to your `~/.zshrc` file:
57
-
58
- compctl -g '~/.teamocil/*(:t:r)' teamocil
59
-
60
- ## Contributors
61
-
62
- Feel free to contribute and submit pull requests!
63
-
64
- * Samuel Garneau ([garno](https://github.com/garno))
65
- * Jimmy Bourassa ([jbourassa](https://github.com/jbourassa))
66
-
67
- ## License
68
-
69
- Teamocil is © 2011 Rémi Prévost and may be freely distributed under the [LITL license](http://litl.info/). See the `LICENSE` file.