Fully migrate to init.lua

This PR aims to fully migrate `config.yml` to `init.lua`

Also, use `builtin.foo_func` instead of `xplr.fn.builtin.foo_func`.
Similarly, use `custom.foo_func` instead of `xplr.fn.custom.foo_func`.

Closes: https://github.com/sayanarijit/xplr/issues/160
This commit is contained in:
Arijit Basu 2021-05-22 10:12:27 +05:30 committed by Arijit Basu
parent 0a2f2aeda8
commit da1d7742f7
4 changed files with 1821 additions and 1236 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -4,13 +4,13 @@ use anyhow::Result;
use mlua::Lua;
use mlua::LuaSerdeExt;
pub fn resolve_fn<'lua, 'a>(
fn resolve_fn_recursive<'lua, 'a>(
table: &mlua::Table<'lua>,
mut path: impl Iterator<Item = &'a str>,
) -> Result<mlua::Function<'lua>> {
if let Some(nxt) = path.next() {
match table.get(nxt)? {
mlua::Value::Table(t) => resolve_fn(&t, path),
mlua::Value::Table(t) => resolve_fn_recursive(&t, path),
mlua::Value::Function(f) => Ok(f),
t => bail!("{:?} is not a function", t),
}
@ -19,7 +19,17 @@ pub fn resolve_fn<'lua, 'a>(
}
}
/// This function resolves paths like `builtin.func_foo`, `custom.func_bar` into lua functions.
pub fn resolve_fn<'lua>(
globals: &mlua::Table<'lua>,
path: &str,
) -> Result<mlua::Function<'lua>> {
let path = format!("xplr.fn.{}", path);
resolve_fn_recursive(&globals, path.split('.'))
}
// TODO: Remove config input when config.yml is deprecated
/// Used to initialize Lua globals
pub fn init(lua: &Lua, lua_script: &str, config: Config) -> Result<Config> {
let globals = lua.globals();
@ -48,6 +58,7 @@ pub fn init(lua: &Lua, lua_script: &str, config: Config) -> Result<Config> {
Ok(config.with_version(version))
}
/// Used to extend Lua globals
pub fn extend(lua: &Lua, lua_script: &str) -> Result<Config> {
lua.load(&lua_script).set_name("init")?.exec()?;

View File

@ -550,7 +550,7 @@ fn draw_table<B: Backend>(
.filter_map(|c| {
c.format()
.to_owned()
.and_then(|f| resolve_fn(&globals, f.split('.')).ok())
.and_then(|f| resolve_fn(&globals, &f).ok())
})
.map(|f| f.call((v.clone(),)).unwrap_or_else(|e| e.to_string()))
.collect::<Vec<String>>()