Skip to content

Commit 7ce0673

Browse files
committed
Offer opening iTerm2 as a tab instead of a new window (issue #712)
Reuse a terminal window that is already open, which is usually what is wanted when a repository is sent to iTerm2. - Add an "Open as tab" preference beside the terminal handler, off by default so the existing behaviour is unchanged - Write the command into a new tab of iTerm2's current window when the preference is on, falling back to a new window when iTerm2 is not running or has none open - Gray the preference out for Terminal.app, which has no Apple Event for opening a tab - Clear the opened session with `tput clear` rather than `clear`, which emits ESC[3J and makes iTerm2 break into the new tab to ask whether discarding the scrollback is allowed - Raise the checkbox to sit directly under the terminal popup, so it reads as belonging to "Open in Terminal:" rather than to the Gist options below it
1 parent 031951c commit 7ce0673

8 files changed

Lines changed: 127 additions & 8 deletions

File tree

‎Classes/Controllers/PBPrefsWindowController.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
/* Variables for the Integration View */
2424
IBOutlet NSPopUpButton *terminalHandlerPopup;
25+
IBOutlet NSButton *terminalOpenAsTabCheckbox;
2526
IBOutlet NSPathControl *gitPathController;
2627
IBOutlet NSImageView *badGitPathIcon;
2728
IBOutlet NSView *gitPathOpenAccessory;
@@ -34,6 +35,7 @@
3435
- (IBAction)resetGitPath:sender;
3536
- (IBAction)resetAllDialogWarnings:(id)sender;
3637
- (IBAction)changeTerminalHandler:(id)sender;
38+
- (IBAction)changeTerminalOpenAsTab:(id)sender;
3739
- (IBAction)changeCommitDateFormat:(id)sender;
3840

3941
@end

‎Classes/Controllers/PBPrefsWindowController.m‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ - (void)populateTerminalHandlers
111111
if (index != -1) {
112112
[terminalHandlerPopup selectItemAtIndex:index];
113113
}
114+
115+
[self updateTerminalOpenAsTab];
116+
}
117+
118+
// Only iTerm2 can be told to open a tab, so the checkbox is offered for it
119+
// alone rather than being left on and doing nothing.
120+
- (void)updateTerminalOpenAsTab
121+
{
122+
NSString *handler = [[terminalHandlerPopup selectedItem] representedObject];
123+
124+
terminalOpenAsTabCheckbox.enabled = [PBTerminalUtil handlerSupportsTabs:handler];
125+
terminalOpenAsTabCheckbox.state = [PBGitDefaults terminalOpenAsTab] ? NSControlStateValueOn : NSControlStateValueOff;
114126
}
115127

116128
- (IBAction)changeTerminalHandler:(id)sender
@@ -121,6 +133,13 @@ - (IBAction)changeTerminalHandler:(id)sender
121133
}
122134

123135
[PBGitDefaults setTerminalHandler:handler];
136+
137+
[self updateTerminalOpenAsTab];
138+
}
139+
140+
- (IBAction)changeTerminalOpenAsTab:(id)sender
141+
{
142+
[PBGitDefaults setTerminalOpenAsTab:([sender state] == NSControlStateValueOn)];
124143
}
125144

126145
#pragma mark -

‎Classes/Util/PBTerminalUtil.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,16 @@ extern NSString *const PBTerminalHandleriTerm2;
4141
*/
4242
+ (NSString *)handlerForPreference:(NSString *)bundleIdentifier;
4343

44+
/*
45+
* Whether GitX can open a new tab in a handler's existing window, rather than
46+
* always opening a new window.
47+
*/
48+
+ (BOOL)handlerSupportsTabs:(NSString *)bundleIdentifier;
49+
50+
/*
51+
* The shell line GitX writes into a freshly opened session to run a command
52+
* at a directory.
53+
*/
54+
+ (NSString *)shellLineForCommand:(NSString *)command inDirectory:(NSURL *)directory;
55+
4456
@end

‎Classes/Util/PBTerminalUtil.m‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ + (NSString *)handlerForPreference:(NSString *)bundleIdentifier
7474
return PBTerminalHandlerTerminal;
7575
}
7676

