Class: ItermWindow::Tab

Inherits:
Object
  • Object
show all
Defined in:
lib/iterm_window.rb

Overview

The Tab class models a tab (session) in an iTerm terminal window and allows for it to be controlled by Ruby.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, name, bookmark = nil, options = {}, &block) ⇒ Tab

Returns a new instance of Tab.



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/iterm_window.rb', line 196

def initialize(window, name, bookmark = nil, options = {}, &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"
  write "cd #{options[:dir]}" if options[:dir]
  tab_color options[:color] if options[:color]

  execute_block &block if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



255
256
257
# File 'lib/iterm_window.rb', line 255

def method_missing(name, *args)
  write("#{name} #{args.join(' ')}")
end

Instance Attribute Details

#bookmarkObject (readonly)

Returns the value of attribute bookmark.



194
195
196
# File 'lib/iterm_window.rb', line 194

def bookmark
  @bookmark
end

#nameObject (readonly)

Returns the value of attribute name.



193
194
195
# File 'lib/iterm_window.rb', line 193

def name
  @name
end

Class Method Details

.create_tab_color(color) ⇒ Object

Raises:

  • (ArgumentError)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/iterm_window.rb', line 259

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(
    (case color.length
    when 3
      /./
    when 6
      /../
    end)
  ).collect { |part| 
    part += part if part.length == 1
    part.hex
  }).collect do |color, brightness|
    "\033]6;1;bg;#{color};brightness;#{brightness}\a"
  end.join
end

Instance Method Details

#execute_block(&block) ⇒ Object

Runs a block on this tab with proper opening and closing statements



247
248
249
250
251
252
253
# File 'lib/iterm_window.rb', line 247

def execute_block(&block)
  @currently_executing_block = true
  output "tell session id #{name}_tty"
  self.instance_eval(&block)
  output "end tell"
  @currently_executing_block = false
end

#select(&block) ⇒ Object

Brings a tab into focus, runs a block on it if passed



211
212
213
214
215
216
217
# File 'lib/iterm_window.rb', line 211

def select(&block)
  if block_given?
    execute_block &block
  else
    output "select session id #{name}_tty"
  end
end

#set_title(str) ⇒ Object

Sets the title of the tab (ie the text on the iTerm tab itself)



229
230
231
232
233
234
235
# File 'lib/iterm_window.rb', line 229

def set_title(str)
  if @currently_executing_block
    output "set name to '#{str}'"
  else
    execute_block { set_title = str }
  end
end

#tab_color(color) ⇒ Object

Sets the title of the tab (ie the text on the iTerm tab itself)



238
239
240
241
242
243
244
# File 'lib/iterm_window.rb', line 238

def tab_color(color)
  if @currently_executing_block
    output "write text 'cat #{file = create_tab_color_file(color)} && rm #{file}'"
  else
    execute_block { tab_color(color) }
  end
end

#write(command) ⇒ Object

Writes a command into the terminal tab



220
221
222
223
224
225
226
# File 'lib/iterm_window.rb', line 220

def write(command)
  if @currently_executing_block
    output "write text '#{command}'"
  else
    execute_block { write command }
  end
end