Skip to content

Commit

Permalink
Merge pull request #4 from mcmire/fix_setting_tab_title
Browse files Browse the repository at this point in the history
Fix setting tab title so it actually works in iTerm 2
  • Loading branch information
achiurizo committed Dec 9, 2011
2 parents 75980cd + dc98493 commit 376e6c0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
1 change: 1 addition & 0 deletions consular-iterm.gemspec
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_dependency 'consular'
s.add_dependency 'rb-appscript'
s.add_development_dependency 'minitest'
s.add_development_dependency 'mocha'

# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
Expand Down
51 changes: 25 additions & 26 deletions lib/consular/iterm.rb
Expand Up @@ -13,7 +13,7 @@ class ITerm < Core

class << self

# Checks to see if the current system is darwin and
# Checks to see if the current system is darwin and
# if $TERM_PROGRAM is iTerm.app
#
# @api public
Expand Down Expand Up @@ -80,15 +80,14 @@ def prepend_befores(commands, befores = nil)

# Prepend a title setting command prior to the other commands.
#
# @param [String] title
# The title to set for the context of the commands.
# @param [Array<String>] commands
# The context of commands to preprend to.
# @param [String]
# The tab title.
# @param [Appscript::Reference]
# The tab instance resulting from #open_tab.
#
# @api public
def set_title(title, commands)
cmd = "PS1=\"$PS1\\[\\e]2;#{title}\\a\\]\""
title ? commands.insert(0, cmd) : commands
def set_title(title, tab)
tab.name.set(title) if title
end

# Executes the commands for each designated window.
Expand Down Expand Up @@ -117,31 +116,31 @@ def set_title(title, commands)
# @api public
def execute_window(content, options = {})
window_options = content[:options]
_contents = content[:tabs]
_first_run = true
tab_contents = content[:tabs]
first_run = true

_contents.keys.sort.each do |key|
_content = _contents[key]
_options = content[:options]
_name = options[:name]
tab_contents.keys.sort.each do |key|
tab_content = tab_contents[key]
tab_options = tab_content[:options] || {}
tab_name = tab_options[:name]

_tab =
if _first_run && !options[:default]
open_window options.merge(window_options)
else
key == 'default' ? active_tab : open_tab(_options) && active_tab
end
tab =
if first_run && !options[:default]
open_window options.merge(window_options)
else
key == 'default' ? active_tab : open_tab(tab_options) && active_tab
end
set_title tab_name, tab

_first_run = false
commands = prepend_befores _content[:commands], content[:before]
commands = set_title _name, commands
first_run = false
commands = prepend_befores tab_content[:commands], content[:before]

if content.key? :panes
commands.each { |cmd| execute_command cmd, :in => _tab }
commands.each { |cmd| execute_command cmd, :in => tab }
execute_panes content
content.delete :panes
else
commands.each { |cmd| execute_command cmd, :in => _tab }
commands.each { |cmd| execute_command cmd, :in => tab }
end
end
end
Expand Down Expand Up @@ -233,7 +232,7 @@ def horizontal_split

# to select panes; iTerm's Appscript select method does not work
# as expected, we have to select via menu instead
#
#
# @param [String] direction
# Direction to split the pane. The valid directions are:
# 'Above', 'Below', 'Left', 'Right'
Expand Down
13 changes: 10 additions & 3 deletions spec/iterm_spec.rb
Expand Up @@ -23,9 +23,16 @@
assert_equal ['ls'], @core.prepend_befores(['ls'])
end

it "should set .set_title" do
assert_equal ["PS1=\"$PS1\\[\\e]2;hey\\a\\]\"",'ls'], @core.set_title('hey', ['ls'])
assert_equal ['ls'], @core.set_title(nil, ['ls'])
it "should set the title of the tab if one given" do
(name = Object.new).expects(:set)
(tab = Object.new).stubs(:name).returns(name)
@core.set_title('the title', tab)
end

it "should not bother setting the tab title if not one given" do
(name = Object.new).expects(:set).never
(tab = Object.new).stubs(:name).returns(name)
@core.set_title(nil, tab)
end

end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -1,6 +1,7 @@
require 'rubygems'
gem 'minitest'
require 'minitest/autorun'
require 'mocha'
require File.expand_path('../../lib/consular/iterm', __FILE__)


Expand Down

0 comments on commit 376e6c0

Please sign in to comment.