From 4afe7914a4d4f59703d14f9ba8470ed87831ddcb Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 1 May 2025 20:13:47 +0530 Subject: Add stdlib and web.utils --- lua/null-browser/api.lua | 31 ++++++++----------------------- lua/null-browser/defaults/vi.lua | 18 ++++++------------ lua/null-browser/stdlib.lua | 30 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 lua/null-browser/stdlib.lua (limited to 'lua') 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 -- cgit v1.3.1