Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
cf57d2c2f0
|
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -563,6 +563,16 @@ 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"
|
||||||
@ -652,8 +662,12 @@ 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]]
|
||||||
|
|||||||
@ -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", "fs"] }
|
tokio = { version = "1.33.0", features = ["macros", "rt", "sync", "time", "io-std", "io-util", "net"] }
|
||||||
|
|||||||
34
src/main.rs
34
src/main.rs
@ -1,5 +1,4 @@
|
|||||||
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};
|
||||||
@ -9,9 +8,10 @@ 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::{fs::File, select, spawn};
|
use tokio::{select, spawn};
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
#[structopt()]
|
#[structopt()]
|
||||||
@ -28,9 +28,8 @@ 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)]
|
||||||
watch: Option<PathBuf>,
|
socket: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Rendered = Vec<Vec<(char, Color)>>;
|
type Rendered = Vec<Vec<(char, Color)>>;
|
||||||
@ -110,12 +109,9 @@ async fn main() -> eyre::Result<()> {
|
|||||||
let opt = Opt::from_args();
|
let opt = Opt::from_args();
|
||||||
color_eyre::install()?;
|
color_eyre::install()?;
|
||||||
|
|
||||||
if let Some(path) = &opt.watch {
|
let mut socket = None;
|
||||||
let _file = File::options()
|
if let Some(path) = &opt.socket {
|
||||||
.create_new(true)
|
socket = Some(UnixListener::bind(path)?);
|
||||||
.write(true)
|
|
||||||
.open(path)
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut volume = get_volume()?;
|
let mut volume = get_volume()?;
|
||||||
@ -129,7 +125,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 watchfile_path = opt.watch.clone();
|
let socket_path = opt.socket.clone();
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
select! {
|
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);
|
let _ = remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,16 +142,18 @@ async fn main() -> eyre::Result<()> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(path) = &opt.watch {
|
if let Some(socket) = socket {
|
||||||
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);
|
||||||
watcher.watch(path, move |_ev| {
|
spawn(async move {
|
||||||
render.notify_waiters();
|
loop {
|
||||||
extend_timeout.notify_waiters();
|
let _ = socket.accept().await;
|
||||||
})?;
|
extend_timeout.notify_waiters();
|
||||||
|
render.notify_waiters();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|||||||
Reference in New Issue
Block a user