From c85e37bfee2bdb346ef757718ec0bc60fd50f7a8 Mon Sep 17 00:00:00 2001 From: Logan Wright Date: Wed, 5 Apr 2017 17:08:22 +0200 Subject: [PATCH 1/4] allow kill hooks --- Sources/Console/Console/Console.swift | 4 ++++ Sources/Console/Terminal/Terminal.swift | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Sources/Console/Console/Console.swift b/Sources/Console/Console/Console.swift index f3062c9b..5261a500 100644 --- a/Sources/Console/Console/Console.swift +++ b/Sources/Console/Console/Console.swift @@ -70,6 +70,10 @@ public protocol ConsoleProtocol { - returns: The return string from the method */ func backgroundExecute(program: String, arguments: [String]) throws -> String + + /// Upon a console instance being killed for example w/ ctrl+c + /// a console should forward the message to kill listeners + func registerKillListener(_ listener: @escaping (Int32) -> Void) } extension ConsoleProtocol { diff --git a/Sources/Console/Terminal/Terminal.swift b/Sources/Console/Terminal/Terminal.swift index 56453d53..28184094 100644 --- a/Sources/Console/Terminal/Terminal.swift +++ b/Sources/Console/Terminal/Terminal.swift @@ -2,6 +2,7 @@ import libc import Foundation private var _pids: [UnsafeMutablePointer] = [] +private var _killListeners: [(Int32) -> Void] = [] public class Terminal: ConsoleProtocol { public enum Error: Swift.Error { @@ -10,14 +11,22 @@ public class Terminal: ConsoleProtocol { } public let arguments: [String] - + + /** Creates an instance of Terminal. */ public init(arguments: [String]) { self.arguments = arguments + updateKillCommands() + } + private func updateKillCommands() { func kill(sig: Int32) { + _killListeners.forEach { listener in + listener(sig) + } + for pid in _pids { _ = libc.kill(pid.pointee, sig) } @@ -156,4 +165,8 @@ public class Terminal: ConsoleProtocol { private func command(_ command: Command) { output(command.ansi, newLine: false) } + + public func registerKillListener(_ listener: @escaping (Int32) -> Void) { + _killListeners.append(listener) + } } From 3a9edcb302feaca61bec6fce891d4a2b0da9f776 Mon Sep 17 00:00:00 2001 From: Logan Wright Date: Wed, 5 Apr 2017 17:09:21 +0200 Subject: [PATCH 2/4] return to original call site --- Sources/Console/Terminal/Terminal.swift | 3 --- 1 file changed, 3 deletions(-) diff --git a/Sources/Console/Terminal/Terminal.swift b/Sources/Console/Terminal/Terminal.swift index 28184094..071e427f 100644 --- a/Sources/Console/Terminal/Terminal.swift +++ b/Sources/Console/Terminal/Terminal.swift @@ -18,10 +18,7 @@ public class Terminal: ConsoleProtocol { */ public init(arguments: [String]) { self.arguments = arguments - updateKillCommands() - } - private func updateKillCommands() { func kill(sig: Int32) { _killListeners.forEach { listener in listener(sig) From 871864d1558b92c2c07d6d598c3ce5f3ab55d056 Mon Sep 17 00:00:00 2001 From: Logan Wright Date: Wed, 5 Apr 2017 17:09:56 +0200 Subject: [PATCH 3/4] unintentional white space --- Sources/Console/Terminal/Terminal.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Console/Terminal/Terminal.swift b/Sources/Console/Terminal/Terminal.swift index 071e427f..517ceb1a 100644 --- a/Sources/Console/Terminal/Terminal.swift +++ b/Sources/Console/Terminal/Terminal.swift @@ -12,7 +12,6 @@ public class Terminal: ConsoleProtocol { public let arguments: [String] - /** Creates an instance of Terminal. */ From 379e11c5f7e635084b4e283c1277164d33bafea1 Mon Sep 17 00:00:00 2001 From: Logan Wright Date: Wed, 5 Apr 2017 17:23:15 +0200 Subject: [PATCH 4/4] fix tests --- Tests/ConsoleTests/Utilities.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Tests/ConsoleTests/Utilities.swift b/Tests/ConsoleTests/Utilities.swift index b6de2f6b..a80d60c1 100644 --- a/Tests/ConsoleTests/Utilities.swift +++ b/Tests/ConsoleTests/Utilities.swift @@ -5,6 +5,10 @@ class TestConsole: ConsoleProtocol { var inputBuffer: String = "" var outputBuffer: String = "" + var size: (width: Int, height: Int) { + return (0, 0) + } + func output(_ string: String, style: ConsoleStyle, newLine: Bool) { outputBuffer += string if newLine { @@ -26,7 +30,7 @@ class TestConsole: ConsoleProtocol { } - var size: (width: Int, height: Int) { - return (0, 0) + func registerKillListener(_ listener: @escaping (Int32) -> Void) { + } }