diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-08-17 11:41:12 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-08-17 11:41:12 +0530 |
| commit | b16d38df05cd80aac209f42bfcb47971c62227e6 (patch) | |
| tree | 4e10fd1ef7d9c39adf57fdae7aa00240118b799c | |
| parent | 6906c5962549494812bed8b5f0c3bbc499a86751 (diff) | |
| download | null-browser-b16d38df05cd80aac209f42bfcb47971c62227e6.tar.gz null-browser-b16d38df05cd80aac209f42bfcb47971c62227e6.zip | |
Add extras.system module + yu key to copy to clipboard
| -rw-r--r-- | TODO.org | 2 | ||||
| -rw-r--r-- | init.lua | 2 | ||||
| -rw-r--r-- | lua/null-browser/api.lua | 12 | ||||
| -rw-r--r-- | lua/null-browser/defaults/vi.lua | 14 | ||||
| -rw-r--r-- | lua/null-browser/extras/hints.lua | 25 | ||||
| -rw-r--r-- | lua/null-browser/extras/html.lua | 11 | ||||
| -rw-r--r-- | lua/null-browser/extras/system.lua | 10 |
7 files changed, 44 insertions, 32 deletions
@@ -22,6 +22,7 @@ - [X] json encode/decode - [X] Fix path to symbols.json in web.help.get_items (use docs path) - [X] Document events and event opts +- [ ] Element focus (input focus) - [ ] Update all api to use opts table pattern - [ ] web.keymap.configure_mode(modename, { passthrough = false }) - [ ] Fullscreen @@ -40,7 +41,6 @@ ** Next - [ ] View focus event (between decorations/views) -- [ ] Element focus (input focus) - [ ] User scripts (greasemonkey?) - [ ] User stylesheets (per site and global?) - [ ] Log stdout, errors and results from lua somewhere @@ -166,7 +166,7 @@ end) local hints = require 'null-browser.extras.hints' hints.init() web.keymap.set('n', 'f', function() - hints.start('a[href], button, [role="button"]', hints.action.open_in_view) + hints.start('a[href], button, [role="button"]', hints.action.click) end) web.keymap.set('n', '<s-f>', function() hints.start('a[href], button, [role="button"]', hints.action.open_in_new_view) diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua index 0625869..4e96e29 100644 --- a/lua/null-browser/api.lua +++ b/lua/null-browser/api.lua @@ -74,6 +74,18 @@ function web.view.create(url) return __internals.view_create(url) end --- local view_id = web.view.current() function web.view.current() return __internals.view_current() end +--- Get current url +--- +--- @return string url Current view url +--- +--- @usage +--- local url = web.view.current_url() +function web.view.current_url() + local views = web.view.list() + local view = views[web.view.current_index()]; + return view and view.url +end + --- Get a list of views in the current window --- --- @return table views List of views diff --git a/lua/null-browser/defaults/vi.lua b/lua/null-browser/defaults/vi.lua index ba7eaba..5d9210b 100644 --- a/lua/null-browser/defaults/vi.lua +++ b/lua/null-browser/defaults/vi.lua @@ -53,6 +53,11 @@ function M.initialize() end) end) + -- Copy current url to clipboard + web.keymap.set('n', 'yu', function() + require 'null-browser.extras.system'.copy_to_clipboard(web.view.current_url() or '') + end) + -- Search web.keymap.set('n', '<c-f>', function() config.menu:input({ prompt = 'Search:', query = web.search.get_text() }, function(err, input) @@ -131,12 +136,11 @@ function M.initialize() -- Update current url web.keymap.set('n', '<c-l>', function() - local views = web.view.list() - local view = views[web.view.current_index()]; - if view == nil then return end - config.menu:select(config.history.list(), { prompt = 'Set url:', query = view.url }, function(err, result) + local url = web.view.current_url() + local view = web.view.current() + config.menu:select(config.history.list(), { prompt = 'Set url:', query = url or '' }, function(err, result) if err or not result then return end - web.view.set_url(web.utils.string_trim(result)) + web.view.set_url(web.utils.string_trim(result), view) end) end) diff --git a/lua/null-browser/extras/hints.lua b/lua/null-browser/extras/hints.lua index df518cd..1ada44c 100644 --- a/lua/null-browser/extras/hints.lua +++ b/lua/null-browser/extras/hints.lua @@ -72,37 +72,24 @@ end function hints.action.open_in_new_view() web.view.run_js [[ const match = _nullbrowser.hints.currentMatch; - if (match.elem?.href) { - window.open(match.elem.href) - } else { - match.elem?.click() - } + if (match.elem?.href) window.open(match.elem.href); + else match.elem?.click(); ]] end -function hints.action.open_in_view() - web.view.run_js [[ - const match = _nullbrowser.hints.currentMatch; - if (match.elem?.href) { - location.href = match.elem.href - } else { - match.elem?.click() - } - ]] +function hints.action.click() + web.view.run_js [[_nullbrowser.hints.currentMatch?.elem?.click?.();]] end function hints.action.focus() web.keymap.set_mode 'i' - web.view.run_js([[ _nullbrowser.hints.currentMatch?.elem?.focus?.(); ]]) + web.view.run_js([[_nullbrowser.hints.currentMatch?.elem?.focus?.();]]) end function hints.action.copy_link() web.view.run_js([[_nullbrowser.hints.currentMatch.elem?.href]], { on_result = function(url) - local pipe = io.popen('xclip -selection clipboard', 'w') - if not pipe then return end - pipe:write(url) - pipe:close() + require 'null-browser.extras.system'.copy_to_clipboard(url) end, }) end diff --git a/lua/null-browser/extras/html.lua b/lua/null-browser/extras/html.lua index db4e864..27db8a0 100644 --- a/lua/null-browser/extras/html.lua +++ b/lua/null-browser/extras/html.lua @@ -41,12 +41,11 @@ html.a = html.create_el('a') html.input = html.create_el('input') function html.escape_string(str) - str = str:gsub('&', '&') - str = str:gsub('<', '<') - str = str:gsub('>', '>') - str = str:gsub('"', '"') - str = str:gsub("'", ''') - return str + return str:gsub('&', '&') + :gsub('<', '<') + :gsub('>', '>') + :gsub('"', '"') + :gsub("'", ''') end return html diff --git a/lua/null-browser/extras/system.lua b/lua/null-browser/extras/system.lua new file mode 100644 index 0000000..78f47df --- /dev/null +++ b/lua/null-browser/extras/system.lua @@ -0,0 +1,10 @@ +local system = {} + +function system.copy_to_clipboard(text) + local pipe = io.popen('xclip -selection clipboard', 'w') + if not pipe then return end + pipe:write(text) + pipe:close() +end + +return system |
