diff --git a/VT100Screen.m b/VT100Screen.m index 0f1052bea7..693a06a545 100644 --- a/VT100Screen.m +++ b/VT100Screen.m @@ -2349,10 +2349,33 @@ - (void)terminalPopCurrentTitleForWindow:(BOOL)isWindow { - (BOOL)terminalPostGrowlNotification:(NSString *)message { if (postGrowlNotifications_) { - NSString *description = [NSString stringWithFormat:@"Session %@ #%d: %@", + // If there's any newlines in the message string, treat the first line as + // the alert title and the remainder as the actual message. Ignore cases + // where there's really only a single line that begins and/or ends with newlines. + // Various things between the client and here can and will expand \n to \r\n, so + // use -getLineStart:end:contentsEnd:forRange: to reliably do what the user would + // expect here and not leave unintended dangling control characters for growl. + NSString *description = message; + NSString *title = nil; + NSUInteger endIndex, contentsIndex; + [message getLineStart:NULL + end:&endIndex + contentsEnd:&contentsIndex + forRange:NSMakeRange(0, 0)]; + if (endIndex < [message length]-1 && + contentsIndex > 0) { + title = [message substringToIndex:contentsIndex]; + description = [message substringFromIndex:endIndex]; + } else { + title = NSLocalizedStringFromTableInBundle(@"Alert", + @"iTerm", + [NSBundle bundleForClass:[self class]], + @"Growl Alerts"); + } + description = [NSString stringWithFormat:@"Session %@ #%d: %@", [delegate_ screenName], [delegate_ screenNumber], - message]; + description]; BOOL sent = [[iTermGrowlDelegate sharedInstance] growlNotify:@"Alert" withDescription:description diff --git a/tests/growl.sh b/tests/growl.sh new file mode 100755 index 0000000000..325fb2b34e --- /dev/null +++ b/tests/growl.sh @@ -0,0 +1,41 @@ +#!/bin/bash +# +# This file tests the growl OSC escapes handling. Newlines embedded in the +# argument indicate the first component should be used as a custom alert +# title, unless the title or message would be empty, in which case the +# argument is used as the message text as before. Other newlines are left +# as-is in the message text. + +# Alert +# Session Shell #1: Line0 +echo -en "\033]9;Line0\007" + +# Alert +# Session Shell #1: Line0 +echo -en "\033]9;Line0\n\007" + +# Alert +# Session Shell #1: +# Line1 +echo -en "\033]9;\nLine1\007" + +# Alert +# Session Shell #1: +# Line1 +# Line2 +echo -en "\033]9;\nLine1\nLine2\007" + +# Custom Title +# Session Shell #1: Line0 +echo -en "\033]9;Custom Title\nLine0\007" + +# Custom Title +# Session Shell #1: Line0 +# Line1 +echo -en "\033]9;Custom Title\nLine0\nLine1\007" + +# Custom Title +# Session Shell #1: +# Line1 +# Line2 +echo -en "\033]9;Custom Title\n\nLine1\nLine2\007"