-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Eldritch file.follow (#546)
* Implement file.follow * Fix follow to only do one file * Clean up follow * Update contribution docs for current practice
- Loading branch information
Showing
7 changed files
with
70 additions
and
17 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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::io::{Seek, BufReader, BufRead}; | ||
use notify::{Watcher, RecursiveMode, RecommendedWatcher, Config}; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use starlark::{eval::Evaluator, values::Value}; | ||
|
||
pub fn follow<'v>(path: String, f: Value<'v>, eval: &mut Evaluator<'v, '_>) -> Result<()> { | ||
let starlark_heap = eval.heap(); | ||
// get pos to end of file | ||
let mut file = std::fs::File::open(&path)?; | ||
let mut pos = std::fs::metadata(&path)?.len(); | ||
|
||
// set up watcher | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
let mut watcher = RecommendedWatcher::new(tx, Config::default())?; | ||
watcher.watch(path.as_ref(), RecursiveMode::NonRecursive)?; | ||
|
||
// watch | ||
for _event in rx.into_iter().flatten() { | ||
// ignore any event that didn't change the pos | ||
if file.metadata()?.len() == pos { | ||
continue; | ||
} | ||
|
||
// read from pos to end of file | ||
file.seek(std::io::SeekFrom::Start(pos))?; | ||
|
||
// update post to end of file | ||
pos = file.metadata()?.len(); | ||
|
||
let reader = BufReader::new(&file); | ||
for line in reader.lines() { | ||
let val = starlark_heap.alloc(line?.to_string()); | ||
eval.eval_function(f, &[val], &[])?; | ||
} | ||
} | ||
Ok(()) | ||
} |
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