Skip to content

Commit

Permalink
Add clear screen option when watching for changes before starting
Browse files Browse the repository at this point in the history
the build clear the screen if enabled.
  • Loading branch information
johan-smits authored and ctron committed Oct 11, 2024
1 parent a80057e commit 0b1f909
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -47,6 +50,7 @@ impl Watch {
poll: _,
poll_interval: _,
enable_cooldown: _,
clear_screen: _,
build,
} = self;

Expand All @@ -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,
})
Expand Down
2 changes: 2 additions & 0 deletions src/config/models/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/config/rt/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct RtcWatch {
pub poll: Option<Duration>,
/// 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,
}
Expand All @@ -37,6 +39,8 @@ pub struct WatchOptions {
pub poll: Option<Duration>,
/// 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,
}
Expand All @@ -48,6 +52,7 @@ impl RtcWatch {
build: build_opts,
poll,
enable_cooldown,
clear_screen,
no_error_reporting,
} = opts;

Expand Down Expand Up @@ -98,6 +103,7 @@ impl RtcWatch {
ignored_paths,
poll,
enable_cooldown,
clear_screen,
no_error_reporting,
})
}
Expand Down
13 changes: 13 additions & 0 deletions src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub struct WatchSystem {
last_change: Instant,
/// The cooldown for the watcher. [`None`] disables the cooldown.
watcher_cooldown: Option<Duration>,
/// Clear the screen before each run
clear_screen: bool,
/// Don't send build errors to the frontend.
no_error_reporting: bool,
}
Expand Down Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 0b1f909

Please sign in to comment.