From c931b005ab405870d45847b6189cfbc36db83479 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Mon, 24 Aug 2020 11:17:59 -0700 Subject: [PATCH] Change ctrl+c behavior in ~ While in a continuous build, when the user enters ctrl+c into the sbt server console (not a thin client connection) when sbt has been launched in interactive mode, the server exits. This commit makes it so that instead we just cancel the watch. As a result, if sbt was started in batch mode, e.g. `sbt ~compile`, ctrl+c will still exit sbt but in interactive mode ctrl+c will take the user back to the shell. --- .../main/scala/sbt/internal/Continuous.scala | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main/src/main/scala/sbt/internal/Continuous.scala b/main/src/main/scala/sbt/internal/Continuous.scala index ed79d5b10..7aacae75f 100644 --- a/main/src/main/scala/sbt/internal/Continuous.scala +++ b/main/src/main/scala/sbt/internal/Continuous.scala @@ -332,7 +332,15 @@ private[sbt] object Continuous extends DeprecatedContinuous { ) = getFileEvents(configs, logger, state, commands, fileStampCache, channel.name) val executor = new WatchExecutor(channel.name) val nextEvent: Int => Watch.Action = - combineInputAndFileEvents(nextInputEvent, nextFileEvent, message, logger, logger, executor) + combineInputAndFileEvents( + nextInputEvent, + nextFileEvent, + message, + logger, + logger, + executor, + channel + ) val onExit = () => { cleanupFileMonitor() Util.ignoreResult(executor.close()) @@ -781,6 +789,7 @@ private[sbt] object Continuous extends DeprecatedContinuous { interrupted.set(false) terminal.inputStream.read match { case -1 => Watch.Ignore + case 3 => Watch.CancelWatch // ctrl+c on windows case byte => inputHandler(byte.toChar.toString) } } catch { @@ -808,6 +817,7 @@ private[sbt] object Continuous extends DeprecatedContinuous { logger: Logger, rawLogger: Logger, executor: WatchExecutor, + channel: CommandChannel ): Int => Watch.Action = count => { val events = new LinkedBlockingQueue[Either[Watch.Action, (Watch.Event, Watch.Action)]] @@ -817,6 +827,10 @@ private[sbt] object Continuous extends DeprecatedContinuous { val fileJob = executor .submit(s"get-file-events-$count", nextFileEvent(count).foreach(e => events.put(Right(e)))) + val signalRegistration = channel match { + case _: ConsoleChannel => Some(Signals.register(() => events.put(Left(Watch.CancelWatch)))) + case _ => None + } try { if (!inputJob.waitUntilStart(1.second) || !fileJob.waitUntilStart(1.second)) { inputJob.cancel() @@ -848,6 +862,7 @@ private[sbt] object Continuous extends DeprecatedContinuous { } } } finally { + signalRegistration.foreach(_.remove()) inputJob.cancel() fileJob.cancel() ()