aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TODO.org8
-rw-r--r--config.lua2
-rw-r--r--lua/null-browser/api.lua135
-rw-r--r--src/LuaRuntime.cpp55
-rw-r--r--src/LuaRuntime.hpp2
5 files changed, 135 insertions, 67 deletions
diff --git a/TODO.org b/TODO.org
index 866c045..9ec5532 100644
--- a/TODO.org
+++ b/TODO.org
@@ -21,8 +21,8 @@
- [X] Close window if last view closed
- [X] Rename terms: tab to view? (also web.tabs)
- [X] Configuration lua api
-- [ ] Switch to __internals
-- [ ] Respect cli args for main window
+- [X] Switch to __internals
+- [X] Respect cli args for main window
- [ ] Tests for api
- [ ] Log stdout, errors and results from lua somewhere
- [ ] Downloading/download path config
@@ -74,8 +74,8 @@ web.view.set_url(url, { view = 1 }) -- Set url for view 1
web.view.set_url(url, { view = 1, win = 1 }) -- Set url for view 1 in win 1
web.view.set_url(url, { win = 1 }) -- Set url for current view in win 1
-web.view.new(url, { win = 1 }) -- New view in win 1
-web.view.new(url) -- New view in current window
+web.view.create(url, { win = 1 }) -- New view in win 1
+web.view.create(url) -- New view in current window
web.view.close({ view = 1 }) -- Close view 1
web.view.close() -- Close current view in current window
diff --git a/config.lua b/config.lua
index 9466a20..c86a818 100644
--- a/config.lua
+++ b/config.lua
@@ -44,7 +44,7 @@ web.keymap.set('n', 'o', function()
print(web.get('new_view_url'))
dmenu.select(history.list(), { prompt = 'Open view:' }, function(err, result)
if err or not result then return end
- web.view.new(to_url(result))
+ web.view.create(to_url(result))
end)
end)
-- Open in current view
diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua
index 7d61344..ac7aa08 100644
--- a/lua/null-browser/api.lua
+++ b/lua/null-browser/api.lua
@@ -1,7 +1,13 @@
-print("api load started")
+--- @type table
+__internals = __internals
--- @type table
-web = web
+_G.web = _G.web or {
+ keymap = {},
+ view = {},
+ history = {},
+ event = {},
+}
local function shallow_copy(t)
local t2 = {}
@@ -14,20 +20,97 @@ local inspector_loaded, inspector = pcall(require, 'inspect')
if inspector_loaded then
web.inspect = inspector.inspect
else
- web.inspect = function() print('[warn] "inspect" module not loaded') end
+ web.inspect = function(val)
+ print('[warn] "inspect" module not loaded'); return val
+ end
end
--- web.keymap = web.keymap or {}
--- web.view = web.view or {}
-web.event = web.event or {}
+--- Add a keymap
+---
+--- @param mode string|table Mode or a list of modes ("i", "n")
+--- @param key string Key sequence (Eg: `<c-t>j`)
+--- @param action function Function to run when keymap is triggered
+---
+--- @example
+--- ```lua
+--- web.keymap.set('n', 'o', function()
+--- web.view.create('https://google.com') -- Open google in new view
+--- end)
+--- ```
+function web.keymap.set(mode, key, action) return __internals.keymap_set(mode, key, action) end
+
+--- Close view
+---
+--- @param view_id? number View id to close
+---
+--- @example
+--- ```lua
+--- web.view.close() -- Close current view in active window
+--- web.view.close(3) -- Close view with id 3
+--- ```
+function web.view.close(view_id) return __internals.view_close(view_id) end
+
+--- Create a new view with a url
+---
+--- @param url string Url to open in the new view
+---
+--- @example
+--- ```lua
+--- web.view.create('https://duckduckgo.com') -- Opens url in new view
+--- web.view.create() -- Opens new_view_url by default
+--- ```
+function web.view.create(url) return __internals.view_create(url) end
+
+--- Get current view id
+---
+--- @return number view_id Current view id
+---
+--- @example
+--- ```lua
+--- local view_id = web.view.current()
+--- ```
+function web.view.current() return __internals.view_current() end
+
+--- Get a list of views in the current window
+---
+--- @return table views List of views
+---
+--- @example
+--- ```lua
+--- local views = web.view.list()
+--- print(views[1].url, views[1].title, views[1].id)
+--- ```
+function web.view.list() return __internals.view_list() end
+
+--- Select a view in current window
+---
+--- @param view_id number View id to select
+---
+--- @example
+--- ```lua
+--- web.view.select(3) -- Select view with id 3
+--- ```
+function web.view.select(view_id) return __internals.view_select(view_id) end
+
+--- Set url of a given view
+---
+--- @param url string Url to open
+--- @param view_id? number View id to select
+---
+--- @example
+--- ```lua
+--- web.view.set_url('https://foobar.com')
+--- web.view.set_url('https://foobar.com', 3) -- Set url for view with id 3
+--- ```
+function web.view.set_url(url, view_id) return __internals.view_set_url(url, view_id) end
--- Listen to events from the browser
---
---- @class null.EventListenerOptions
+--- @class nullb.EventListenerOptions
--- @field callback any Callback called when that event occurs
---
--- @param events string|table Event or events to listen to
---- @param opts null.EventListenerOptions Options
+--- @param opts nullb.EventListenerOptions Options
---
--- @example
--- ```lua
@@ -42,21 +125,19 @@ function web.event.add_listener(events, opts)
if type(events) == "string" then events = { events } end
opts.events = events or {}
- return __internals.register_event(opts)
+ return __internals.event_add_listener(opts)
end
--- Set configuration options
---
---- @param key string The name of the configuration
---- @param value string|boolean|number Configuration value
+--- @param key string The name of the configuration
+--- @param value nil|string|boolean|number Configuration value
---
--- @example
--- ```lua
--- web.set('new_view_url', 'https://duckduckgo.com')
--- ```
-function web.set(key, value)
- __internals.set_config(key, value)
-end
+function web.set(key, value) __internals.config_set(key, value) end
--- Get configuration value
---
@@ -66,8 +147,28 @@ end
--- ```lua
--- local url = web.get('new_view_url')
--- ```
-function web.get(key)
- return __internals.get_config(key)
-end
+function web.get(key) return __internals.config_get(key) end
+
+--- Go back in history for given view
+---
+--- @param count? number Number of history items to go back in (default = 1)
+--- @param view_id? number Id of the view
+---
+--- @example
+--- ```lua
+--- web.history.back(2, 3) -- Go back 2 history items for view id 3
+--- ```
+function web.history.back(count, view_id) return __internals.history_back(count, view_id) end
+
+--- Go forward in history for given view
+---
+--- @param count? number Number of history items to go forward in (default = 1)
+--- @param view_id? number Id of the view
+---
+--- @example
+--- ```lua
+--- web.history.forward(2, 3) -- Go forward 2 history items for view id 3
+--- ```
+function web.history.forward(count, view_id) return __internals.history_forward(count, view_id) end
print("api loaded")
diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp
index 07d8193..5646d2b 100644
--- a/src/LuaRuntime.cpp
+++ b/src/LuaRuntime.cpp
@@ -129,54 +129,23 @@ void LuaRuntime::init_web_lib() {
// NOLINTBEGIN(modernize-avoid-c-arrays)
luaL_Reg internals[] = {
- {"register_event", &LuaRuntime::lua_event_register},
- {"set_config", &LuaRuntime::lua_config_set},
- {"get_config", &LuaRuntime::lua_config_get},
+ {"event_add_listener", &LuaRuntime::lua_event_register},
+ {"config_set", &LuaRuntime::lua_config_set},
+ {"config_get", &LuaRuntime::lua_config_get},
+ {"keymap_set", &LuaRuntime::lua_keymap_set},
+ {"view_close", &LuaRuntime::lua_view_close},
+ {"view_create", &LuaRuntime::lua_view_create},
+ {"view_current", &LuaRuntime::lua_view_current},
+ {"view_list", &LuaRuntime::lua_view_list},
+ {"view_select", &LuaRuntime::lua_view_select},
+ {"view_set_url", &LuaRuntime::lua_open_url},
+ {"history_back", &LuaRuntime::lua_history_back},
+ {"history_forward", &LuaRuntime::lua_history_forward},
{nullptr, nullptr},
};
luaL_newlib(state, internals);
lua_setglobal(state, internals_global_name);
- // web
- luaL_Reg web[] = {
- /// @deprecated
- {"open", &LuaRuntime::lua_open_url},
- {nullptr, nullptr},
- };
- luaL_newlib(state, web);
- lua_setglobal(state, web_global_name);
- lua_getglobal(state, web_global_name);
-
- // Keymap api (web.keymap)
- luaL_Reg webkeymap[] = {
- {"set", &LuaRuntime::lua_keymap_set},
- {nullptr, nullptr},
- };
- luaL_newlib(state, webkeymap);
- lua_setfield(state, -2, "keymap");
-
- // View actions (web.view)
- luaL_Reg webviews[] = {
- {"close", &LuaRuntime::lua_view_close},
- {"new", &LuaRuntime::lua_view_create},
- {"current", &LuaRuntime::lua_view_current},
- {"list", &LuaRuntime::lua_view_list},
- {"select", &LuaRuntime::lua_view_select},
- {"set_url", &LuaRuntime::lua_open_url},
- {nullptr, nullptr},
- };
- luaL_newlib(state, webviews);
- lua_setfield(state, -2, "view");
-
- // History navigation
- luaL_Reg webhistory[] = {
- {"back", &LuaRuntime::lua_history_back},
- {"forward", &LuaRuntime::lua_history_forward},
- {nullptr, nullptr},
- };
- luaL_newlib(state, webhistory);
- lua_setfield(state, -2, "history");
-
// NOLINTEND(modernize-avoid-c-arrays)
}
diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp
index 670c0ae..4978c74 100644
--- a/src/LuaRuntime.hpp
+++ b/src/LuaRuntime.hpp
@@ -3,7 +3,6 @@
#include <QtCore>
#include <functional>
#include <lua.hpp>
-#include <string>
#include "AsyncEventLoop.hpp"
#include "lua.h"
@@ -25,7 +24,6 @@ class LuaRuntime : public QObject {
Q_OBJECT
const char *uv_global_name = "uv";
- const char *web_global_name = "web";
const char *internals_global_name = "__internals";
public: