more features, yeah

This commit is contained in:
John Bintz 2011-07-12 11:22:41 -04:00
parent 18d0772fb3
commit 8e5ea076d6
3 changed files with 160 additions and 10 deletions

14
README.md Normal file
View File

@ -0,0 +1,14 @@
# iTermWindow
*Make iTerm obey your command! Start up your complex development environment quickly and easily.*
The typical Rails project requires three or tour terminal windows/tabs open at once:
* The Rails app itself running using `rails s`
* Two continuous testing environments, powered by Guard or Autotest, running on your Ruby and JavaScript code
* A console ready for committing code or other maintenance tasks
* A log file or two
* Developed March 17, 2008 by Chris Powers
* Extended June 2011 and beyond by John Bintz and many others

View File

@ -73,17 +73,36 @@ require 'tempfile'
class ItermWindow class ItermWindow
attr_reader :tab_color_files attr_reader :tab_color_files
class << self
def colors=(colors)
@colors = colors
end
def colors
@colors
end
end
ItermWindow.colors = {
:rails => 'F99',
:rspec => '99F',
:js => '9F9',
:doc => 'FF9',
:log => 'DFF',
}
# While you can directly use ItermWindow.new, using either ItermWindow.open or # While you can directly use ItermWindow.new, using either ItermWindow.open or
# ItermWindow.current is the preferred method. # ItermWindow.current is the preferred method.
def initialize def initialize
@buffer = [] @buffer = []
@tabs = {} @tabs = {}
@tab_color_files = [] @tab_color_files = []
@default_tab = nil
end end
# Creates a new terminal window, runs the block on it # Creates a new terminal window, runs the block on it
def self.open(&block) def self.open(options = {}, &block)
self.new.run(:new, &block) self.new.run(:new, options, &block)
end end
# Selects the first terminal window, runs the block on it # Selects the first terminal window, runs the block on it
@ -91,7 +110,8 @@ class ItermWindow
self.new.run(:current, &block) self.new.run(:current, &block)
end end
def run(window_type = :new, &block) def run(window_type = :new, options = {}, &block)
@options = options
run_commands window_type, &block run_commands window_type, &block
send_output send_output
end end
@ -102,8 +122,13 @@ class ItermWindow
end end
# Creates a new tab from 'Default Session', runs the block on it # Creates a new tab from 'Default Session', runs the block on it
def open_tab(name, &block) def open_tab(name, options = {}, &block)
create_tab(name, 'Default Session', &block) create_tab(name, 'Default Session', options, &block)
@default_tab = name if options[:default]
end
def default_tab(name, options = {}, &block)
open_tab(name, options.merge(:default => true), &block)
end end
# Outputs a single line of Applescript code # Outputs a single line of Applescript code
@ -131,14 +156,15 @@ class ItermWindow
output "set myterm to #{window_types[window_type]}" output "set myterm to #{window_types[window_type]}"
output "tell myterm" output "tell myterm"
self.instance_eval(&block) if block_given? self.instance_eval(&block) if block_given?
@tabs[@default_tab].select if @default_tab
output "end tell" output "end tell"
output "end tell" output "end tell"
end end
# Creates a new Tab object, either default or from a bookmark, # Creates a new Tab object, either default or from a bookmark,
# and creates a convenience method for retrieval # and creates a convenience method for retrieval
def create_tab(name, bookmark=nil, &block) def create_tab(name, bookmark=nil, options = {}, &block)
@tabs[name] = Tab.new(self, name, bookmark, &block) @tabs[name] = Tab.new(self, name, bookmark, @options.merge(options), &block)
create_tab_convenience_method(name) create_tab_convenience_method(name)
end end
@ -163,7 +189,7 @@ class ItermWindow
attr_reader :name attr_reader :name
attr_reader :bookmark attr_reader :bookmark
def initialize(window, name, bookmark = nil, &block) def initialize(window, name, bookmark = nil, options = {}, &block)
@name = name @name = name
@bookmark = bookmark @bookmark = bookmark
@window = window @window = window
@ -171,6 +197,9 @@ class ItermWindow
output "launch session '#{@bookmark}'" output "launch session '#{@bookmark}'"
# store tty id for later access # store tty id for later access
output "set #{name}_tty to the tty of the last session" output "set #{name}_tty to the tty of the last session"
write "cd #{options[:dir]}" if options[:dir]
tab_color options[:color] if options[:color]
execute_block &block if block_given? execute_block &block if block_given?
end end
@ -219,6 +248,10 @@ class ItermWindow
@currently_executing_block = false @currently_executing_block = false
end end
def method_missing(name, *args)
write("#{name} #{args.join(' ')}")
end
def self.create_tab_color(color) def self.create_tab_color(color)
raise ArgumentError.new("bad hex color: #{color}") if color.downcase[%r{[^a-f0-9]}] || !([ 3, 6 ].include?(color.length)) raise ArgumentError.new("bad hex color: #{color}") if color.downcase[%r{[^a-f0-9]}] || !([ 3, 6 ].include?(color.length))
%w{red green blue}.zip(color.scan( %w{red green blue}.zip(color.scan(
@ -244,9 +277,18 @@ class ItermWindow
def create_tab_color_file(color) def create_tab_color_file(color)
file = Tempfile.open('iterm').path + '.tc' file = Tempfile.open('iterm').path + '.tc'
File.open(file, 'wb') { |f| f.puts self.class.create_tab_color(color) } File.open(file, 'wb') { |f| f.puts self.class.create_tab_color(ensure_color(color)) }
@window.tab_color_files << file @window.tab_color_files << file
file file
end end
def ensure_color(color)
case color
when Symbol
ItermWindow.colors[color]
else
color
end
end
end end
end end

View File

@ -20,7 +20,7 @@ describe ItermWindow do
describe ".open" do describe ".open" do
it "should instantiate a new window and run the block" do it "should instantiate a new window and run the block" do
ItermWindow.expects(:new).returns(@window) ItermWindow.expects(:new).returns(@window)
@window.expects(:run).with(:new) @window.expects(:run).with(:new, {})
ItermWindow.open do ItermWindow.open do
end end
@ -207,6 +207,80 @@ CMD
end end
end end
describe 'magic commands' do
before(:each) do
ItermWindow.expects(:new).returns(@window)
end
it "should cd to the directory for the tab" do
@window.expects(:shell_out)
ItermWindow.open do
open_tab :first_tab do
bundle "exec guard"
end
end
@window.concatenated_buffer.should match(/write text "bundle exec guard"/)
end
end
describe 'default tab' do
before(:each) do
ItermWindow.expects(:new).returns(@window)
end
it "should mark the tab as the default tab" do
@window.expects(:shell_out)
ItermWindow.open do
open_tab :first_tab, :default => true do
end
end
@window.concatenated_buffer.should match(/select session id first_tab_tty/)
end
it "should mark the tab as the default tab" do
@window.expects(:shell_out)
ItermWindow.open do
default_tab :first_tab do
end
end
@window.concatenated_buffer.should match(/select session id first_tab_tty/)
end
end
describe 'change directory' do
before(:each) do
ItermWindow.expects(:new).returns(@window)
end
it "should cd to the directory for the tab" do
@window.expects(:shell_out)
ItermWindow.open do
open_tab :first_tab, :dir => 'my-dir' do
end
end
@window.concatenated_buffer.should match(/cd my-dir/)
end
it "should cd to the directry for all tabs" do
@window.expects(:shell_out)
ItermWindow.open :dir => 'my-dir' do
open_tab :first_tab do
end
end
@window.concatenated_buffer.should match(/cd my-dir/)
end
end
describe 'tab color' do describe 'tab color' do
before(:each) do before(:each) do
ItermWindow.expects(:new).returns(@window) ItermWindow.expects(:new).returns(@window)
@ -222,6 +296,26 @@ CMD
end end
end end
end end
it 'should generate color in a tab definition' do
@window.expects(:shell_out)
ItermWindow::Tab.any_instance.expects(:create_tab_color_file).with("FF00AA")
ItermWindow.open do
open_tab :first_tab, :color => "FF00AA" do
end
end
end
it 'should use predetermined colors' do
@window.expects(:shell_out)
ItermWindow::Tab.any_instance.expects(:create_tab_color_file).with(:rails)
ItermWindow.open do
open_tab :first_tab, :color => :rails do
end
end
end
end end
describe ".create_tab_color" do describe ".create_tab_color" do