Skip to content

Commit b1151b1

Browse files
committed
impl file watch
1 parent a7ec708 commit b1151b1

File tree

2 files changed

+69
-13
lines changed

2 files changed

+69
-13
lines changed

src/bee/lua_filesystem.rs

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ impl LuaFilePath {
103103
pub fn mt_eq(&self, rhs: &LuaFilePath) -> bool {
104104
self.path == rhs.path
105105
}
106+
107+
pub fn to_path(&self) -> path::PathBuf {
108+
path::PathBuf::from(&self.path)
109+
}
106110
}
107111

108112
impl UserData for LuaFilePath {

src/bee/lua_filewatch.rs

+65-13
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,106 @@
11
use mlua::prelude::LuaResult;
22
use mlua::prelude::*;
3-
// use notify::{Event, RecursiveMode, Watcher};
3+
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
44
// 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;
68
// use std::time::Duration;
79
// use tokio::task;
810

911
struct LuaFileWatch {
12+
watcher: Option<RecommendedWatcher>,
13+
receiver: Option<Receiver<Event>>,
14+
recursive: Option<bool>,
15+
filter: Option<mlua::Function>,
1016
}
1117

1218
impl LuaFileWatch {
1319
fn new() -> LuaFileWatch {
1420
LuaFileWatch {
21+
watcher: None,
22+
receiver: None,
23+
recursive: None,
24+
filter: None,
1525
}
1626
}
1727

1828
fn set_recursive(&mut self, recursive: bool) -> LuaResult<()> {
29+
self.recursive = Some(recursive);
1930
Ok(())
2031
}
2132

22-
fn set_follow_symlinks(&mut self, follow_symlinks: bool) -> LuaResult<()> {
33+
fn set_follow_symlinks(&mut self, _: bool) -> LuaResult<()> {
2334
Ok(())
2435
}
2536

2637
fn set_filter(&mut self, filter: mlua::Function) -> LuaResult<()> {
38+
self.filter = Some(filter);
2739
Ok(())
2840
}
2941

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))
3258
}
3359

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);
3582
Ok(())
3683
}
3784
}
3885

3986
impl LuaUserData for LuaFileWatch {
4087
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));
4699
}
47-
48100
}
49101

50102
fn bee_filewatch_create(_: &Lua, _: ()) -> LuaResult<LuaFileWatch> {
51-
Ok(LuaFileWatch {})
103+
Ok(LuaFileWatch::new())
52104
}
53105

54106
pub fn bee_filewatch(lua: &Lua) -> LuaResult<LuaTable> {

0 commit comments

Comments
 (0)