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
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
# ItermWindow.current is the preferred method.
def initialize
@buffer = []
@tabs = {}
@tab_color_files = []
@default_tab = nil
end
# Creates a new terminal window, runs the block on it
def self.open(&block)
self.new.run(:new, &block)
def self.open(options = {}, &block)
self.new.run(:new, options, &block)
end
# Selects the first terminal window, runs the block on it
@ -91,7 +110,8 @@ class ItermWindow
self.new.run(:current, &block)
end
def run(window_type = :new, &block)
def run(window_type = :new, options = {}, &block)
@options = options
run_commands window_type, &block
send_output
end
@ -102,8 +122,13 @@ class ItermWindow
end
# Creates a new tab from 'Default Session', runs the block on it
def open_tab(name, &block)
create_tab(name, 'Default Session', &block)
def open_tab(name, options = {}, &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
# Outputs a single line of Applescript code
@ -131,14 +156,15 @@ class ItermWindow
output "set myterm to #{window_types[window_type]}"
output "tell myterm"
self.instance_eval(&block) if block_given?
@tabs[@default_tab].select if @default_tab
output "end tell"
output "end tell"
end
# Creates a new Tab object, either default or from a bookmark,
# and creates a convenience method for retrieval
def create_tab(name, bookmark=nil, &block)
@tabs[name] = Tab.new(self, name, bookmark, &block)
def create_tab(name, bookmark=nil, options = {}, &block)
@tabs[name] = Tab.new(self, name, bookmark, @options.merge(options), &block)
create_tab_convenience_method(name)
end
@ -163,7 +189,7 @@ class ItermWindow
attr_reader :name
attr_reader :bookmark
def initialize(window, name, bookmark = nil, &block)
def initialize(window, name, bookmark = nil, options = {}, &block)
@name = name
@bookmark = bookmark
@window = window
@ -171,6 +197,9 @@ class ItermWindow
output "launch session '#{@bookmark}'"
# store tty id for later access
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?
end
@ -219,6 +248,10 @@ class ItermWindow
@currently_executing_block = false
end
def method_missing(name, *args)
write("#{name} #{args.join(' ')}")
end
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))
%w{red green blue}.zip(color.scan(
@ -244,9 +277,18 @@ class ItermWindow
def create_tab_color_file(color)
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
file
end
def ensure_color(color)
case color
when Symbol
ItermWindow.colors[color]
else
color
end
end
end
end

View File

@ -20,7 +20,7 @@ describe ItermWindow do
describe ".open" do
it "should instantiate a new window and run the block" do
ItermWindow.expects(:new).returns(@window)
@window.expects(:run).with(:new)
@window.expects(:run).with(:new, {})
ItermWindow.open do
end
@ -207,6 +207,80 @@ CMD
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
before(:each) do
ItermWindow.expects(:new).returns(@window)
@ -222,6 +296,26 @@ CMD
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
describe ".create_tab_color" do