2008-06-06 21:59:27 +00:00
|
|
|
# Developed March 17, 2008 by Chris Powers, Killswitch Collective http://killswitchcollective.com
|
|
|
|
#
|
|
|
|
# The ItermWindow class models an iTerm terminal window and allows for full control via Ruby commands.
|
|
|
|
# Under the hood, this class is a wrapper of iTerm's Applescript scripting API. Methods are used to
|
|
|
|
# generate Applescript code which is run as an <tt>osascript</tt> command when the ItermWindow initialization
|
|
|
|
# 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
|
2008-08-12 22:08:02 +00:00
|
|
|
# method. Each tab is given a name (symbol) by which it can be accessed later as a method of ItermWindow.
|
2008-06-06 21:59:27 +00:00
|
|
|
#
|
|
|
|
# EXAMPLE - Open a new iTerm window, cd to a project and open it in TextMate
|
|
|
|
#
|
2008-08-12 22:08:02 +00:00
|
|
|
# ItermWindow.open do
|
|
|
|
# open_tab :my_tab do
|
|
|
|
# write "cd ~/projects/my_project/trunk"
|
|
|
|
# write "mate ./"
|
2008-06-06 21:59:27 +00:00
|
|
|
# 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
|
|
|
|
#
|
2008-08-12 22:08:02 +00:00
|
|
|
# ItermWindow.current do
|
|
|
|
# open_tab :project_dir do
|
|
|
|
# write "cd ~/projects/my_project/trunk"
|
|
|
|
# write "mate ./"
|
|
|
|
# set_title "MyProject Dir"
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
2008-08-12 22:08:02 +00:00
|
|
|
# window.open_tab :server do
|
|
|
|
# write "cd ~/projects/my_project/trunk"
|
|
|
|
# write "script/server -p 3005"
|
|
|
|
# set_title "MyProject Server"
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
2008-08-12 22:08:02 +00:00
|
|
|
# window.open_tab :console do
|
|
|
|
# write "cd ~/projects/my_project/trunk"
|
|
|
|
# write "script/console"
|
|
|
|
# set_title "MyProject Console"
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# EXAMPLE - Same thing, but use bookmarks that were made for the server and console. Also, switch focus back to project dir.
|
|
|
|
#
|
2008-08-12 22:08:02 +00:00
|
|
|
# ItermWindow.current do
|
|
|
|
# open_tab :project_dir do
|
|
|
|
# write "cd ~/projects/my_project/trunk"
|
|
|
|
# write "mate ./"
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
2008-08-12 22:08:02 +00:00
|
|
|
# open_bookmark :server, 'MyProject Server'
|
|
|
|
# open_bookmark :console, 'MyProject Console'
|
|
|
|
# project_dir.select
|
2008-06-06 21:59:27 +00:00
|
|
|
#
|
|
|
|
# EXAMPLE - Arbitrarily open two tabs, switch between them and run methods/blocks with Tab#select method and Tab#write directly
|
|
|
|
#
|
2008-08-12 22:08:02 +00:00
|
|
|
# ItermWindow.open do
|
|
|
|
# open_tab :first_tab
|
|
|
|
# open_tab :second_tab
|
|
|
|
# first_tab.select do
|
|
|
|
# write 'cd ~/projects'
|
|
|
|
# write 'ls'
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
2008-08-12 22:08:02 +00:00
|
|
|
# second_tab.write "echo 'hello there!'"
|
|
|
|
# first_tab.select # brings first tab back to focus
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
|
|
# The ItermWindow class models an iTerm terminal window and allows for full control via Ruby commands.
|
|
|
|
class ItermWindow
|
|
|
|
|
|
|
|
# While you can directly use ItermWindow.new, using either ItermWindow.open or
|
|
|
|
# ItermWindow.current is the preferred method.
|
|
|
|
def initialize(window_type = :new, &block)
|
|
|
|
@buffer = []
|
|
|
|
@tabs = {}
|
|
|
|
run_commands window_type, &block
|
|
|
|
send_output
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a new terminal window, runs the block on it
|
|
|
|
def self.open(&block)
|
|
|
|
new(:new, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Selects the first terminal window, runs the block on it
|
|
|
|
def self.current(&block)
|
|
|
|
new(:current, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a new tab from a bookmark, runs the block on it
|
|
|
|
def open_bookmark(name, bookmark, &block)
|
|
|
|
create_tab(name, bookmark, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a new tab from 'Default Session', runs the block on it
|
|
|
|
def open_tab(name, &block)
|
|
|
|
create_tab(name, 'Default Session', &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Outputs a single line of Applescript code
|
|
|
|
def output(command)
|
|
|
|
@buffer << command.gsub(/'/, '"')
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Outputs @buffer to the command line as an osascript function
|
|
|
|
def send_output
|
|
|
|
buffer_str = @buffer.map {|line| "-e '#{line}'"}.join(' ')
|
|
|
|
`osascript #{buffer_str}`
|
|
|
|
# puts buffer_str
|
|
|
|
end
|
|
|
|
|
|
|
|
# Initializes the terminal window
|
2008-08-12 22:08:02 +00:00
|
|
|
def run_commands(window_type, &block)
|
2008-06-06 21:59:27 +00:00
|
|
|
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"
|
2008-08-12 22:08:02 +00:00
|
|
|
self.instance_eval(&block) if block_given?
|
2008-06-06 21:59:27 +00:00
|
|
|
output "end tell"
|
|
|
|
output "end tell"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a new Tab object, either default or from a bookmark
|
|
|
|
def create_tab(name, bookmark=nil, &block)
|
|
|
|
@tabs[name] = Tab.new(self, name, bookmark, &block)
|
|
|
|
end
|
|
|
|
|
2008-08-12 22:08:02 +00:00
|
|
|
# Access the tabs by their names
|
|
|
|
def method_missing(method_name, *args, &block)
|
|
|
|
@tabs[method_name] || super
|
|
|
|
end
|
|
|
|
|
2008-06-06 21:59:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# The Tab class models a tab (session) in an iTerm terminal window and allows for it to be controlled by Ruby.
|
|
|
|
class Tab
|
|
|
|
|
|
|
|
attr_reader :name
|
|
|
|
attr_reader :bookmark
|
|
|
|
|
|
|
|
def initialize(window, name, bookmark = nil, &block)
|
|
|
|
@name = name
|
|
|
|
@bookmark = bookmark
|
|
|
|
@window = window
|
|
|
|
@currently_executing_block = false
|
|
|
|
output "launch session '#{@bookmark}'"
|
|
|
|
# store tty id for later access
|
|
|
|
output "set #{name}_tty to the tty of the last session"
|
|
|
|
execute_block &block if block_given?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Brings a tab into focus, runs a block on it if passed
|
|
|
|
def select(&block)
|
|
|
|
if block_given?
|
|
|
|
execute_block &block
|
|
|
|
else
|
|
|
|
output "select session id #{name}_tty"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Writes a command into the terminal tab
|
|
|
|
def write(command)
|
|
|
|
if @currently_executing_block
|
|
|
|
output "write text '#{command}'"
|
|
|
|
else
|
2008-08-12 22:08:02 +00:00
|
|
|
execute_block { write command }
|
2008-06-06 21:59:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the title of the tab (ie the text on the iTerm tab itself)
|
2008-08-12 22:08:02 +00:00
|
|
|
def set_title(str)
|
2008-06-06 21:59:27 +00:00
|
|
|
if @currently_executing_block
|
|
|
|
output "set name to '#{str}'"
|
|
|
|
else
|
2008-08-12 22:08:02 +00:00
|
|
|
execute_block { set_title = str }
|
2008-06-06 21:59:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# These style methods keep crashing iTerm for some reason...
|
|
|
|
|
|
|
|
# # Sets the tab's font color
|
2008-08-12 22:08:02 +00:00
|
|
|
# def set_font_color(str)
|
2008-06-06 21:59:27 +00:00
|
|
|
# if @currently_executing_block
|
|
|
|
# output "set foreground color to '#{str}'"
|
|
|
|
# else
|
2008-08-12 22:08:02 +00:00
|
|
|
# execute_block { set_font_color = str }
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # Sets the tab's background color
|
2008-08-12 22:08:02 +00:00
|
|
|
# def set_background_color(str)
|
2008-06-06 21:59:27 +00:00
|
|
|
# if @currently_executing_block
|
|
|
|
# output "set background color to '#{str}'"
|
|
|
|
# else
|
2008-08-12 22:08:02 +00:00
|
|
|
# execute_block { set_bg_color = str }
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
|
|
|
# end
|
2008-08-12 22:08:02 +00:00
|
|
|
# alias_method :set_bg_color, :set_background_color
|
2008-06-06 21:59:27 +00:00
|
|
|
#
|
|
|
|
# # Sets the tab's transparency
|
2008-08-12 22:08:02 +00:00
|
|
|
# def set_transparency(float)
|
2008-06-06 21:59:27 +00:00
|
|
|
# if @currently_executing_block
|
|
|
|
# output "set transparency to '#{float}'"
|
|
|
|
# else
|
2008-08-12 22:08:02 +00:00
|
|
|
# execute_block { set_transparency = float }
|
2008-06-06 21:59:27 +00:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# Runs a block on this tab with proper opening and closing statements
|
2008-08-12 22:08:02 +00:00
|
|
|
def execute_block(&block)
|
2008-06-06 21:59:27 +00:00
|
|
|
@currently_executing_block = true
|
|
|
|
output "tell session id #{name}_tty"
|
2008-08-12 22:08:02 +00:00
|
|
|
self.instance_eval(&block)
|
2008-06-06 21:59:27 +00:00
|
|
|
output "end tell"
|
|
|
|
@currently_executing_block = false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def output(command)
|
|
|
|
@window.output command
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|