Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/cli/src/main/scala/scala/cli/commands/WatchUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ object WatchUtil {
s"$gray$message$reset"
}

/** Clears the terminal screen using ANSI escape codes. This prints the escape sequence to clear
* the entire screen and moves the cursor to the top-left corner.
*/
def clearScreen(): Unit = {
// \u001b[2J clears the entire screen
// \u001b[H moves the cursor to the top-left corner (home position)
System.out.print("\u001b[2J\u001b[H")
System.out.flush()
}

def printWatchMessage(): Unit =
System.err.println(waitMessage("Watching sources"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ object Compile extends ScalaCommand[CompileOptions] with BuildCommandHelpers {

val shouldBuildTestScope = options.shared.scope.test.getOrElse(false)
if (options.watch.watchMode) {
var isFirstRun = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var isFirstRun = true
val isFirstRun = new AtomicBoolean(true)

Could be considered a nitpick, but this feels more future-proof.

val watcher = Build.watch(
inputs,
buildOptions,
Expand All @@ -115,6 +116,9 @@ object Compile extends ScalaCommand[CompileOptions] with BuildCommandHelpers {
actionableDiagnostics = actionableDiagnostics,
postAction = () => WatchUtil.printWatchMessage()
) { res =>
if (options.watch.watchClearScreen && !isFirstRun)
WatchUtil.clearScreen()
isFirstRun = false
for (builds <- res.orReport(logger))
postBuild(builds, allowExit = false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
val withTestScope = options.shared.scope.test.getOrElse(false)
if options.watch.watchMode then {
var expectedModifyEpochSecondOpt = Option.empty[Long]
var isFirstRun = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var isFirstRun = true
val isFirstRun = new AtomicBoolean(true)

val watcher = Build.watch(
inputs,
initialBuildOptions,
Expand All @@ -101,6 +102,9 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
actionableDiagnostics = actionableDiagnostics,
postAction = () => WatchUtil.printWatchMessage()
) { res =>
if (options.watch.watchClearScreen && !isFirstRun)
WatchUtil.clearScreen()
isFirstRun = false
res.orReport(logger).map(_.builds).foreach {
case b if b.forall(_.success) =>
val successfulBuilds = b.collect { case s: Build.Successful => s }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
publishLocal = false,
forceSigningExternally = options.signingCli.forceSigningExternally.getOrElse(false),
parallelUpload = options.parallelUpload,
options.watch.watch,
watch = options.watch.watch,
watchClearScreen = options.watch.watchClearScreen,
isCi = options.publishParams.isCi,
() => configDb,
options.mainClass,
Expand All @@ -279,6 +280,7 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
forceSigningExternally: Boolean,
parallelUpload: Option[Boolean],
watch: Boolean,
watchClearScreen: Boolean,
isCi: Boolean,
configDb: () => ConfigDb,
mainClassOptions: MainClassOptions,
Expand All @@ -288,6 +290,7 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
val actionableDiagnostics = configDb().get(Keys.actions).getOrElse(None)

if watch then {
var isFirstRun = true
val watcher = Build.watch(
inputs = inputs,
options = initialBuildOptions,
Expand All @@ -299,8 +302,11 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
partial = None,
actionableDiagnostics = actionableDiagnostics,
postAction = () => WatchUtil.printWatchMessage()
) {
_.orReport(logger).foreach { builds =>
) { res =>
if (watchClearScreen && !isFirstRun)
WatchUtil.clearScreen()
isFirstRun = false
res.orReport(logger).foreach { builds =>
maybePublish(
builds = builds,
workingDir = workingDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ object PublishLocal extends ScalaCommand[PublishLocalOptions] {
forceSigningExternally = options.scalaSigning.forceSigningExternally.getOrElse(false),
parallelUpload = Some(true),
watch = options.watch.watch,
watchClearScreen = options.watch.watchClearScreen,
isCi = options.publishParams.isCi,
configDb = () => ConfigDb.empty, // shouldn't be used, no need of repo credentials here
mainClassOptions = options.mainClass,
Expand Down
4 changes: 4 additions & 0 deletions modules/cli/src/main/scala/scala/cli/commands/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ object Repl extends ScalaCommand[ReplOptions] with BuildCommandHelpers {
}
}
else if (options.sharedRepl.watch.watchMode) {
var isFirstRun = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var isFirstRun = true
val isFirstRun = new AtomicBoolean(true)

val watcher = Build.watch(
inputs,
initialBuildOptions,
Expand All @@ -220,6 +221,9 @@ object Repl extends ScalaCommand[ReplOptions] with BuildCommandHelpers {
actionableDiagnostics = actionableDiagnostics,
postAction = () => WatchUtil.printWatchMessage()
) { res =>
if (options.sharedRepl.watch.watchClearScreen && !isFirstRun)
WatchUtil.clearScreen()
isFirstRun = false
for (builds <- res.orReport(logger))
postBuild(builds, allowExit = false) {
successfulBuilds =>
Expand Down
4 changes: 4 additions & 0 deletions modules/cli/src/main/scala/scala/cli/commands/run/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
*/
val mainThreadOpt = AtomicReference(Option.empty[Thread])

var isFirstRun = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var isFirstRun = true
val isFirstRun = new AtomicBoolean(true)

val watcher = Build.watch(
inputs = inputs,
options = initialBuildOptions,
Expand All @@ -266,6 +267,9 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
if processOpt.get().exists(_._1.isAlive()) then WatchUtil.printWatchWhileRunningMessage()
else WatchUtil.printWatchMessage()
) { res =>
if (options.sharedRun.watch.watchClearScreen && !isFirstRun)
WatchUtil.clearScreen()
isFirstRun = false
for ((process, onExitProcess) <- processOpt.get()) {
onExitProcess.cancel(true)
ProcUtil.interruptProcess(process, logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ final case class SharedWatchOptions(
@Tag(tags.should)
@Tag(tags.inShortHelp)
@Name("revolver")
restart: Boolean = false
restart: Boolean = false,
@Group(HelpGroup.Watch.toString)
@HelpMessage("Clear the screen each time the watch mode detects changes and re-compiles or re-runs")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@HelpMessage("Clear the screen each time the watch mode detects changes and re-compiles or re-runs")
@HelpMessage("Clear the screen each time watch mode detects changes and re-compiles or re-runs")

@Tag(tags.implementation)
@Name("watch-cls")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Name("watch-cls")
@Name("watchCls")
@Name("watchClear")

watchClearScreen: Boolean = false
) { // format: on

lazy val watchMode: Boolean = watch || restart
Expand Down
4 changes: 4 additions & 0 deletions modules/cli/src/main/scala/scala/cli/commands/test/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ object Test extends ScalaCommand[TestOptions] {
}

if (options.watch.watchMode) {
var isFirstRun = true
val watcher = Build.watch(
inputs,
initialBuildOptions,
Expand All @@ -158,6 +159,9 @@ object Test extends ScalaCommand[TestOptions] {
actionableDiagnostics = actionableDiagnostics,
postAction = () => WatchUtil.printWatchMessage()
) { res =>
if (options.watch.watchClearScreen && !isFirstRun)
WatchUtil.clearScreen()
isFirstRun = false
for (builds <- res.orReport(logger))
maybeTest(builds, allowExit = false)
}
Expand Down
Loading