From 0b1f909b084f83195699e33cb0b6ce8bfa355456 Mon Sep 17 00:00:00 2001 From: Johan Smits Date: Fri, 11 Oct 2024 10:49:02 +0200 Subject: [PATCH] Add clear screen option when watching for changes before starting the build clear the screen if enabled. --- src/cmd/serve.rs | 1 + src/cmd/watch.rs | 5 +++++ src/config/models/test.rs | 2 ++ src/config/rt/watch.rs | 6 ++++++ src/watch.rs | 13 +++++++++++++ 5 files changed, 27 insertions(+) diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 0bb7fe44..005b57aa 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -194,6 +194,7 @@ impl Serve { }, poll: self.watch.poll.then_some(self.watch.poll_interval.0), enable_cooldown: self.watch.enable_cooldown, + clear_screen: self.watch.clear_screen, no_error_reporting: cfg.serve.no_error_reporting, }, // This will be the effective value for `serve.open` during runtime. diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index b58cea64..9efbcc9c 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -32,6 +32,9 @@ pub struct Watch { /// Allow enabling a cooldown, discarding all change events during the build #[arg(long, env = "TRUNK_WATCH_ENABLE_COOLDOWN")] pub enable_cooldown: bool, + /// Clear the screen before each run + #[arg(short, long = "clear", env = "TRUNK_WATCH_CLEAR")] + pub clear_screen: bool, // NOTE: flattened structures come last #[command(flatten)] @@ -47,6 +50,7 @@ impl Watch { poll: _, poll_interval: _, enable_cooldown: _, + clear_screen: _, build, } = self; @@ -70,6 +74,7 @@ impl Watch { }, poll: self.poll.then_some(self.poll_interval.0), enable_cooldown: self.enable_cooldown, + clear_screen: self.clear_screen, // in watch mode we can't report errors no_error_reporting: false, }) diff --git a/src/config/models/test.rs b/src/config/models/test.rs index fa6403ab..4e1e1c2a 100644 --- a/src/config/models/test.rs +++ b/src/config/models/test.rs @@ -41,6 +41,7 @@ async fn err_bad_trunk_toml_watch_path() { }, poll: None, enable_cooldown: false, + clear_screen: false, no_error_reporting: false, }) .await @@ -68,6 +69,7 @@ async fn err_bad_trunk_toml_watch_ignore() { }, poll: None, enable_cooldown: false, + clear_screen: false, no_error_reporting: false, }) .await diff --git a/src/config/rt/watch.rs b/src/config/rt/watch.rs index 7ef9165a..9a9ee889 100644 --- a/src/config/rt/watch.rs +++ b/src/config/rt/watch.rs @@ -18,6 +18,8 @@ pub struct RtcWatch { pub poll: Option, /// Allow enabling a cooldown pub enable_cooldown: bool, + /// Clear the screen before each run + pub clear_screen: bool, /// No error reporting. pub no_error_reporting: bool, } @@ -37,6 +39,8 @@ pub struct WatchOptions { pub poll: Option, /// Allow enabling a cooldown pub enable_cooldown: bool, + /// Clear the screen before each run + pub clear_screen: bool, /// No error reporting. pub no_error_reporting: bool, } @@ -48,6 +52,7 @@ impl RtcWatch { build: build_opts, poll, enable_cooldown, + clear_screen, no_error_reporting, } = opts; @@ -98,6 +103,7 @@ impl RtcWatch { ignored_paths, poll, enable_cooldown, + clear_screen, no_error_reporting, }) } diff --git a/src/watch.rs b/src/watch.rs index bee3f6f0..908eb43a 100644 --- a/src/watch.rs +++ b/src/watch.rs @@ -91,6 +91,8 @@ pub struct WatchSystem { last_change: Instant, /// The cooldown for the watcher. [`None`] disables the cooldown. watcher_cooldown: Option, + /// Clear the screen before each run + clear_screen: bool, /// Don't send build errors to the frontend. no_error_reporting: bool, } @@ -136,6 +138,7 @@ impl WatchSystem { last_build_finished: Instant::now(), last_change: Instant::now(), watcher_cooldown, + clear_screen: cfg.clear_screen, no_error_reporting: cfg.no_error_reporting, }) } @@ -228,6 +231,16 @@ impl WatchSystem { } } + if self.clear_screen { + // This first message will not be seen if the clear screen worked. + tracing::trace!("Clear screen is enabled, clearing the screen"); + let term = console::Term::stdout(); + if let Err(err) = term.clear_screen() { + tracing::error!("Unable to clear the screen due to error: #{err}"); + } else { + tracing::trace!("Clear screen is enabled, cleared the screen"); + } + } self.spawn_build().await; }