aboutsummaryrefslogtreecommitdiff
path: root/lua
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 /lua
parent1d9cd2bcb69ccbccea67166f9d42ec8ff6892fae (diff)
downloadnull-browser-c4413f3cca0a0750af9881276162b70f3489b499.tar.gz
null-browser-c4413f3cca0a0750af9881276162b70f3489b499.zip
Add history storage and completion + split dmenu,history into modules
Diffstat (limited to '')
-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
3 files changed, 91 insertions, 1 deletions
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