Skip to content

Commit

Permalink
Progress updates working
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmturner committed Jan 7, 2025
1 parent 14406e8 commit 221dcdb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 58 deletions.
Binary file modified artifacts/libffi.dylib
Binary file not shown.
3 changes: 3 additions & 0 deletions crates/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub extern "C" fn build_index(progress_cb: extern "C" fn(msg: *const c_char)) {
let mut index = rfsee_tf_idf::TfIdf::default();
index.par_load_rfcs(progress_cb).unwrap();
index.finish(progress_cb);
if let Ok(cstr) = CString::new("Saving index to disk") {
progress_cb(cstr.as_ptr())
}
index.save(&path);
}

Expand Down
21 changes: 8 additions & 13 deletions crates/tf_idf/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@ impl TfIdf {
if let Ok(r) = fetch_rfc(&string) {
let mut guard = parsed_rfcs.lock().unwrap();
guard.push(r);
// let processed = guard.len();
// if processed % 100 == 0 {
// let progress = (processed as f64 / rfcs_count as f64) * 100_f64;
// if let Ok(msg) = CString::new(format!("Fetch progress: {progress:0.0}%")) {
// progress_cb(msg.as_ptr())
// }
// }
};
let mut guard = remaining.lock().unwrap();
*guard -= 1;
Expand All @@ -181,8 +174,10 @@ impl TfIdf {
while !finished {
let remaining = remaining.clone();
let guard = remaining.lock().unwrap();
// Need to log here, and not in the thread pool because we cant have different threads
// call the callback
if let Ok(msg) = CString::new(format!("{} remaining RFCs to fetch", *guard)) {
progress_cb(msg.into_raw())
progress_cb(msg.as_ptr())
}
if *guard == 0 {
finished = true
Expand All @@ -200,11 +195,11 @@ impl TfIdf {
self.add_rfc_entry(rfc);
if i % 100 == 0 {
let progress = (i as f64 / rfcs_count as f64) * 100_f64;
// if let Ok(msg) =
// CString::new(format!("Parse progress: {progress:0.0}%"))
// {
// progress_cb(msg.as_ptr())
// }
if let Ok(msg) =
CString::new(format!("Parse progress: {progress:0.0}%"))
{
progress_cb(msg.as_ptr())
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lua/rfsee/ffi.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
local ffi = require("ffi")

ffi.cdef([[
typedef void (*progress_callback_t)(double progress);
typedef void (*progress_callback_t)(const char* msg);
void build_index(progress_callback_t progress_cb);
void test_print(progress_callback_t progress_cb);
struct RfcSearchResult {
const char* url;
Expand Down
47 changes: 3 additions & 44 deletions lua/rfsee/index.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,54 +66,13 @@ function M.refresh()
local buf, win = window.create_progress_window()
window.update_progress_window(buf, "Building RFC index")

-- local function progress_cb(ptr)
-- -- local msg = ffi.string(ptr)
-- -- -- local msg = string.format("Downloading RFCs progress: %.1f%%", pct)
-- -- window.update_progress_window(buf, msg)
--
-- local ok, err = xpcall(function()
-- print("Getting string")
-- io.stdout:flush()
-- local msg = ffi.string(ptr) -- May throw if `ptr` is invalid
-- print("Got string")
-- io.stdout:flush()
-- window.update_progress_window(buf, msg)
-- end, debug.traceback)
-- print("Ok: ", ok)
-- io.stdout:flush()
-- if not ok then
-- -- Log the error, but don't let it unwind into Rust
-- print("Error in progress_cb:", err)
-- vim.cmd("redraw")
-- end
-- end

local function real_progress_cb(ptr)
print("Inside real_progress_cb, about to ffi.string(ptr)")
io.stdout:flush()

local msg = ffi.string(ptr) -- If this fails, it won't kill the process if wrapped by pcall
print("Successfully got msg:", msg)
io.stdout:flush()

local function progress_cb(ptr)
local msg = ffi.string(ptr)
window.update_progress_window(buf, msg)
end

local function safe_progress_cb(ptr)
local ok, err = xpcall(function()
real_progress_cb(ptr)
end, debug.traceback)

if not ok then
print("Lua callback error:", err)
io.stdout:flush()
-- DO NOT re-throw the error; otherwise it unwinds back into Rust
end
end

local progress_cb_c = ffi.cast("progress_callback_t", safe_progress_cb)
local progress_cb_c = ffi.cast("progress_callback_t", progress_cb)

-- M.progress_cb_c = ffi.cast("progress_callback_t", progress_cb)
lib.build_index(progress_cb_c)
local end_time = os.clock()
window.update_progress_window(buf, string.format("Built RFC index", end_time - start_time))
Expand Down

0 comments on commit 221dcdb

Please sign in to comment.