summaryrefslogtreecommitdiff
path: root/src/sheet/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sheet/mod.rs')
-rw-r--r--src/sheet/mod.rs71
1 files changed, 69 insertions, 2 deletions
diff --git a/src/sheet/mod.rs b/src/sheet/mod.rs
index 015e3d8..22b73fe 100644
--- a/src/sheet/mod.rs
+++ b/src/sheet/mod.rs
@@ -1,14 +1,17 @@
+use std::{sync::{Arc, Mutex, RwLock}, thread::JoinHandle};
+
use cell::{Cell, CellRef};
-use mlua::{IntoLua, UserData, Value};
+use mlua::{Function, IntoLua, UserData, Value};
use register::{Register, SheetId};
pub mod cell;
pub mod register;
-#[derive(Debug, Default, Clone)]
+#[derive(Debug, Default)]
pub struct Sheet {
id: register::SheetId,
rows: Vec<Vec<Cell>>,
+ progress: Mutex<u8>,
}
impl Sheet {
@@ -16,6 +19,7 @@ impl Sheet {
Self {
id,
rows: vec![vec![Cell::new_empty(); width]; height],
+ progress: Mutex::new(0),
}
}
@@ -63,6 +67,24 @@ impl Sheet {
pub fn id(&self) -> register::SheetId {
self.id
}
+
+ pub fn apply(&mut self, other: Sheet) {
+ self.rows = other.rows;
+ }
+
+ pub fn progress(&self) -> u8 {
+ *self.progress.lock().unwrap()
+ }
+}
+
+impl Clone for Sheet {
+ fn clone(&self) -> Self {
+ Sheet {
+ id: self.id,
+ rows: self.rows.clone(),
+ progress: Mutex::new(0),
+ }
+ }
}
struct SheetLuaRef {
@@ -96,3 +118,48 @@ impl UserData for SheetLuaRef {
})
}
}
+
+
+pub trait EvalFunction {
+ fn eval_function(&self, func: String, range: Vec<(usize, usize)>) -> JoinHandle<Result<Sheet, String>>;
+}
+
+impl EvalFunction for Arc<RwLock<Sheet>> {
+ fn eval_function(
+ &self,
+ func_text: String,
+ range: Vec<(usize, usize)>,
+ ) -> JoinHandle<Result<Sheet, String>> {
+ let this = Arc::clone(self);
+ std::thread::spawn(move || {
+ let read_sheet = this.read().unwrap();
+ let mut sheet = read_sheet.clone();
+ *read_sheet.progress.lock().unwrap() = 0;
+ let lua = crate::lua::new_instance().unwrap();
+ let result = lua
+ .load(func_text.clone())
+ .set_name("Temp Script")
+ .eval::<Function>();
+
+ match result {
+ Ok(func) => {
+ for (i, (row, column)) in range.iter().enumerate() {
+
+ *read_sheet.progress.lock().unwrap() = (i * 100 / range.len()) as u8;
+ let cellref = read_sheet.get_ref(*row, *column);
+
+ match func.call::<_, Cell>(cellref) {
+ Ok(value) => {
+ sheet.set_cell(*row, *column, value)
+ },
+ Err(error) => return Err(error.to_string()),
+ }
+ }
+ Ok(sheet)
+ }
+ Err(error) => Err(error.to_string()),
+ }
+ })
+ }
+
+}