diff options
| -rw-r--r-- | TODO.org | 2 | ||||
| -rw-r--r-- | lua/null-browser/api.lua | 31 | ||||
| -rw-r--r-- | lua/null-browser/defaults/vi.lua | 18 | ||||
| -rw-r--r-- | lua/null-browser/stdlib.lua | 30 | ||||
| -rw-r--r-- | src/LuaRuntime.cpp | 8 | ||||
| -rw-r--r-- | src/LuaRuntime.hpp | 1 |
6 files changed, 52 insertions, 38 deletions
@@ -7,8 +7,8 @@ - [X] Configuration - [X] Searchengines - [X] Scroll api (for j/k/h/l/gg/G) +- [X] table extend/merge - [ ] Tests for api -- [ ] table extend/merge - [ ] Fullscreen - [ ] Zoom in/out - [ ] Permission requests handling/persisting diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua index 170347f..cb8fd77 100644 --- a/lua/null-browser/api.lua +++ b/lua/null-browser/api.lua @@ -3,29 +3,14 @@ __internals = __internals --- @type table -_G.web = _G.web or { - search = {}, - keymap = {}, - view = {}, - history = {}, - event = {}, -} +_G.web = _G.web or {} +web.search = web.search or {} +web.keymap = web.keymap or {} +web.view = web.view or {} +web.history = web.history or {} +web.event = web.event or {} -local function shallow_copy(t) - local t2 = {} - for k, v in pairs(t) do t2[k] = v end - return t2 -end - --- inspect lua tables -local inspector_loaded, inspector = pcall(require, 'inspect') -if inspector_loaded then - web.inspect = inspector.inspect -else - web.inspect = function(val) - print('[warn] "inspect" module not loaded'); return val - end -end +require 'null-browser.stdlib' --- Add a keymap --- @@ -171,7 +156,7 @@ end --- }) --- ``` function web.event.add_listener(events, opts) - opts = shallow_copy(opts or {}) + opts = web.utils.table_merge({}, opts or {}) if type(events) == "string" then events = { events } end opts.events = events or {} diff --git a/lua/null-browser/defaults/vi.lua b/lua/null-browser/defaults/vi.lua index d73ffb5..1b72f60 100644 --- a/lua/null-browser/defaults/vi.lua +++ b/lua/null-browser/defaults/vi.lua @@ -1,20 +1,13 @@ -local function trim(s) - local res, _ = string.gsub(s, '^%s*(.-)%s*$', '%1') - return res -end - local M = {} local config = { menu = require 'null-browser.extras.dmenu', history = require 'null-browser.extras.history', - preprocess_url = trim, + preprocess_url = web.utils.string_trim, } function M.setup(opts) - if opts.menu then config.menu = opts.menu end - if opts.history then config.history = opts.history end - if opts.preprocess_url then config.preprocess_url = opts.preprocess_url end + config = web.utils.table_merge(config, opts) end function M.initialize() @@ -50,7 +43,7 @@ function M.initialize() web.keymap.set('n', 'dh', function() config.menu.select(config.history.list(), { prompt = 'Delete history:' }, function(err, result) if err or not result then return end - config.history.delete(trim(result)) + config.history.delete(web.utils.string_trim(result)) end) end) @@ -90,9 +83,10 @@ function M.initialize() for index, view in ipairs(web.view.list()) do table.insert(views_list, index .. ': ' .. view.title .. ' (' .. view.url .. ')') end + config.menu.select(views_list, { prompt = 'Views:' }, function(err, result) if err or not result then return end - local index_str, _ = trim(result):gsub('%s*:.*$', '') + local index_str, _ = string.gsub(result, '%s*:.*$', '') local index = tonumber(index_str) if views[index] then web.view.select(views[index].id) @@ -136,7 +130,7 @@ function M.initialize() if view == nil then return end config.menu.select(config.history.list(), { prompt = 'Set url:', input = view.url }, function(err, result) if err or not result then return end - web.view.set_url(trim(result)) + web.view.set_url(web.utils.string_trim(result)) end) end) diff --git a/lua/null-browser/stdlib.lua b/lua/null-browser/stdlib.lua new file mode 100644 index 0000000..f96dd48 --- /dev/null +++ b/lua/null-browser/stdlib.lua @@ -0,0 +1,30 @@ +--- @type table +_G.web = _G.web or {} +web.utils = web.utils or {} + +-- inspect lua tables +local inspector_loaded, inspector = pcall(require, 'inspect') +if inspector_loaded then + web.inspect = inspector.inspect +else + web.inspect = function(val) + print('[warn] "inspect" module not loaded'); return val + end +end + +-- TODO: Documentation +function web.utils.string_trim(str) + local res, _ = string.gsub(str, '^%s*(.-)%s*$', '%1') + return res +end + +-- TODO: Documentation +function web.utils.table_merge(t, ...) + for i = 1, select("#", ...) do + for k, v in pairs(select(i, ...)) do + t[k] = v + end + end + + return t +end diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index b4c39a2..73555e0 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -48,11 +48,15 @@ void LuaRuntime::init_web_api() { luaL_newlib(state, internals_api); lua_setglobal(state, internals_global_name); + require_module("null-browser.api"); +} + +void LuaRuntime::require_module(const QString &module_name) { preserve_top(state, { lua_getglobal(state, "require"); - lua_pushstring(state, "null-browser.api"); + lua_pushstring(state, module_name.toStdString().c_str()); if (lua_pcall(state, 1, 0, 0) != LUA_OK) { - qCritical() << "Unable to load browser api" << lua_tostring(state, -1); + qCritical() << "Unable to require module" << module_name << ":" << lua_tostring(state, -1); } }); } diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp index bff200a..2af61f7 100644 --- a/src/LuaRuntime.hpp +++ b/src/LuaRuntime.hpp @@ -35,6 +35,7 @@ public: void evaluate(const QString &code); void load_file_sync(const QString &path); void append_package_path(const QString &path); + void require_module(const QString &module_name); void stop_event_loop(); void start_event_loop(); |
