diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-03-27 13:08:20 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-03-28 19:39:52 +0530 |
| commit | 46a7fee95a010f20490d7f66f0c2c82ed936ca5e (patch) | |
| tree | 9afcbacfb0cdc22f44fb1c61f196347a391e5bd5 /config.lua | |
| parent | a54f6ae81b54ca59bf913bba16f271a35ca08d9d (diff) | |
| download | null-browser-46a7fee95a010f20490d7f66f0c2c82ed936ca5e.tar.gz null-browser-46a7fee95a010f20490d7f66f0c2c82ed936ca5e.zip | |
Refactor and fix tab id use for focus/close
Diffstat (limited to 'config.lua')
| -rw-r--r-- | config.lua | 57 |
1 files changed, 35 insertions, 22 deletions
@@ -5,12 +5,23 @@ web = web --- @type table uv = uv +local function trim(s) return s:gsub("^%s*(.-)%s*$", "%1") end + +local function get_current_tab_index() + local currentTab = web.tabs.current(); + for index, tab in ipairs(web.tabs.list()) do + if tab.id == currentTab then + return index + end + end +end + local Dmenu = {} function Dmenu.select(list, opts, callback) local selection = nil local stdin = uv.new_pipe(); local stdout = uv.new_pipe(); - local args = { '-p', opts.prompt or '>' } + local args = { '-p', opts.prompt or '>', '-it', opts.input or '' } uv.spawn('dmenu', { args = args, stdio = { stdin, stdout, nil } }, function(code) uv.close(stdout) uv.close(stdin) @@ -31,10 +42,6 @@ function Dmenu.select(list, opts, callback) uv.shutdown(stdin) end -local function trim(s) - return s:gsub("^%s*(.-)%s*$", "%1") -end - local urls = { 'https://excalidraw.com', 'https://lite.duckduckgo.com', @@ -57,10 +64,20 @@ web.keymap.set('n', '<s-o>', function() web.open(trim(result)) end) end) +-- Update current url +web.keymap.set('n', '<c-l>', function() + local tabs = web.tabs.list() + local tab = tabs[get_current_tab_index()]; + if tab == nil then return end + Dmenu.select(urls, { prompt = 'Set url:', input = tab.url }, function(err, result) + if err or not result then return end + web.open(trim(result)) + end) +end) -- Run lua code web.keymap.set('n', 'q', function() - Dmenu.select({}, { prompt = 'Lua >' }, function(err, result) + Dmenu.select({}, { prompt = 'Lua:' }, function(err, result) if err or not result then return end local run, run_err = load(result) if run_err then print(run_err) end @@ -71,30 +88,27 @@ end) -- History back/forward web.keymap.set('n', '<s-h>', function() web.history.back(); end) web.keymap.set('n', '<s-l>', function() web.history.forward(); end) + +-- Close tab web.keymap.set('n', '<c-w>', function() web.tabs.close(); end) -- Tab select web.keymap.set('n', 'b', function() - local tabsList = {} - for _, tab in ipairs(web.tabs.list()) do - table.insert(tabsList, tab.id .. ': ' .. tab.title .. ' (' .. tab.url) + local tabs_list = {} + local tabs = web.tabs.list() + for index, tab in ipairs(web.tabs.list()) do + table.insert(tabs_list, index .. ': ' .. tab.title .. ' (' .. tab.url .. ')') end - Dmenu.select(tabsList, { prompt = 'Tab:' }, function(err, result) + Dmenu.select(tabs_list, { prompt = 'Tab:' }, function(err, result) if err or not result then return end - local id, _ = trim(result):gsub("%s*:.*$", "") - web.tabs.select(id) + local index_str, _ = trim(result):gsub("%s*:.*$", "") + local index = tonumber(index_str) + if tabs[index] then + web.tabs.select(tabs[index].id) + end end) end) -local function get_current_tab_index() - local currentTab = web.tabs.current(); - for index, tab in ipairs(web.tabs.list()) do - if tab.id == currentTab then - return index - end - end -end - -- Next tab web.keymap.set('n', 'tn', function() local tabs = web.tabs.list() @@ -106,7 +120,6 @@ end) -- Prev tab web.keymap.set('n', 'tp', function() - print('-----------') local tabs = web.tabs.list() if #tabs <= 1 then return end local index = get_current_tab_index() - 1; |
