Add terminationHandler property and terminate method to OakCommand

This commit is contained in:
Allan Odgaard
2016-09-26 10:04:39 +02:00
parent ede6b4b026
commit bf2eb3eb83
2 changed files with 17 additions and 5 deletions

View File

@@ -12,7 +12,9 @@ PUBLIC @interface OakCommand : NSObject
@property (nonatomic, weak) NSResponder* firstResponder;
@property (nonatomic, getter = isAsyncCommand, readonly) BOOL asyncCommand;
@property (nonatomic, readonly) NSUUID* identifier;
@property (nonatomic) void(^terminationHandler)(OakCommand*, BOOL normalExit);
- (instancetype)initWithBundleCommand:(bundle_command_t const&)aCommand;
- (void)executeWithInput:(NSFileHandle*)fileHandleForReading variables:(std::map<std::string, std::string> const&)someVariables outputHandler:(void(^)(std::string const& out, output::type placement, output_format::type format, output_caret::type outputCaret, std::map<std::string, std::string> const& environment))handler;
- (void)waitUntilExit;
- (void)terminate;
@end

View File

@@ -376,7 +376,8 @@ static pid_t run_command (dispatch_group_t rootGroup, std::string const& cmd, in
}
BOOL discardHTML = NO;
if(rc != 0 && !_userDidAbort && !(200 <= rc && rc <= 207))
BOOL normalExit = rc == 0 || (200 <= rc && rc <= 207);
if(normalExit == NO && _userDidAbort == NO)
{
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:@{
NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Failure running “%@”.", to_ns(_bundleCommand.name)],
@@ -449,6 +450,9 @@ static pid_t run_command (dispatch_group_t rootGroup, std::string const& cmd, in
[self discardHTMLOutputView:_htmlOutputView];
}
if(_terminationHandler)
_terminationHandler(self, normalExit);
// Wake potential event loop
[NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 context:NULL subtype:0 data1:0 data2:0] atStart:NO];
[[NSNotificationCenter defaultCenter] postNotificationName:OakCommandDidTerminateNotification object:self];
@@ -489,10 +493,7 @@ static pid_t run_command (dispatch_group_t rootGroup, std::string const& cmd, in
{
NSInteger choice = NSRunAlertPanel([NSString stringWithFormat:@"Stop “%@”", to_ns(_bundleCommand.name)], @"Would you like to kill the current shell command?", @"Kill Command", @"Cancel", nil);
if(choice == NSAlertDefaultReturn) // "Kill Command"
{
_userDidAbort = YES;
oak::kill_process_group_in_background(_processIdentifier);
}
[self terminate];
}
else
{
@@ -505,6 +506,15 @@ static pid_t run_command (dispatch_group_t rootGroup, std::string const& cmd, in
[NSApp postEvent:event atStart:NO];
}
- (void)terminate
{
if(_processIdentifier != 0)
{
_userDidAbort = YES;
oak::kill_process_group_in_background(_processIdentifier);
}
}
// =============================
// = NSErrorRecoveryAttempting =
// =============================