Skip to content

Commit

Permalink
Some progress logging
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmturner committed Jan 4, 2025
1 parent f1f4d8f commit bba9435
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 7 deletions.
Binary file modified artifacts/libffi.dylib
Binary file not shown.
6 changes: 5 additions & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ enum Command {
},
}

extern "C" fn progress_callback(progress: f64) {
println!("Downloading RFCs progress: {progress:.2}%")
}

fn handle_command(args: Args) -> RFSeeResult<()> {
if let Some(command) = args.clone().command {
match command {
Command::Index { path } => {
println!("Indexing RFCs");
let start = Instant::now();
let mut index = TfIdf::default();
index.par_load_rfcs()?;
index.par_load_rfcs(progress_callback)?;
println!("Loading RFCs took {:?}", start.elapsed());
let building_index_start = Instant::now();
index.finish();
Expand Down
4 changes: 2 additions & 2 deletions crates/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ struct RfcSearchResultsContainer {
}

#[no_mangle]
pub extern "C" fn build_index() {
pub extern "C" fn build_index(callback: extern "C" fn(progress: f64)) {
let path = rfsee_tf_idf::get_index_path(None).unwrap();
let mut index = rfsee_tf_idf::TfIdf::default();
index.par_load_rfcs().unwrap();
index.par_load_rfcs(callback).unwrap();
index.finish();
index.save(&path);
}
Expand Down
8 changes: 7 additions & 1 deletion crates/tf_idf/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ impl TfIdf {
}

/// Load the RFCs in parallel using a threadpool
pub fn par_load_rfcs(&mut self) -> RFSeeResult<()> {
pub fn par_load_rfcs(&mut self, callback: extern "C" fn(progress: f64)) -> RFSeeResult<()> {
let pool = threadpool::ThreadPool::new(12);
let raw_rfc_index = fetch_rfc_index()?;
let raw_rfcs = parse_rfc_index(&raw_rfc_index)?;
let rfcs_count = raw_rfcs.len();

let parsed_rfcs: Vec<RfcEntry> = Vec::new();
let parsed_rfcs = Arc::new(Mutex::new(parsed_rfcs));
Expand All @@ -159,6 +160,11 @@ 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;
callback(progress)
}
};
let mut guard = remaining.lock().unwrap();
*guard -= 1;
Expand Down
4 changes: 3 additions & 1 deletion lua/rfsee/ffi.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
local ffi = require("ffi")

ffi.cdef([[
void build_index();
typedef void (*progress_callback_t)(double progress);
void build_index(progress_callback_t cb);
struct RfcSearchResult {
const char* url;
const char* title;
Expand Down
11 changes: 10 additions & 1 deletion lua/rfsee/index.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,20 @@ function M.search_terms(terms)
end, { buffer = results_buf, noremap = true, silent = true })
end

-- Our Lua callback, cast to a C function pointer

function M.refresh()
local start_time = os.clock()
local buf, win = window.create_progress_window()
window.update_progress_window(buf, "Building RFC index")
lib.build_index()

local function on_progress(pct)
local msg = string.format("Downloading RFCs progress: %.1f%%", pct)
window.update_progress_window(buf, msg)
end

local on_progress_c = ffi.cast("progress_callback_t", on_progress)
lib.build_index(on_progress_c)
local end_time = os.clock()
window.update_progress_window(buf, string.format("Built RFC index", end_time - start_time))
-- Brief pause before closing
Expand Down
2 changes: 1 addition & 1 deletion lua/rfsee/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local M = {}

function M.create_progress_window()
local buf = vim.api.nvim_create_buf(false, true)
local width = 20
local width = 35
local height = 1
local row = 0
local col = vim.o.columns - (width + 1) -- Place it at the top right
Expand Down

0 comments on commit bba9435

Please sign in to comment.