Reworked blocks so that the obligatory |window| and |tab| variables are no longer needed. Blocks are now being evaluated in the context of the window or tab.

This commit is contained in:
Chris Powers 2008-08-12 17:08:02 -05:00
parent db6f61bd5e
commit 425765fcc7
2 changed files with 85 additions and 86 deletions

View File

@ -10,57 +10,57 @@ block is closed.
ItermWindow::Tab models a tab (session) in an iTerm terminal window and allows for it to be controlled by Ruby.
These tabs can be created with either the ItermWindow#open_bookmark method or the ItermWindow#open_tab
method. Each tab is given a name (symbol) by which it can be accessed later in the code using
the ItermWindow's bracket method (ie window[:tab_name]).
the tab name as an ItermWindow method.
== EXAMPLE - Open a new iTerm window, cd to a project and open it in TextMate
ItermWindow.open do |window|
window.open_tab :my_tab do |tab|
tab.write "cd ~/projects/my_project/trunk"
tab.write "mate ./"
ItermWindow.open do
open_tab :my_tab do
write "cd ~/projects/my_project/trunk"
write "mate ./"
end
end
== EXAMPLE - Use the current iTerm window, cd to a project and open in TextMate, launch the server and the console and title them
ItermWindow.current do |window|
window.open_tab :project_dir do |tab|
tab.write "cd ~/projects/my_project/trunk"
tab.write "mate ./"
tab.title = "MyProject Dir"
ItermWindow.current do
open_tab :project_dir do
write "cd ~/projects/my_project/trunk"
write "mate ./"
set_title "MyProject Dir"
end
window.open_tab :server do |tab|
tab.write "cd ~/projects/my_project/trunk"
tab.write "script/server -p 3005"
tab.title = "MyProject Server"
open_tab :server do
write "cd ~/projects/my_project/trunk"
write "script/server -p 3005"
set_title "MyProject Server"
end
window.open_tab :console do |tab|
tab.write "cd ~/projects/my_project/trunk"
tab.write "script/console"
tab.title = "MyProject Console"
open_tab :console do
write "cd ~/projects/my_project/trunk"
write "script/console"
set_title "MyProject Console"
end
end
== EXAMPLE - Same thing, but use bookmarks that were made for the server and console. Also, switch focus back to project dir.
ItermWindow.current do |window|
window.open_tab :project_dir do |tab|
tab.write "cd ~/projects/my_project/trunk"
tab.write "mate ./"
ItermWindow.current do
open_tab :project_dir do
write "cd ~/projects/my_project/trunk"
write "mate ./"
end
window.open_bookmark :server, 'MyProject Server'
window.open_bookmark :console, 'MyProject Console'
window[:project_dir].select
open_bookmark :server, 'MyProject Server'
open_bookmark :console, 'MyProject Console'
project_dir.select
== EXAMPLE - Arbitrarily open two tabs, switch between them and run methods/blocks with Tab#select method and Tab#write directly
ItermWindow.open do |window|
window.open_tab :first_tab
window.open_tab :second_tab
window[:first_tab].select do |tab|
tab.write 'cd ~/projects'
tab.write 'ls'
ItermWindow.open do
open_tab :first_tab
open_tab :second_tab
first_tab.select do
write 'cd ~/projects'
write 'ls'
end
window[:second_tab].write "echo 'hello there!'"
window[:first_tab].select # brings first tab back to focus
second_tab.write "echo 'hello there!'"
first_tab.select # brings first tab back to focus
end

View File

