Compare commits

1 Commits

Author SHA1 Message Date
cf57d2c2f0 Add unixsocket 2023-11-13 18:27:21 +01:00
3 changed files with 31 additions and 19 deletions

14
Cargo.lock generated
View File

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

View File

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

View File

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