Compare commits

1 Commits

Author SHA1 Message Date
3517d88a1c Add watchfile 2023-10-28 19:44:47 +02:00
3 changed files with 19 additions and 31 deletions

14
Cargo.lock generated
View File

@ -563,16 +563,6 @@ version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
[[package]]
name = "socket2"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9"
dependencies = [
"libc",
"windows-sys",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.8.0"
@ -662,12 +652,8 @@ checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
"libc",
"mio",
"pin-project-lite", "pin-project-lite",
"socket2",
"tokio-macros", "tokio-macros",
"windows-sys",
] ]
[[package]] [[package]]

View File

@ -11,4 +11,4 @@ structopt = "0.3.20"
hotwatch = "0.5.0" hotwatch = "0.5.0"
color-eyre = "0.6.2" color-eyre = "0.6.2"
eyre = "0.6.8" eyre = "0.6.8"
tokio = { version = "1.33.0", features = ["macros", "rt", "sync", "time", "io-std", "io-util", "net"] } tokio = { version = "1.33.0", features = ["macros", "rt", "sync", "time", "io-std", "io-util", "fs"] }

View File

@ -1,4 +1,5 @@
use colored::{Color, Colorize}; use colored::{Color, Colorize};
use hotwatch::Hotwatch;
use std::fs::remove_file; use std::fs::remove_file;
use std::future::pending; use std::future::pending;
use std::io::{stdout, BufWriter, Write}; use std::io::{stdout, BufWriter, Write};
@ -8,10 +9,9 @@ use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use structopt::StructOpt; use structopt::StructOpt;
use terminal_size::{terminal_size, Height, Width}; use terminal_size::{terminal_size, Height, Width};
use tokio::net::UnixListener;
use tokio::sync::Notify; use tokio::sync::Notify;
use tokio::time::sleep; use tokio::time::sleep;
use tokio::{select, spawn}; use tokio::{fs::File, select, spawn};
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt()] #[structopt()]
@ -28,8 +28,9 @@ struct Opt {
#[structopt(short, long)] #[structopt(short, long)]
timeout: Option<u64>, timeout: Option<u64>,
/// Watch for changes on a file. If the file changes, restart the timeout.
#[structopt(short, long)] #[structopt(short, long)]
socket: Option<PathBuf>, watch: Option<PathBuf>,
} }
type Rendered = Vec<Vec<(char, Color)>>; type Rendered = Vec<Vec<(char, Color)>>;
@ -109,9 +110,12 @@ async fn main() -> eyre::Result<()> {
let opt = Opt::from_args(); let opt = Opt::from_args();
color_eyre::install()?; color_eyre::install()?;
let mut socket = None; if let Some(path) = &opt.watch {
if let Some(path) = &opt.socket { let _file = File::options()
socket = Some(UnixListener::bind(path)?); .create_new(true)
.write(true)
.open(path)
.await?;
} }
let mut volume = get_volume()?; let mut volume = get_volume()?;
@ -125,7 +129,7 @@ async fn main() -> eyre::Result<()> {
if let Some(millis) = opt.timeout { if let Some(millis) = opt.timeout {
let duration = Duration::from_millis(millis); let duration = Duration::from_millis(millis);
let extend_timeout = Arc::clone(&extend_timeout); let extend_timeout = Arc::clone(&extend_timeout);
let socket_path = opt.socket.clone(); let watchfile_path = opt.watch.clone();
spawn(async move { spawn(async move {
loop { loop {
select! { select! {
@ -134,7 +138,7 @@ async fn main() -> eyre::Result<()> {
} }
} }
if let Some(path) = socket_path { if let Some(path) = watchfile_path {
let _ = remove_file(path); let _ = remove_file(path);
} }
@ -142,18 +146,16 @@ async fn main() -> eyre::Result<()> {
}); });
} }
if let Some(socket) = socket { if let Some(path) = &opt.watch {
let mut watcher = Hotwatch::new()?;
let render = Arc::new(Notify::new()); let render = Arc::new(Notify::new());
{ {
let render = Arc::clone(&render); let render = Arc::clone(&render);
spawn(async move { watcher.watch(path, move |_ev| {
loop { render.notify_waiters();
let _ = socket.accept().await; extend_timeout.notify_waiters();
extend_timeout.notify_waiters(); })?;
render.notify_waiters();
}
});
} }
loop { loop {