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