|
1 | 1 | use mlua::prelude::LuaResult;
|
2 | 2 | use mlua::prelude::*;
|
3 |
| -// use notify::{Event, RecursiveMode, Watcher}; |
| 3 | +use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher}; |
4 | 4 | // use std::path::PathBuf;
|
5 |
| -// use std::sync::mpsc::{channel, Receiver}; |
| 5 | +use std::sync::mpsc::{channel, Receiver, Sender}; |
| 6 | + |
| 7 | +use super::lua_filesystem::LuaFilePath; |
6 | 8 | // use std::time::Duration;
|
7 | 9 | // use tokio::task;
|
8 | 10 |
|
9 | 11 | struct LuaFileWatch {
|
| 12 | + watcher: Option<RecommendedWatcher>, |
| 13 | + receiver: Option<Receiver<Event>>, |
| 14 | + recursive: Option<bool>, |
| 15 | + filter: Option<mlua::Function>, |
10 | 16 | }
|
11 | 17 |
|
12 | 18 | impl LuaFileWatch {
|
13 | 19 | fn new() -> LuaFileWatch {
|
14 | 20 | LuaFileWatch {
|
| 21 | + watcher: None, |
| 22 | + receiver: None, |
| 23 | + recursive: None, |
| 24 | + filter: None, |
15 | 25 | }
|
16 | 26 | }
|
17 | 27 |
|
18 | 28 | fn set_recursive(&mut self, recursive: bool) -> LuaResult<()> {
|
| 29 | + self.recursive = Some(recursive); |
19 | 30 | Ok(())
|
20 | 31 | }
|
21 | 32 |
|
22 |
| - fn set_follow_symlinks(&mut self, follow_symlinks: bool) -> LuaResult<()> { |
| 33 | + fn set_follow_symlinks(&mut self, _: bool) -> LuaResult<()> { |
23 | 34 | Ok(())
|
24 | 35 | }
|
25 | 36 |
|
26 | 37 | fn set_filter(&mut self, filter: mlua::Function) -> LuaResult<()> {
|
| 38 | + self.filter = Some(filter); |
27 | 39 | Ok(())
|
28 | 40 | }
|
29 | 41 |
|
30 |
| - fn select(&mut self) -> LuaResult<(String, String)> { |
31 |
| - Ok(("".to_string(), "".to_string())) |
| 42 | + fn select(&mut self, lua: &Lua) -> LuaResult<(mlua::Value, mlua::Value)> { |
| 43 | + if let Some(rx) = &self.receiver { |
| 44 | + if let Ok(event) = rx.try_recv() { |
| 45 | + let path = event.paths[0].to_str().unwrap().to_string(); |
| 46 | + let kind = match event.kind { |
| 47 | + notify::EventKind::Create(_) => "create", |
| 48 | + notify::EventKind::Modify(_) => "modify", |
| 49 | + notify::EventKind::Remove(_) => "remove", |
| 50 | + _ => "unknown" |
| 51 | + }; |
| 52 | + |
| 53 | + return Ok((kind.into_lua(lua).unwrap(), path.into_lua(lua).unwrap())); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + Ok((mlua::Nil, mlua::Nil)) |
32 | 58 | }
|
33 | 59 |
|
34 |
| - fn add(&mut self, path: String) -> LuaResult<()> { |
| 60 | + fn add(&mut self, path: LuaFilePath) -> LuaResult<()> { |
| 61 | + let (tx, rx) = channel(); |
| 62 | + let mut watcher = RecommendedWatcher::new( |
| 63 | + move |res| { |
| 64 | + match res { |
| 65 | + Ok(event) => { |
| 66 | + tx.send(event).unwrap(); |
| 67 | + } |
| 68 | + _ => {} |
| 69 | + }; |
| 70 | + }, |
| 71 | + Config::default(), |
| 72 | + ) |
| 73 | + .unwrap(); |
| 74 | + let recursive = if self.recursive.unwrap_or(false) { |
| 75 | + RecursiveMode::Recursive |
| 76 | + } else { |
| 77 | + RecursiveMode::NonRecursive |
| 78 | + }; |
| 79 | + watcher.watch(&path.to_path(), recursive).unwrap(); |
| 80 | + self.watcher = Some(watcher); |
| 81 | + self.receiver = Some(rx); |
35 | 82 | Ok(())
|
36 | 83 | }
|
37 | 84 | }
|
38 | 85 |
|
39 | 86 | impl LuaUserData for LuaFileWatch {
|
40 | 87 | fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
|
41 |
| - methods.add_method_mut("set_recursive", |_, this, recursive: bool| this.set_recursive(recursive)); |
42 |
| - methods.add_method_mut("set_follow_symlinks", |_, this, follow_symlinks: bool| this.set_follow_symlinks(follow_symlinks)); |
43 |
| - methods.add_method_mut("set_filter", |_, this, filter: mlua::Function| this.set_filter(filter)); |
44 |
| - methods.add_method_mut("select", |_, this, ()| this.select()); |
45 |
| - methods.add_method_mut("add", |_, this, path: String| this.add(path)); |
| 88 | + methods.add_method_mut("set_recursive", |_, this, recursive: bool| { |
| 89 | + this.set_recursive(recursive) |
| 90 | + }); |
| 91 | + methods.add_method_mut("set_follow_symlinks", |_, this, follow_symlinks: bool| { |
| 92 | + this.set_follow_symlinks(follow_symlinks) |
| 93 | + }); |
| 94 | + methods.add_method_mut("set_filter", |_, this, filter: mlua::Function| { |
| 95 | + this.set_filter(filter) |
| 96 | + }); |
| 97 | + methods.add_method_mut("select", |lua, this, ()| this.select(lua)); |
| 98 | + methods.add_method_mut("add", |_, this, path: LuaFilePath| this.add(path)); |
46 | 99 | }
|
47 |
| - |
48 | 100 | }
|
49 | 101 |
|
50 | 102 | fn bee_filewatch_create(_: &Lua, _: ()) -> LuaResult<LuaFileWatch> {
|
51 |
| - Ok(LuaFileWatch {}) |
| 103 | + Ok(LuaFileWatch::new()) |
52 | 104 | }
|
53 | 105 |
|
54 | 106 | pub fn bee_filewatch(lua: &Lua) -> LuaResult<LuaTable> {
|
|
0 commit comments