-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,52 @@ | ||
use std::path::Path; | ||
use std::time::Duration; | ||
use notify::poll::PollWatcherConfig; | ||
use notify::{PollWatcher, RecursiveMode, Watcher}; | ||
use std::path::Path; | ||
use std::time::Duration; | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
fn not_windows_main() -> notify::Result<()> { | ||
let mut paths: Vec<_> = std::env::args().skip(1) | ||
.map(|arg| Path::new(&arg).to_path_buf()) | ||
.collect(); | ||
if paths.is_empty() { | ||
let lo_stats = Path::new("/sys/class/net/lo/statistics/tx_bytes").to_path_buf(); | ||
if !lo_stats.exists() { | ||
eprintln!("Must provide path to watch, default system path was not found (probably you're not running on Linux?)"); | ||
std::process::exit(1); | ||
let mut paths: Vec<_> = std::env::args() | ||
.skip(1) | ||
.map(|arg| Path::new(&arg).to_path_buf()) | ||
.collect(); | ||
if paths.is_empty() { | ||
let lo_stats = Path::new("/sys/class/net/lo/statistics/tx_bytes").to_path_buf(); | ||
if !lo_stats.exists() { | ||
eprintln!("Must provide path to watch, default system path was not found (probably you're not running on Linux?)"); | ||
std::process::exit(1); | ||
} | ||
println!( | ||
"Trying {:?}, use `ping localhost` to see changes!", | ||
lo_stats | ||
); | ||
paths.push(lo_stats); | ||
} | ||
println!("Trying {:?}, use `ping localhost` to see changes!", lo_stats); | ||
paths.push(lo_stats); | ||
} | ||
|
||
println!("watching {:?}...", paths); | ||
println!("watching {:?}...", paths); | ||
|
||
let config = PollWatcherConfig { | ||
compare_contents: true, | ||
poll_interval: Duration::from_secs(2), | ||
}; | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
let mut watcher = PollWatcher::with_config(tx, config)?; | ||
for path in paths { | ||
watcher.watch(&path, RecursiveMode::Recursive)?; | ||
} | ||
let config = PollWatcherConfig { | ||
compare_contents: true, | ||
poll_interval: Duration::from_secs(2), | ||
}; | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
let mut watcher = PollWatcher::with_config(tx, config)?; | ||
for path in paths { | ||
watcher.watch(&path, RecursiveMode::Recursive)?; | ||
} | ||
|
||
for res in rx { | ||
match res { | ||
Ok(event) => println!("changed: {:?}", event), | ||
Err(e) => println!("watch error: {:?}", e), | ||
for res in rx { | ||
match res { | ||
Ok(event) => println!("changed: {:?}", event), | ||
Err(e) => println!("watch error: {:?}", e), | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
Ok(()) | ||
} | ||
|
||
fn main() -> notify::Result<()> { | ||
#[cfg(not(target_os = "windows"))] | ||
{ | ||
not_windows_main() | ||
} | ||
} | ||
#[cfg(not(target_os = "windows"))] | ||
{ | ||
not_windows_main() | ||
} | ||
} |