open_terms 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
6
+ *.sw?
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in open_terms.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # OpenTerm
2
+
3
+ This gem makes it easy to open many terminal tabs for OSX and Linux, the main idea is that working on Rails, Python, Node or other types of projects, the developer allways needs to open the same terminal windows. So why not automate this?
4
+
5
+ In a projects I worked on, there was a term.rb file, this was the first "gem" for this project. I need to write some tests, and need help testing it on linux.
6
+
7
+ This version is just a extraction from that code, this is not official yet, I'm putting it on Github only to ask some friends to help testing on Linux, since I do not have any linux box right now.
8
+
9
+ If you want to use this gem on linux, do not forget to install "xdotool" first, using your distribution package tool.
10
+
11
+ To use it on OSX you will need the "rb-appscript" gem installed.
12
+
13
+ To use on a Rails project, add these two lines on your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'open_terms'
17
+ gem 'rb-appscript'
18
+ ```
19
+
20
+ Now you can run:
21
+
22
+ ```
23
+ rake terms:open
24
+ ```
25
+
26
+ If you want to run it without Rake:
27
+
28
+ ```ruby
29
+ require 'open_terms'
30
+ require 'appscript' if RUBY_PLATFORM =~ /darwin/
31
+ OpenTerms.rails_defaults
32
+ ```
33
+
34
+ If you are not in a Rails project:
35
+
36
+ ```ruby
37
+ require 'open_terms'
38
+ require 'appscript' if RUBY_PLATFORM =~ /darwin/
39
+ project_dir = File.expand_path(File.dirname(__FILE__))
40
+ @commands = [
41
+ [:application, %Q{cd "#{project_dir}" && start_your_app_command}],
42
+ [:console, %Q{cd "#{project_dir}" && open_your_console_command}],
43
+ [:logs, %Q{cd "#{project_dir}" && tail -f log/development.log}]
44
+ ]
45
+ OpenTerms.run_commands @commands
46
+ ```
47
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ module OpenTerms
2
+ class GnomeTerminal
3
+ attr_reader :term_pid
4
+ def initialize
5
+ @term_pid = `xdotool getactivewindow`
6
+ end
7
+
8
+ def open commands
9
+ commands.each do |tab_name, command|
10
+ run "xdotool windowfocus #{term_pid}"
11
+ run "xdotool key ctrl+shift+t"
12
+
13
+ run "xdotool key ctrl+shift+alt+t"
14
+ run "xdotool type #{tab_name}"
15
+ run "xdotool key Return"
16
+
17
+ command.split(" ").each do |command_part|
18
+ run "xdotool type #{command_part}"
19
+ run "xdotool key space"
20
+ end
21
+
22
+ run "xdotool key Return"
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def run(command)
29
+ system(command) && sleep(0.05) || raise("Command #{command.inspect} failed")
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ module OpenTerms
2
+ class OsxIterm
3
+ def initialize
4
+ require 'appscript'
5
+ extend Appscript
6
+ @term = app("iTerm")
7
+ @term.activate
8
+ end
9
+
10
+ def open commands
11
+ commands.each do |title,command|
12
+ session = @term.terminals[1].make(:new => :session)
13
+ session.name.set(title.to_s)
14
+ session.exec(:command => "/bin/bash -l")
15
+ session.write(:text => command)
16
+ end
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,65 @@
1
+ module OpenTerms
2
+ class OsxTerminal
3
+
4
+ def initialize
5
+ require 'appscript'
6
+ extend Appscript
7
+ @term = app('Terminal')
8
+ tab = @term.windows.first.tabs.last
9
+ @set_titles = (@term.version.get.to_f < 2.1) && tab.properties.include?("custom_title")
10
+ end
11
+
12
+ def open commands
13
+ run commands
14
+ #set_tab_titles commands
15
+ @term.windows.first.tabs.first.selected.set true
16
+ end
17
+
18
+ protected
19
+
20
+ def run commands
21
+ commands.each do |command|
22
+ if command.class == Array
23
+ (title, command) = command
24
+ else
25
+ title = command
26
+ end
27
+ tab = make_tab title
28
+ @term.do_script(from_project_root(command), :in => tab)
29
+ end
30
+ end
31
+
32
+ def set_tab_titles titles
33
+ tabs = @term.windows.first.tabs.get
34
+ offset = tabs.length - titles.length
35
+ titles.each_with_index do |title, i|
36
+ tabs[i+offset].selected.set true
37
+ set_selected_tab_title title
38
+ end
39
+ end
40
+
41
+ def from_project_root(cmd)
42
+ "cd #{Dir.pwd} && #{cmd}"
43
+ end
44
+
45
+ def make_window
46
+ @term.activate
47
+ app("System Events").application_processes[ "Terminal.app" ].keystroke("n", :using => :command_down)
48
+ end
49
+
50
+ def make_tab(title=nil)
51
+ @term.activate app("System Events").application_processes[ "Terminal.app" ].keystroke("t", :using => :command_down)
52
+ tab = @term.windows.first.tabs.last
53
+ tab.selected.set true
54
+ set_selected_tab_title title if @set_titles && title
55
+ tab
56
+ end
57
+
58
+ def set_selected_tab_title(title)
59
+ @term.activate app("System Events").application_processes[ "Terminal.app" ].keystroke("T", :using => :command_down)
60
+ @term.activate app("System Events").application_processes[ "Terminal.app" ].keystroke(title)
61
+ @term.activate app("System Events").application_processes[ "Terminal.app" ].key_code(36)
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails'
2
+ module OpenTerms
3
+ class Railtie < Rails::Railtie
4
+ railtie_name :open_terms
5
+
6
+ rake_tasks do
7
+ load "rake/open_terms.rake"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module OpenTerms
2
+ VERSION = "0.0.1"
3
+ end
data/lib/open_terms.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require "open_terms/version"
3
+ require "open_terms/osx_iterm"
4
+ require "open_terms/osx_terminal"
5
+ require "open_terms/gnome_terminal"
6
+
7
+ module OpenTerms
8
+ require "open_terms/railtie" if defined?(Rails)
9
+
10
+ def self.run_commands(commands)
11
+ is_osx = RUBY_PLATFORM =~ /darwin/
12
+ if is_osx
13
+ begin
14
+ automator = OsxIterm.new
15
+ rescue
16
+ automator = OsxTerminal.new
17
+ end
18
+ else
19
+ automator = GnomeTerminal.new
20
+ end
21
+ automator.open commands
22
+ end
23
+
24
+ def self.rails_defaults
25
+ project_dir = Rails.root
26
+ @commands = [
27
+ [:application, %Q{cd "#{project_dir}" && script/rails server}],
28
+ [:console, %Q{cd "#{project_dir}" && script/rails console}],
29
+ [:logs, %Q{cd "#{project_dir}" && tail -f log/development.log}]
30
+ ]
31
+ self.run_commands @commands
32
+ end
33
+ end
@@ -0,0 +1,6 @@
1
+ namespace :terms do
2
+ desc "Open the default commands with terminals for this application"
3
+ task :open => :environment do
4
+ OpenTerms.rails_defaults
5
+ end
6
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "open_terms/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "open_terms"
7
+ s.version = OpenTerms::VERSION
8
+ s.authors = ["Rodrigo Urubatan"]
9
+ s.email = ["rodrigo@urubatan.com.br"]
10
+ s.homepage = "http://www.urubatan.com.br"
11
+ s.summary = %q{Easy way to open many terminal windows for OSX and Linux}
12
+ s.description = %q{Easy way to open many terminals for OSX and Linux}
13
+
14
+ s.rubyforge_project = "open_terms"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_terms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rodrigo Urubatan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-22 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Easy way to open many terminals for OSX and Linux
15
+ email:
16
+ - rodrigo@urubatan.com.br
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - lib/open_terms.rb
26
+ - lib/open_terms/gnome_terminal.rb
27
+ - lib/open_terms/osx_iterm.rb
28
+ - lib/open_terms/osx_terminal.rb
29
+ - lib/open_terms/railtie.rb
30
+ - lib/open_terms/version.rb
31
+ - lib/rake/open_terms.rake
32
+ - open_terms.gemspec
33
+ homepage: http://www.urubatan.com.br
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project: open_terms
53
+ rubygems_version: 1.8.10
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Easy way to open many terminal windows for OSX and Linux
57
+ test_files: []