Skip to content

Commit

Permalink
Escape ad unescape only the first character of a message if necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Aug 24, 2013
1 parent 011df8b commit 8539fb4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
source :rubygems
source 'https://rubygems.org'
gemspec
2 changes: 1 addition & 1 deletion Ruby/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PATH
terminal-notifier (1.4.2)

GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
bacon (1.1.0)
metaclass (0.0.1)
Expand Down
9 changes: 4 additions & 5 deletions Ruby/lib/terminal-notifier.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'shellwords'

module TerminalNotifier
BIN_PATH = File.expand_path('../../vendor/terminal-notifier/terminal-notifier.app/Contents/MacOS/terminal-notifier', __FILE__)

Expand All @@ -9,11 +11,8 @@ def self.available?

def self.execute(verbose, options)
if available?
command = [BIN_PATH, *options.map { |k,v| ["-#{k}", "\"#{v.to_s}\""] }.flatten]
if RUBY_VERSION < '1.9'
require 'shellwords'
command = Shellwords.shelljoin(command)
end
command = [BIN_PATH, *options.map { |k,v| v = v.to_s; ["-#{k}", "#{Shellwords.escape(v[0,1])}#{v[1..-1]}"] }.flatten]
command = Shellwords.join(command) if RUBY_VERSION < '1.9'
result = ''
IO.popen(command) do |stdout|
output = stdout.read
Expand Down
11 changes: 4 additions & 7 deletions Ruby/spec/terminal-notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
require 'terminal-notifier'

describe "TerminalNotifier" do
it "executes the tool with the given options" do
command = [TerminalNotifier::BIN_PATH, '-message', '"ZOMG"']
if RUBY_VERSION < '1.9'
require 'shellwords'
command = Shellwords.shelljoin(command)
end
it "executes the tool with the given options and properly escapes the message" do
command = [TerminalNotifier::BIN_PATH, '-message', '\[ZOMG] "OH YEAH"']
command = Shellwords.join(command) if RUBY_VERSION < '1.9'
IO.expects(:popen).with(command).yields(StringIO.new('output'))
TerminalNotifier.execute(false, :message => 'ZOMG')
TerminalNotifier.execute(false, :message => '[ZOMG] "OH YEAH"')
end

it "returns the result output of the command" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@
</CommandLineArgument>
<CommandLineArgument
argument = "-title Kicker"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-message &apos;\\[Execute] rake spec&apos;"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-message &apos;Execute: rake spec&apos;"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-activate com.apple.Safari"
Expand Down
16 changes: 10 additions & 6 deletions Terminal Notifier/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ - (NSString *)__bundleIdentifier;
}


@interface NSUserDefaults (Subscript)
@end

@implementation NSUserDefaults (Subscript)
@implementation NSUserDefaults (SubscriptAndUnescape)
- (id)objectForKeyedSubscript:(id)key;
{
return [self objectForKey:key];
id obj = [self objectForKey:key];
if ([obj isKindOfClass:[NSString class]] && [(NSString *)obj hasPrefix:@"\\"]) {
obj = [(NSString *)obj substringFromIndex:1];
}
return obj;
}
@end

Expand Down Expand Up @@ -76,7 +77,10 @@ - (void)printHelpBanner;
" -execute COMMAND A shell command to perform when the user clicks the notification.\n" \
"\n" \
"When the user activates a notification, the results are logged to the system logs.\n" \
"Use Console.app to view these logs.\n",
"Use Console.app to view these logs.\n" \
"\n" \
"Note that in some circumstances the first character of a message has to be escaped in order to be recognized.\n" \
"An example of this is when using an open bracket, which has to be escaped like so: ‘\\[’.\n",
appName, appVersion, appName);
}

Expand Down

0 comments on commit 8539fb4

Please sign in to comment.