77+
+ (BOOL)handlerSupportsTabs:(NSString *)bundleIdentifier
78+
{
79+
return [bundleIdentifier isEqualToString:PBTerminalHandleriTerm2];
80+
}
81+
82+
+ (NSString *)shellLineForCommand:(NSString *)command inDirectory:(NSURL *)directory
83+
{
84+
return [NSString stringWithFormat:@"cd \"%@\"; tput clear; echo '# Opened by GitX'; %@", directory.path, command];
85+
}
86+
7787
- (void)runCommand:(NSString *)command inDirectory:(NSURL *)directory
7888
{
7989
NSString *terminalHandler = [PBTerminalUtil handlerForPreference:[PBGitDefaults terminalHandler]];
@@ -101,7 +111,7 @@ - (nullable id)eventDidFail:(const AppleEvent *)event withError:(NSError *)error
101111

102112
- (BOOL)runTerminalCommand:(NSString *)command inDirectory:(NSURL *)directory
103113
{
104-
NSString *fullCommand = [NSString stringWithFormat:@"cd \"%@\"; clear; echo '# Opened by GitX'; %@", directory.path, command];
114+
NSString *fullCommand = [PBTerminalUtil shellLineForCommand:command inDirectory:directory];
105115

106116
TerminalApplication *term = [SBApplication applicationWithBundleIdentifier:PBTerminalHandlerTerminal];
107117
if (!term)
@@ -119,15 +129,18 @@ - (BOOL)runTerminalCommand:(NSString *)command inDirectory:(NSURL *)directory
119129

120130
- (BOOL)runiTerm2Command:(NSString *)command inDirectory:(NSURL *)directory
121131
{
122-
NSString *fullCommand = [NSString stringWithFormat:@"cd \"%@\"; clear; echo '# Opened by GitX'; %@", directory.path, command];
132+
NSString *fullCommand = [PBTerminalUtil shellLineForCommand:command inDirectory:directory];
123133

124134
iTerm2Application *term = [SBApplication applicationWithBundleIdentifier:PBTerminalHandleriTerm2];
125135
if (!term)
126136
return NO;
127137
term.delegate = self;
128138

129-
iTerm2Window *win = [term createWindowWithDefaultProfileCommand:nil];
130-
[win.currentSession writeContentsOfFile:nil text:fullCommand newline:YES];
139+
iTerm2Session *session = [self iTerm2SessionForApplication:term];
140+
if (!session)
141+
return NO;
142+
143+
[session writeContentsOfFile:nil text:fullCommand newline:YES];
131144

132145
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
133146
[term activate];
@@ -136,4 +149,16 @@ - (BOOL)runiTerm2Command:(NSString *)command inDirectory:(NSURL *)directory
136149
return YES;
137150
}
138151

152+
- (iTerm2Session *)iTerm2SessionForApplication:(iTerm2Application *)term
153+
{
154+
if ([PBGitDefaults terminalOpenAsTab] && [term isRunning]) {
155+
iTerm2Window *win = term.currentWindow;
156+
if ([win exists]) {
157+
return [win createTabWithDefaultProfileCommand:nil].currentSession;
158+
}
159+
}
160+
161+
return [term createWindowWithDefaultProfileCommand:nil].currentSession;
162+
}
163+
139164
@end

‎Classes/git/PBGitDefaults.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ typedef NS_ENUM(NSInteger, PBCommitDateFormatSetting) {
4646
+ (PBPruneOnFetchSetting)pruneOnFetch;
4747
+ (NSString *)terminalHandler;
4848
+ (void)setTerminalHandler:(NSString *)bundleIdentifier;
49+
+ (BOOL)terminalOpenAsTab;
50+
+ (void)setTerminalOpenAsTab:(BOOL)openAsTab;
4951
+ (PBCommitDateFormatSetting)commitDateFormat;
5052
+ (NSString *)commitDateCustomFormat;
5153

‎Classes/git/PBGitDefaults.m‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define kUseRepositoryWatcher @"PBUseRepositoryWatcher"
3333
#define kPruneOnFetch @"PBPruneOnFetch"
3434
#define kTerminalHandler @"PBTerminalHandler"
35+
#define kTerminalOpenAsTab @"PBTerminalOpenAsTab"
3536
#define kCommitDateFormat @"PBCommitDateFormat"
3637
#define kCommitDateCustomFormat @"PBCommitDateCustomFormat"
3738
#define kDefaultCommitDateCustomFormat @"yyyy-MM-dd HH:mm"
@@ -73,6 +74,8 @@ + (void)initialize
7374
forKey:kPruneOnFetch];
7475
[defaultValues setObject:@"com.apple.Terminal"
7576
forKey:kTerminalHandler];
77+
[defaultValues setObject:[NSNumber numberWithBool:NO]
78+
forKey:kTerminalOpenAsTab];
7679
[defaultValues setObject:[NSNumber numberWithInteger:PBCommitDateFormatLong]
7780
forKey:kCommitDateFormat];
7881
[defaultValues setObject:kDefaultCommitDateCustomFormat
@@ -253,6 +256,16 @@ + (void)setTerminalHandler:(NSString *)bundleIdentifier
253256
[[NSUserDefaults standardUserDefaults] setObject:bundleIdentifier forKey:kTerminalHandler];
254257
}
255258

259+
+ (BOOL)terminalOpenAsTab
260+
{
261+
return [[NSUserDefaults standardUserDefaults] boolForKey:kTerminalOpenAsTab];
262+
}
263+
264+
+ (void)setTerminalOpenAsTab:(BOOL)openAsTab
265+
{
266+
[[NSUserDefaults standardUserDefaults] setBool:openAsTab forKey:kTerminalOpenAsTab];
267+
}
268+
256269
+ (PBCommitDateFormatSetting)commitDateFormat
257270
{
258271
return [[NSUserDefaults standardUserDefaults] integerForKey:kCommitDateFormat];

‎GitXTests/PBTerminalHandlerPreferenceTests.m‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,40 @@ - (void)testAMissingOrEmptyPreferenceFallsBackToTerminal
5555
XCTAssertEqualObjects([PBTerminalUtil handlerForPreference:@""], @"com.apple.Terminal");
5656
}
5757

58+
// Opening a tab rides on an iTerm2 Apple Event that Terminal.app has no
59+
// equivalent for, so the preference must stay shut for Terminal.
60+
- (void)testOnlyiTerm2SupportsTabs
61+
{
62+
XCTAssertTrue([PBTerminalUtil handlerSupportsTabs:@"com.googlecode.iterm2"]);
63+
XCTAssertFalse([PBTerminalUtil handlerSupportsTabs:@"com.apple.Terminal"]);
64+
}
65+
66+
- (void)testAHandlerGitXCannotDriveDoesNotSupportTabs
67+
{
68+
XCTAssertFalse([PBTerminalUtil handlerSupportsTabs:@"com.example.someterminal"]);
69+
XCTAssertFalse([PBTerminalUtil handlerSupportsTabs:nil]);
70+
}
71+
72+
// `clear` emits ESC[3J before it clears the screen, which throws away the
73+
// scrollback and makes iTerm2 interrupt the new session to ask whether that
74+
// should be allowed. `tput clear` emits only the screen clear, which is all
75+
// GitX wants in a session it has just opened.
76+
- (void)testTheOpeningLineClearsTheScreenWithoutDiscardingScrollback
77+
{
78+
NSString *line = [PBTerminalUtil shellLineForCommand:@"git status" inDirectory:[NSURL fileURLWithPath:@"/tmp/repo"]];
79+
80+
XCTAssertTrue([line containsString:@"tput clear"], @"%@", line);
81+
XCTAssertFalse([line containsString:@"; clear"], @"%@", line);
82+
}
83+
84+
- (void)testTheOpeningLineEntersTheDirectoryAndRunsTheCommand
85+
{
86+
NSString *line = [PBTerminalUtil shellLineForCommand:@"git status" inDirectory:[NSURL fileURLWithPath:@"/tmp/repo"]];
87+
88+
XCTAssertTrue([line hasPrefix:@"cd \"/tmp/repo\";"], @"%@", line);
89+
XCTAssertTrue([line hasSuffix:@"git status"], @"%@", line);
90+
}
91+
5892
- (void)testAnApplicationThatIsNotInstalledIsNotReportedAsInstalled
5993
{
6094
XCTAssertFalse([PBTerminalUtil isHandlerInstalled:@"com.example.someterminal"]);

‎Resources/en.lproj/Preferences.xib‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<outlet property="gitPathOpenAccessory" destination="62" id="65"/>
1717
<outlet property="integrationPrefsView" destination="87" id="92"/>
1818
<outlet property="terminalHandlerPopup" destination="182" id="187"/>
19+
<outlet property="terminalOpenAsTabCheckbox" destination="188" id="191"/>
1920
<outlet property="updatesPrefsView" destination="4" id="7"/>
2021
</connections>
2122
</customObject>
@@ -431,8 +432,19 @@ or drag it from the Finder.</string>
431432
<action selector="changeTerminalHandler:" target="-2" id="186"/>
432433
</connections>
433434
</popUpButton>
435+
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="188">
436+
<rect key="frame" x="112" y="137" width="120" height="18"/>
437+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
438+
<buttonCell key="cell" type="check" title="Open as tab" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="189">
439+
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
440+
<font key="font" metaFont="system"/>
441+
</buttonCell>
442+
<connections>
443+
<action selector="changeTerminalOpenAsTab:" target="-2" id="190"/>
444+
</connections>
445+
</button>
434446
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="108">
435-
<rect key="frame" x="112" y="69" width="179" height="18"/>
447+
<rect key="frame" x="112" y="51" width="179" height="18"/>
436448
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
437449
<buttonCell key="cell" type="check" title="Make Gists public" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="109">
438450
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
@@ -444,7 +456,7 @@ or drag it from the Finder.</string>
444456
</connections>
445457
</button>
446458
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="97">
447-
<rect key="frame" x="112" y="91" width="181" height="18"/>
459+
<rect key="frame" x="112" y="73" width="181" height="18"/>
448460
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
449461
<buttonCell key="cell" type="check" title="Confirm creation of Gists" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="98">
450462
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
@@ -456,7 +468,7 @@ or drag it from the Finder.</string>
456468
</connections>
457469
</button>
458470
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="88">
459-
<rect key="frame" x="91" y="35" width="121" height="18"/>
471+
<rect key="frame" x="91" y="17" width="121" height="18"/>
460472
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
461473
<buttonCell key="cell" type="check" title="Enable Gravatar" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="91">
462474
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
@@ -467,7 +479,7 @@ or drag it from the Finder.</string>
467479
</connections>
468480
</button>
469481
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="89">
470-
<rect key="frame" x="91" y="113" width="169" height="18"/>
482+
<rect key="frame" x="91" y="95" width="169" height="18"/>
471483
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
472484
<buttonCell key="cell" type="check" title="Enable ‘Gist it’ button" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="90">
473485
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>

0 commit comments

Comments
 (0)