summaryrefslogtreecommitdiff
path: root/cli/file_watcher.rs
diff options
context:
space:
mode:
authorZheyu Zhang <zheyuzhang03@gmail.com>2022-02-01 00:39:39 +0800
committerGitHub <noreply@github.com>2022-01-31 17:39:39 +0100
commit5490cfed2000a063ef0baec500ab7d539203067c (patch)
treed121da8ad8274de65f77fbced5f452ecb1d7881a /cli/file_watcher.rs
parent3e566bb457663cec57602e564f73ded817e426a8 (diff)
feat(cli): add "--no-clear-screen" flag (#13454)
This commit adds "--no-clear-screen" flag which can be used with "--watch" flag to disable clearing of terminal screen on each file change.
Diffstat (limited to 'cli/file_watcher.rs')
-rw-r--r--cli/file_watcher.rs44
1 files changed, 33 insertions, 11 deletions
diff --git a/cli/file_watcher.rs b/cli/file_watcher.rs
index 87531bb77..fd7d4e1a1 100644
--- a/cli/file_watcher.rs
+++ b/cli/file_watcher.rs
@@ -91,18 +91,19 @@ where
paths_to_watch,
result,
} => {
- // Clear screen first
- eprint!("{}", CLEAR_SCREEN);
- info!(
- "{} File change detected! Restarting!",
- colors::intense_blue("Watcher"),
- );
return (paths_to_watch, result);
}
}
}
}
+pub struct PrintConfig {
+ /// printing watcher status to terminal.
+ pub job_name: String,
+ /// determine whether to clear the terminal screen
+ pub clear_screen: bool,
+}
+
/// Creates a file watcher, which will call `resolver` with every file change.
///
/// - `resolver` is used for resolving file paths to be watched at every restarting
@@ -113,12 +114,10 @@ where
/// - `operation` is the actual operation we want to run every time the watcher detects file
/// changes. For example, in the case where we would like to bundle, then `operation` would
/// have the logic for it like bundling the code.
-///
-/// - `job_name` is just used for printing watcher status to terminal.
pub async fn watch_func<R, O, T, F1, F2>(
mut resolver: R,
mut operation: O,
- job_name: &str,
+ print_config: PrintConfig,
) -> Result<(), AnyError>
where
R: FnMut(Option<Vec<PathBuf>>) -> F1,
@@ -128,11 +127,26 @@ where
{
let (sender, mut receiver) = DebouncedReceiver::new_with_sender();
+ let PrintConfig {
+ job_name,
+ clear_screen,
+ } = print_config;
+
// Store previous data. If module resolution fails at some point, the watcher will try to
// continue watching files using these data.
let mut paths_to_watch;
let mut resolution_result;
+ let print_after_restart = || {
+ if clear_screen {
+ eprint!("{}", CLEAR_SCREEN);
+ }
+ info!(
+ "{} File change detected! Restarting!",
+ colors::intense_blue("Watcher"),
+ );
+ };
+
match resolver(None).await {
ResolutionResult::Ignore => {
// The only situation where it makes sense to ignore the initial 'change'
@@ -149,6 +163,8 @@ where
let (paths, result) = next_restart(&mut resolver, &mut receiver).await;
paths_to_watch = paths;
resolution_result = result;
+
+ print_after_restart();
}
ResolutionResult::Restart {
paths_to_watch: paths,
@@ -159,8 +175,10 @@ where
}
};
- // Clear screen first
- eprint!("{}", CLEAR_SCREEN);
+ if clear_screen {
+ eprint!("{}", CLEAR_SCREEN);
+ }
+
info!("{} {} started.", colors::intense_blue("Watcher"), job_name,);
loop {
@@ -175,6 +193,8 @@ where
paths_to_watch = paths;
}
resolution_result = result;
+
+ print_after_restart();
continue;
},
_ = fut => {},
@@ -202,6 +222,8 @@ where
}
resolution_result = result;
+ print_after_restart();
+
drop(watcher);
}
}