From 345320bb705d776afefdc9602b7d9dc05863403f Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Wed, 23 Jul 2025 11:23:28 +0530 Subject: Update dmenu to use input text instead of selection --- lua/null-browser/defaults/vi.lua | 14 ++++++++------ lua/null-browser/extras/dmenu.lua | 32 +++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) (limited to 'lua') diff --git a/lua/null-browser/defaults/vi.lua b/lua/null-browser/defaults/vi.lua index 86302ee..2213fc5 100644 --- a/lua/null-browser/defaults/vi.lua +++ b/lua/null-browser/defaults/vi.lua @@ -42,10 +42,11 @@ function M.initialize() end) -- Delete from history 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(web.utils.string_trim(result)) - end) + config.menu:select(config.history.list(), { prompt = 'Delete history:', select_last_line = true }, + function(err, result) + if err or not result then return end + config.history.delete(web.utils.string_trim(result)) + end) end) -- Search @@ -85,7 +86,7 @@ function M.initialize() table.insert(views_list, index .. ': ' .. view.title .. ' (' .. view.url .. ')') end - config.menu:select(views_list, { prompt = 'Views:' }, function(err, result) + config.menu:select(views_list, { prompt = 'Views:', select_last_line = true }, function(err, result) if err or not result then return end local index_str, _ = string.gsub(result, '%s*:.*$', '') local index = tonumber(index_str) @@ -140,7 +141,8 @@ function M.initialize() web.event.add_listener('PermissionRequested', { callback = function(event) - config.menu:select({ 'Allow', 'Deny' }, { prompt = 'Requesting permission for ' .. event.permission_type }, + config.menu:select({ 'Allow', 'Deny' }, + { prompt = 'Requesting permission for ' .. event.permission_type, select_last_line = true }, function(err, choice) if err then return end if web.utils.string_trim(choice) == 'Allow' then diff --git a/lua/null-browser/extras/dmenu.lua b/lua/null-browser/extras/dmenu.lua index f817294..5d660bb 100644 --- a/lua/null-browser/extras/dmenu.lua +++ b/lua/null-browser/extras/dmenu.lua @@ -10,6 +10,8 @@ function Dmenu:new(opts) query_arg = '-it', prompt = nil, query = nil, + select_last_line = true, + transform_output = nil, } local obj = {} setmetatable(obj, self) @@ -18,7 +20,7 @@ function Dmenu:new(opts) return obj end -function Dmenu:prepare_command(opts) +function Dmenu:prepare_options(opts) opts = opts or {} local args = web.utils.table_merge({}, self.options.args or {}) for _, arg in ipairs(opts.args or {}) do @@ -37,29 +39,45 @@ function Dmenu:prepare_command(opts) table.insert(args, options.query) end - return { command = options.command, args = args } + return { + command = options.command, + args = args, + select_last_line = options.select_last_line, + transform_output = options.transform_output or function(s) return s end, + } end function Dmenu:select(list, opts, callback) - local cmd = self:prepare_command(opts) + local options = self:prepare_options(opts) local stdin = web.uv.new_pipe(); local stdout = web.uv.new_pipe(); local selection = nil - web.uv.spawn(cmd.command, { args = cmd.args, stdio = { stdin, stdout, nil } }, function(code) + if not options.select_last_line then + selection = {} + end + web.uv.spawn(options.command, { args = options.args, stdio = { stdin, stdout, nil } }, function(code) web.uv.close(stdout) web.uv.close(stdin) + local result = options.transform_output(selection) if code == 0 then - callback(nil, selection) + callback(nil, result) else - callback('[dmenu] Exit with status code: ' .. code, selection) + callback('[dmenu] Exit with status code: ' .. code, result) end end) web.uv.read_start(stdout, function(_, data) - if data then selection = data end + if data then + if options.select_last_line then + selection = data + else + selection = selection or {} + table.insert(selection, data) + end + end end) for _, value in ipairs(list) do -- cgit v1.3.1