@ -7,60 +7,59 @@
#
# ItermWindow::Tab models a tab (session) in an iTerm terminal window and allows for it to be controlled by Ruby.
# These tabs can be created with either the ItermWindow#open_bookmark method or the ItermWindow#open_tab
# method. Each tab is given a name (symbol) by which it can be accessed later in the code using
# the ItermWindow's bracket method (ie window[:tab_name]).
# method. Each tab is given a name (symbol) by which it can be accessed later as a method of ItermWindow.
#
# EXAMPLE - Open a new iTerm window, cd to a project and open it in TextMate
#
# ItermWindow.open do |window|
# window.open_tab :my_tab do |tab|
# tab.write "cd ~/projects/my_project/trunk"
# tab.write "mate ./"
# ItermWindow.open do
# open_tab :my_tab do
# write "cd ~/projects/my_project/trunk"
# write "mate ./"
# end
# end
#
# EXAMPLE - Use the current iTerm window, cd to a project and open in TextMate, launch the server and the console and title them
#
# ItermWindow.current do |window|
# window.open_tab :project_dir do |tab|
# tab.write "cd ~/projects/my_project/trunk"
# tab.write "mate ./"
# tab.title = "MyProject Dir"
# ItermWindow.current do
# open_tab :project_dir do
# write "cd ~/projects/my_project/trunk"
# write "mate ./"
# set_title "MyProject Dir"
# end
# window.open_tab :server do |tab|
# tab.write "cd ~/projects/my_project/trunk"
# tab.write "script/server -p 3005"
# tab.title = "MyProject Server"
# window.open_tab :server do
# write "cd ~/projects/my_project/trunk"
# write "script/server -p 3005"
# set_title "MyProject Server"
# end
# window.open_tab :console do |tab|
# tab.write "cd ~/projects/my_project/trunk"
# tab.write "script/console"
# tab.title = "MyProject Console"
# window.open_tab :console do
# write "cd ~/projects/my_project/trunk"
# write "script/console"
# set_title "MyProject Console"
# end
# end
#
# EXAMPLE - Same thing, but use bookmarks that were made for the server and console. Also, switch focus back to project dir.
#
# ItermWindow.current do |window|
# window.open_tab :project_dir do |tab|
# tab.write "cd ~/projects/my_project/trunk"
# tab.write "mate ./"
# ItermWindow.current do
# open_tab :project_dir do
# write "cd ~/projects/my_project/trunk"
# write "mate ./"
# end
# window.open_bookmark :server, 'MyProject Server'
# window.open_bookmark :console, 'MyProject Console'
# window[:project_dir].select
# open_bookmark :server, 'MyProject Server'
# open_bookmark :console, 'MyProject Console'
# project_dir.select
#
# EXAMPLE - Arbitrarily open two tabs, switch between them and run methods/blocks with Tab#select method and Tab#write directly
#
# ItermWindow.open do |window|
# window.open_tab :first_tab
# window.open_tab :second_tab
# window[:first_tab].select do |tab|
# tab.write 'cd ~/projects'
# tab.write 'ls'
# ItermWindow.open do
# open_tab :first_tab
# open_tab :second_tab
# first_tab.select do
# write 'cd ~/projects'
# write 'ls'
# end
# window[:second_tab].write "echo 'hello there!'"
# window[:first_tab].select # brings first tab back to focus
# second_tab.write "echo 'hello there!'"
# first_tab.select # brings first tab back to focus
# end
@ -86,11 +85,6 @@ class ItermWindow
new(:current, &block)
end
# Directly accesses a tab by its name
def [](tab_name)
@tabs[tab_name]
end
# Creates a new tab from a bookmark, runs the block on it
def open_bookmark(name, bookmark, &block)
create_tab(name, bookmark, &block)
@ -117,14 +111,14 @@ class ItermWindow
end
# Initializes the terminal window
def run_commands(window_type)
def run_commands(window_type, &block)
window_types = {:new => '(make new terminal)', :current => 'first terminal'}
raise ArgumentError, "ItermWindow#run_commands should be passed :new or :current." unless window_types.keys.include? window_type
output "tell application 'iTerm'"
output "activate"
output "set myterm to #{window_types[window_type]}"
output "tell myterm"
yield self if block_given?
self.instance_eval(&block) if block_given?
output "end tell"
output "end tell"
end
@ -134,6 +128,11 @@ class ItermWindow
@tabs[name] = Tab.new(self, name, bookmark, &block)
end
# Access the tabs by their names
def method_missing(method_name, *args, &block)
@tabs[method_name] || super
end
# The Tab class models a tab (session) in an iTerm terminal window and allows for it to be controlled by Ruby.
@ -167,54 +166,54 @@ class ItermWindow
if @currently_executing_block
output "write text '#{command}'"
else
execute_block { |tab| tab.write command }
execute_block { write command }
end
end
# Sets the title of the tab (ie the text on the iTerm tab itself)
def title=(str)
def set_title(str)
if @currently_executing_block
output "set name to '#{str}'"
else
execute_block { |tab| tab.title = str }
execute_block { set_title = str }
end
end
# These style methods keep crashing iTerm for some reason...
# # Sets the tab's font color
# def font_color=(str)
# def set_font_color(str)
# if @currently_executing_block
# output "set foreground color to '#{str}'"
# else
# execute_block { |tab| tab.font_color = str }
# execute_block { set_font_color = str }
# end
# end
#
# # Sets the tab's background color
# def background_color=(str)
# def set_background_color(str)
# if @currently_executing_block
# output "set background color to '#{str}'"
# else
# execute_block { |tab| tab.bg_color = str }
# execute_block { set_bg_color = str }
# end
# end
# alias_method :bg_color=, :background_color=
# alias_method :set_bg_color, :set_background_color
#
# # Sets the tab's transparency
# def transparency=(float)
# def set_transparency(float)
# if @currently_executing_block
# output "set transparency to '#{float}'"
# else
# execute_block { |tab| tab.transparency = float }
# execute_block { set_transparency = float }
# end
# end
# Runs a block on this tab with proper opening and closing statements
def execute_block
def execute_block(&block)
@currently_executing_block = true
output "tell session id #{name}_tty"
yield self
self.instance_eval(&block)
output "end tell"
@currently_executing_block = false
end