iterm_window 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +69 -0
  3. data/lib/iterm_window.rb +231 -0
  4. metadata +57 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Chris Powers
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,69 @@
1
+ = iTermWindow
2
+
3
+ <em>Developed March 17, 2008 by Chris Powers</em>
4
+
5
+ The ItermWindow class models an iTerm terminal window and allows for full control via Ruby commands.
6
+ Under the hood, this class is a wrapper of iTerm's Applescript scripting API. Methods are used to
7
+ generate Applescript code which is run as an <tt>osascript</tt> command when the ItermWindow initialization
8
+ block is closed.
9
+
10
+ ItermWindow::Tab models a tab (session) in an iTerm terminal window and allows for it to be controlled by Ruby.
11
+ These tabs can be created with either the ItermWindow#open_bookmark method or the ItermWindow#open_tab
12
+ method. Each tab is given a name (symbol) by which it can be accessed later in the code using
13
+ the tab name as an ItermWindow method.
14
+
15
+ == EXAMPLE - Open a new iTerm window, cd to a project and open it in TextMate
16
+
17
+ require 'rubygems'
18
+ require 'chrisjpowers-iterm_window'
19
+
20
+ ItermWindow.open do
21
+ open_tab :my_tab do
22
+ write "cd ~/projects/my_project/trunk"
23
+ write "mate ./"
24
+ end
25
+ end
26
+
27
+ == EXAMPLE - Use the current iTerm window, cd to a project and open in TextMate, launch the server and the console and title them
28
+
29
+ ItermWindow.current do
30
+ open_tab :project_dir do
31
+ write "cd ~/projects/my_project/trunk"
32
+ write "mate ./"
33
+ set_title "MyProject Dir"
34
+ end
35
+ open_tab :server do
36
+ write "cd ~/projects/my_project/trunk"
37
+ write "script/server -p 3005"
38
+ set_title "MyProject Server"
39
+ end
40
+ open_tab :console do
41
+ write "cd ~/projects/my_project/trunk"
42
+ write "script/console"
43
+ set_title "MyProject Console"
44
+ end
45
+ end
46
+
47
+ == EXAMPLE - Same thing, but use bookmarks that were made for the server and console. Also, switch focus back to project dir.
48
+
49
+ ItermWindow.current do
50
+ open_tab :project_dir do
51
+ write "cd ~/projects/my_project/trunk"
52
+ write "mate ./"
53
+ end
54
+ open_bookmark :server, 'MyProject Server'
55
+ open_bookmark :console, 'MyProject Console'
56
+ project_dir.select
57
+
58
+ == EXAMPLE - Arbitrarily open two tabs, switch between them and run methods/blocks with Tab#select method and Tab#write directly
59
+
60
+ ItermWindow.open do
61
+ open_tab :first_tab
62
+ open_tab :second_tab
63
+ first_tab.select do
64
+ write 'cd ~/projects'
65
+ write 'ls'
66
+ end
67
+ second_tab.write "echo 'hello there!'"
68
+ first_tab.select # brings first tab back to focus
69
+ end
@@ -0,0 +1,231 @@
1
+ # Developed March 17, 2008 by Chris Powers
2
+ #
3
+ # The ItermWindow class models an iTerm terminal window and allows for full control via Ruby commands.
4
+ # Under the hood, this class is a wrapper of iTerm's Applescript scripting API. Methods are used to
5
+ # generate Applescript code which is run as an <tt>osascript</tt> command when the ItermWindow initialization
6
+ # block is closed.
7
+ #
8
+ # ItermWindow::Tab models a tab (session) in an iTerm terminal window and allows for it to be controlled by Ruby.
9
+ # These tabs can be created with either the ItermWindow#open_bookmark method or the ItermWindow#open_tab
10
+ # method. Each tab is given a name (symbol) by which it can be accessed later as a method of ItermWindow.
11
+ #
12
+ # EXAMPLE - Open a new iTerm window, cd to a project and open it in TextMate
13
+ #
14
+ # ItermWindow.open do
15
+ # open_tab :my_tab do
16
+ # write "cd ~/projects/my_project/trunk"
17
+ # write "mate ./"
18
+ # end
19
+ # end
20
+ #
21
+ # EXAMPLE - Use the current iTerm window, cd to a project and open in TextMate, launch the server and the console and title them
22
+ #
23
+ # ItermWindow.current do
24
+ # open_tab :project_dir do
25
+ # write "cd ~/projects/my_project/trunk"
26
+ # write "mate ./"
27
+ # set_title "MyProject Dir"
28
+ # end
29
+ #
30
+ # window.open_tab :server do
31
+ # write "cd ~/projects/my_project/trunk"
32
+ # write "script/server -p 3005"
33
+ # set_title "MyProject Server"
34
+ # end
35
+ # window.open_tab :console do
36
+ # write "cd ~/projects/my_project/trunk"
37
+ # write "script/console"
38
+ # set_title "MyProject Console"
39
+ # end
40
+ # end
41
+ #
42
+ # EXAMPLE - Same thing, but use bookmarks that were made for the server and console. Also, switch focus back to project dir.
43
+ #
44
+ # ItermWindow.current do
45
+ # open_tab :project_dir do
46
+ # write "cd ~/projects/my_project/trunk"
47
+ # write "mate ./"
48
+ # end
49
+ # open_bookmark :server, 'MyProject Server'
50
+ # open_bookmark :console, 'MyProject Console'
51
+ # project_dir.select
52
+ #
53
+ # EXAMPLE - Arbitrarily open two tabs, switch between them and run methods/blocks with Tab#select method and Tab#write directly
54
+ #
55
+ # ItermWindow.open do
56
+ # open_tab :first_tab
57
+ # open_tab :second_tab
58
+ # first_tab.select do
59
+ # write 'cd ~/projects'
60
+ # write 'ls'
61
+ # end
62
+ # second_tab.write "echo 'hello there!'"
63
+ # first_tab.select # brings first tab back to focus
64
+ # end
65
+
66
+
67
+ # The ItermWindow class models an iTerm terminal window and allows for full control via Ruby commands.
68
+ class ItermWindow
69
+
70
+ # While you can directly use ItermWindow.new, using either ItermWindow.open or
71
+ # ItermWindow.current is the preferred method.
72
+ def initialize(window_type = :new, &block)
73
+ @buffer = []
74
+ @tabs = {}
75
+ run_commands window_type, &block
76
+ send_output
77
+ end
78
+
79
+ # Creates a new terminal window, runs the block on it
80
+ def self.open(&block)
81
+ new(:new, &block)
82
+ end
83
+
84
+ # Selects the first terminal window, runs the block on it
85
+ def self.current(&block)
86
+ new(:current, &block)
87
+ end
88
+
89
+ # Creates a new tab from a bookmark, runs the block on it
90
+ def open_bookmark(name, bookmark, &block)
91
+ create_tab(name, bookmark, &block)
92
+ end
93
+
94
+ # Creates a new tab from 'Default Session', runs the block on it
95
+ def open_tab(name, &block)
96
+ create_tab(name, 'Default Session', &block)
97
+ end
98
+
99
+ # Outputs a single line of Applescript code
100
+ def output(command)
101
+ @buffer << command.gsub(/'/, '"')
102
+ end
103
+
104
+
105
+ private
106
+
107
+ # Outputs @buffer to the command line as an osascript function
108
+ def send_output
109
+ buffer_str = @buffer.map {|line| "-e '#{line}'"}.join(' ')
110
+ `osascript #{buffer_str}`
111
+ # puts buffer_str
112
+ end
113
+
114
+ # Initializes the terminal window
115
+ def run_commands(window_type, &block)
116
+ window_types = {:new => '(make new terminal)', :current => 'first terminal'}
117
+ raise ArgumentError, "ItermWindow#run_commands should be passed :new or :current." unless window_types.keys.include? window_type
118
+ output "tell application 'iTerm'"
119
+ output "activate"
120
+ output "set myterm to #{window_types[window_type]}"
121
+ output "tell myterm"
122
+ self.instance_eval(&block) if block_given?
123
+ output "end tell"
124
+ output "end tell"
125
+ end
126
+
127
+ # Creates a new Tab object, either default or from a bookmark
128
+ def create_tab(name, bookmark=nil, &block)
129
+ @tabs[name] = Tab.new(self, name, bookmark, &block)
130
+ end
131
+
132
+ # Access the tabs by their names
133
+ def method_missing(method_name, *args, &block)
134
+ @tabs[method_name] || super
135
+ end
136
+
137
+
138
+
139
+ # The Tab class models a tab (session) in an iTerm terminal window and allows for it to be controlled by Ruby.
140
+ class Tab
141
+
142
+ attr_reader :name
143
+ attr_reader :bookmark
144
+
145
+ def initialize(window, name, bookmark = nil, &block)
146
+ @name = name
147
+ @bookmark = bookmark
148
+ @window = window
149
+ @currently_executing_block = false
150
+ output "launch session '#{@bookmark}'"
151
+ # store tty id for later access
152
+ output "set #{name}_tty to the tty of the last session"
153
+ execute_block &block if block_given?
154
+ end
155
+
156
+ # Brings a tab into focus, runs a block on it if passed
157
+ def select(&block)
158
+ if block_given?
159
+ execute_block &block
160
+ else
161
+ output "select session id #{name}_tty"
162
+ end
163
+ end
164
+
165
+ # Writes a command into the terminal tab
166
+ def write(command)
167
+ if @currently_executing_block
168
+ output "write text '#{command}'"
169
+ else
170
+ execute_block { write command }
171
+ end
172
+ end
173
+
174
+ # Sets the title of the tab (ie the text on the iTerm tab itself)
175
+ def set_title(str)
176
+ if @currently_executing_block
177
+ output "set name to '#{str}'"
178
+ else
179
+ execute_block { set_title = str }
180
+ end
181
+ end
182
+
183
+ # These style methods keep crashing iTerm for some reason...
184
+
185
+ # # Sets the tab's font color
186
+ # def set_font_color(str)
187
+ # if @currently_executing_block
188
+ # output "set foreground color to '#{str}'"
189
+ # else
190
+ # execute_block { set_font_color = str }
191
+ # end
192
+ # end
193
+ #
194
+ # # Sets the tab's background color
195
+ # def set_background_color(str)
196
+ # if @currently_executing_block
197
+ # output "set background color to '#{str}'"
198
+ # else
199
+ # execute_block { set_bg_color = str }
200
+ # end
201
+ # end
202
+ # alias_method :set_bg_color, :set_background_color
203
+ #
204
+ # # Sets the tab's transparency
205
+ # def set_transparency(float)
206
+ # if @currently_executing_block
207
+ # output "set transparency to '#{float}'"
208
+ # else
209
+ # execute_block { set_transparency = float }
210
+ # end
211
+ # end
212
+
213
+ # Runs a block on this tab with proper opening and closing statements
214
+ def execute_block(&block)
215
+ @currently_executing_block = true
216
+ output "tell session id #{name}_tty"
217
+ self.instance_eval(&block)
218
+ output "end tell"
219
+ @currently_executing_block = false
220
+ end
221
+
222
+ private
223
+
224
+ def output(command)
225
+ @window.output command
226
+ end
227
+
228
+
229
+ end
230
+
231
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iterm_window
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.3
5
+ platform: ruby
6
+ authors:
7
+ - Chris Powers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-20 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: chrisjpowers@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - LICENSE
27
+ - lib/iterm_window.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/chrisjpowers/iterm_window
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: The ItermWindow class models an iTerm terminal window and allows for full control via Ruby commands.
56
+ test_files: []
57
+