aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-04-05 15:41:10 +0530
committerAkshay Nair <phenax5@gmail.com>2025-04-05 16:00:03 +0530
commitc4413f3cca0a0750af9881276162b70f3489b499 (patch)
treeeb1cbf577f0751e7293e3d1e96147bc36e48719e
parent1d9cd2bcb69ccbccea67166f9d42ec8ff6892fae (diff)
downloadnull-browser-c4413f3cca0a0750af9881276162b70f3489b499.tar.gz
null-browser-c4413f3cca0a0750af9881276162b70f3489b499.zip
Add history storage and completion + split dmenu,history into modules
-rw-r--r--TODO.org8
-rw-r--r--config.lua54
-rw-r--r--lua/null-browser/api.lua4
-rw-r--r--lua/null-browser/extras/dmenu.lua32
-rw-r--r--lua/null-browser/extras/history.lua56
-rw-r--r--src/LuaRuntime.cpp2
6 files changed, 107 insertions, 49 deletions
diff --git a/TODO.org b/TODO.org
index 5c00f33..bacf005 100644
--- a/TODO.org
+++ b/TODO.org
@@ -14,10 +14,10 @@
- [X] New window on new window request
- [X] Socket for opening window in current session (lua eval)
- [X] Config loading and lua path
-- [ ] Events/autocommands
-- [ ] Url changed event
-- [ ] History storage
-- [ ] History completion
+- [X] Events/autocommands
+- [X] Url changed event
+- [X] History storage
+- [X] History completion
- [ ] Log stdout, errors and results from lua somewhere
- [ ] Downloading
diff --git a/config.lua b/config.lua
index 8ce44ad..4199574 100644
--- a/config.lua
+++ b/config.lua
@@ -6,7 +6,7 @@ local web = web
local uv = uv
local function trim(s)
- local res, _ = string.gsub(s, "^%s*(.-)%s*$", "%1")
+ local res, _ = string.gsub(s, '^%s*(.-)%s*$', '%1')
return res
end
@@ -19,58 +19,26 @@ local function get_current_tab_index()
end
end
-local Dmenu = {}
-function Dmenu.select(list, opts, callback)
- print('DEMNU CLALED')
- local selection = nil
- local stdin = uv.new_pipe();
- local stdout = uv.new_pipe();
- 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)
- if code == 0 then
- callback(nil, selection)
- else
- callback('Exit with status code: ' .. code, selection)
- end
- end)
-
- uv.read_start(stdout, function(_, data)
- if data then selection = data end
- end)
-
- for _, value in ipairs(list) do
- uv.write(stdin, value .. '\n')
- end
- uv.shutdown(stdin)
-end
-
-local urls = {
- 'https://excalidraw.com',
- 'https://lite.duckduckgo.com',
- 'https://ediblemonad.dev',
- 'https://google.com',
- 'https://github.com/trending',
-}
+local dmenu = require 'null-browser.extras.dmenu'
+local history = require 'null-browser.extras.history'
web.event.add_listener('UrlChanged', {
callback = function(opts)
- print("URL CHANGE YEAH");
- print(opts.url);
+ print('url change', opts.url);
+ history.add(opts.url)
end
})
-- Open in new tab
web.keymap.set('n', 'o', function()
- Dmenu.select(urls, { prompt = 'Open tab:' }, function(err, result)
+ dmenu.select(history.list(), { prompt = 'Open tab:' }, function(err, result)
if err or not result then return end
web.tabs.new(trim(result))
end)
end)
-- Open in current tab
web.keymap.set('n', '<s-o>', function()
- Dmenu.select(urls, { prompt = 'Open:' }, function(err, result)
+ dmenu.select(history.list(), { prompt = 'Open:' }, function(err, result)
if err or not result then return end
web.tabs.set_url(trim(result))
end)
@@ -80,7 +48,7 @@ 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)
+ dmenu.select(history.list(), { prompt = 'Set url:', input = tab.url }, function(err, result)
if err or not result then return end
web.tabs.set_url(trim(result))
end)
@@ -88,7 +56,7 @@ end)
-- Run lua code
web.keymap.set('n', 'q', function()
- Dmenu.select({}, { prompt = 'Lua:' }, function(err, result)
+ dmenu.input({ 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
@@ -110,9 +78,9 @@ web.keymap.set('n', 'b', function()
for index, tab in ipairs(web.tabs.list()) do
table.insert(tabs_list, index .. ': ' .. tab.title .. ' (' .. tab.url .. ')')
end
- Dmenu.select(tabs_list, { prompt = 'Tab:' }, function(err, result)
+ dmenu.select(tabs_list, { prompt = 'Tab:' }, function(err, result)
if err or not result then return end
- local index_str, _ = trim(result):gsub("%s*:.*$", "")
+ local index_str, _ = trim(result):gsub('%s*:.*$', '')
local index = tonumber(index_str)
if tabs[index] then
web.tabs.select(tabs[index].id)
diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua
index d2e0be7..1be7589 100644
--- a/lua/null-browser/api.lua
+++ b/lua/null-browser/api.lua
@@ -1,4 +1,4 @@
-print("FOOOOOOOOOOBARRRRRR")
+print("api loaded")
local function shallow_copy(t)
local t2 = {}
@@ -6,6 +6,8 @@ local function shallow_copy(t)
return t2
end
+-- web.keymap = web.keymap or {}
+-- web.tab = web.tab or {}
web.event = web.event or {}
web.event.add_listener = function(events, opts)
diff --git a/lua/null-browser/extras/dmenu.lua b/lua/null-browser/extras/dmenu.lua
new file mode 100644
index 0000000..1bd467f
--- /dev/null
+++ b/lua/null-browser/extras/dmenu.lua
@@ -0,0 +1,32 @@
+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 '>', '-it', opts.input or '' }
+ uv.spawn('dmenu', { args = args, stdio = { stdin, stdout, nil } }, function(code)
+ uv.close(stdout)
+ uv.close(stdin)
+ if code == 0 then
+ callback(nil, selection)
+ else
+ callback('Exit with status code: ' .. code, selection)
+ end
+ end)
+
+ uv.read_start(stdout, function(_, data)
+ if data then selection = data end
+ end)
+
+ for _, value in ipairs(list) do
+ uv.write(stdin, value .. '\n')
+ end
+ uv.shutdown(stdin)
+end
+
+function dmenu.input(opts, callback)
+ dmenu.select({}, opts, callback)
+end
+
+return dmenu
diff --git a/lua/null-browser/extras/history.lua b/lua/null-browser/extras/history.lua
new file mode 100644
index 0000000..2ae48d5
--- /dev/null
+++ b/lua/null-browser/extras/history.lua
@@ -0,0 +1,56 @@
+local history = {
+ path = './history',
+ max_entires = 200,
+}
+
+function history.list()
+ local file = uv.fs_open(history.path, 'r', 438)
+ if not file then return {} end
+ local stat = assert(uv.fs_fstat(file))
+ local data = assert(uv.fs_read(file, stat.size))
+ assert(uv.fs_close(file))
+
+ local urls = {}
+ for line in string.gmatch(data, '[^\r\n]+') do
+ local already_exists = false
+ for _, url in ipairs(urls) do
+ if url == line then already_exists = true end
+ end
+ if #urls >= history.max_entires then break end
+ if not already_exists then
+ table.insert(urls, line)
+ end
+ end
+
+ return urls
+end
+
+function history.update(func)
+ local urls = history.list()
+ local file = assert(uv.fs_open(history.path, 'w', 438))
+ urls = func(urls)
+ local contents = table.concat(urls, '\n')
+ assert(uv.fs_write(file, contents))
+ assert(uv.fs_close(file))
+end
+
+function history.add(url)
+ history.update(function(urls)
+ table.insert(urls, 1, url)
+ return urls
+ end)
+end
+
+function history.delete(url_to_delete)
+ history.update(function(urls)
+ for index, url in urls do
+ if url == url_to_delete then
+ table.remove(urls, index)
+ return urls
+ end
+ end
+ return urls
+ end)
+end
+
+return history
diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp
index 70301b4..328cd89 100644
--- a/src/LuaRuntime.cpp
+++ b/src/LuaRuntime.cpp
@@ -115,7 +115,7 @@ int LuaRuntime::lua_event_register(lua_State *state) {
preserve_top(state, {
lua_rawgeti(state, LUA_REGISTRYINDEX, function_ref);
event->lua_push(state);
- lua_call(state, 1, 0);
+ lua_pcall(state, 1, 0, 0);
})
};