From ca7c46e535ac60a976a8c60477fa9424c46b6e3f Mon Sep 17 00:00:00 2001 From: alyx Date: Fri, 5 Apr 2024 13:30:39 -0400 Subject: lua loading --- src/theming/lua.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 4 deletions(-) (limited to 'src/theming/lua.rs') diff --git a/src/theming/lua.rs b/src/theming/lua.rs index 22ad4d6..c9168cb 100644 --- a/src/theming/lua.rs +++ b/src/theming/lua.rs @@ -1,13 +1,16 @@ // SPDX-License-Identifier: AGPL-3.0-only use std::sync::LazyLock; -use std::collections::BTreeMap; use std::ops::Deref; +use std::path::Path; use tokio::sync::Mutex; -use mlua::*; +use mlua::{Lua, Compiler, StdLib, Value, LuaOptions}; +use http::StatusCode; use crate::CONFIG; +static INTERNAL_THEMES: &[(&str, &str)] = &[]; + static LUA: LazyLock> = LazyLock::new(|| { let stdlib = StdLib::TABLE | StdLib::STRING | StdLib::UTF8 | StdLib::BIT | StdLib::MATH; let lua = Lua::new_with(stdlib, LuaOptions::new().thread_pool_size(2)).expect("lua initialization"); @@ -23,13 +26,66 @@ static LUA: LazyLock> = LazyLock::new(|| { .set_compiler(Compiler::new().set_optimization_level(2).set_debug_level(1)) .eval() .expect("expect.lua loading"); - lua.globals().set("expect", expect); + lua.globals().set("expect", expect).unwrap(); let html: Value = lua.load(include_str!("lua-lib/html.lua")) .set_compiler(Compiler::new().set_optimization_level(2).set_debug_level(1)) .eval() .expect("html.lua loading"); - lua.globals().set("html", html); + lua.globals().set("html", html).unwrap(); + + let themes = lua.create_table().expect("creating themes table"); + + for (k, v) in INTERNAL_THEMES { + let _ = themes.set(*k, lua.load(*v).into_function().expect("loading internal theme")); + } + + if let Some(theme_dir) = CONFIG.theme_dir.as_ref() { + for (k, v) in walk_dir(theme_dir.as_ref().as_ref()).expect("walking theme dir") { + let _ = themes.set(k, lua.load(v).into_function().expect("loading internal theme")); + } + } + + let _ = lua.globals().set("__themes", themes).unwrap(); + + lua.globals().set_readonly(true); Mutex::new(lua) }); + +fn walk_dir(path: &Path) -> std::io::Result> { + use std::fs; + + let mut path_bits = vec![]; + let mut dir_readers = vec![fs::read_dir(path)?]; + + let mut ret = vec![]; + + while let Some(r) = dir_readers.iter_mut().last() { + if let Some(ent) = r.next() { + let ent = ent?; + let name = ent.file_name().into_string().expect("why do you have such FUCKED UP FILE PATHS"); + let ty = ent.file_type()?; + if ty.is_file() && name.ends_with(CONFIG.theme_ext_lua.as_ref()) { + ret.push((path_bits.join("") + &name, fs::read_to_string(ent.path())?)); + } + else if ty.is_dir() { + path_bits.push(name); + dir_readers.push(fs::read_dir(ent.path())?); + } + } + else { + dir_readers.pop(); + } + } + + Ok(ret) +} + +pub fn touch() { + let _ = LUA.deref(); +} + +pub fn render_theme(name: &str, ctx: &crate::ctx::Ctx) -> Option> { + return None +} -- cgit v1.2.3-54-g00ecf