diff options
| -rw-r--r-- | TODO.org | 2 | ||||
| -rw-r--r-- | init.lua (renamed from config.lua) | 0 | ||||
| -rw-r--r-- | lua/null-browser/api.lua | 85 | ||||
| -rw-r--r-- | lua/null-browser/extras/history.lua | 2 | ||||
| -rw-r--r-- | src/Configuration.hpp | 12 | ||||
| -rw-r--r-- | src/LuaRuntime.cpp | 26 | ||||
| -rw-r--r-- | src/LuaRuntime.hpp | 3 | ||||
| -rw-r--r-- | src/main.cpp | 8 | ||||
| -rw-r--r-- | src/widgets/BrowserApp.cpp | 17 | ||||
| -rw-r--r-- | src/widgets/BrowserApp.hpp | 5 |
10 files changed, 123 insertions, 37 deletions
@@ -4,7 +4,7 @@ - [X] Update window title on current webview title change - [X] Search text in page - [X] Dev tools -- [ ] Configuration +- [X] Configuration - [ ] Scroll api (for j/k/h/l/gg/G) - [ ] Tests for api - [ ] Fullscreen diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua index 7a3efb1..293afc5 100644 --- a/lua/null-browser/api.lua +++ b/lua/null-browser/api.lua @@ -29,18 +29,37 @@ end --- Add a keymap --- ---- @param mode string|table Mode or a list of modes ("i", "n") +--- @param mode string Keymap mode ("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 +--- web.view.create('https://google.com') --- end) --- ``` function web.keymap.set(mode, key, action) return __internals.keymap_set(mode, key, action) end +--- Set the current keymap mode +--- +--- @param mode string The keymap mode to set +--- +--- @example +--- ```lua +--- web.keymap.set_mode('n') -- Set to normal mode +--- web.keymap.set_mode('i') -- Set to insert mode +--- ``` +function web.keymap.set_mode(mode) return __internals.keymap_set_mode(mode) end + +--- Get the current keymap mode +--- +--- @example +--- ```lua +--- local current_mode = web.keymap.get_mode() +--- ``` +function web.keymap.get_mode() return __internals.keymap_get_mode() end + --- Close view --- --- @param view_id? number View id to close @@ -106,6 +125,17 @@ function web.view.select(view_id) return __internals.view_select(view_id) end --- ``` function web.view.set_url(url, view_id) return __internals.view_set_url(url, view_id) end +--- Open devtools window for the view +--- +--- @param view_id? number Id of the view +--- +--- @example +--- ```lua +--- web.view.open_devtools() -- Open devtools window for current view +--- web.view.open_devtools(5) -- Open devtools window for view id 5 +--- ``` +function web.view.open_devtools(view_id) return __internals.view_open_devtools(view_id) end + --- Listen to events from the browser --- --- @class nullb.EventListenerOptions @@ -173,36 +203,57 @@ function web.history.back(count, view_id) return __internals.history_back(count, --- ``` function web.history.forward(count, view_id) return __internals.history_forward(count, view_id) end ---- Search +--- Search text inside a view --- --- @param text string Text to search --- @param view_id? number Id of the view --- --- @example --- ```lua ---- web.search.set_text('whatever') +--- web.search.set_text('whatever') -- Search in current view +--- web.search.set_text('whatever', 5) -- Search in view id 5 --- ``` function web.search.set_text(text, view_id) return __internals.search_set_text(text, view_id) end --- TODO: Documentation please +--- Reset searched text in a view (Same as web.search.set_text('')) +--- +--- @param view_id? number Id of the view +--- +--- @example +--- ```lua +--- web.search.reset() -- Reset search state for current view +--- web.search.reset(5) -- Reset search state for view id 5 +--- ``` function web.search.reset(view_id) return __internals.search_set_text('', view_id) end --- TODO: Documentation please +--- Get the last searched text +--- +--- @example +--- ```lua +--- local text = web.search.get_text() +--- ``` function web.search.get_text() return __internals.search_get_text() end --- TODO: Documentation please +--- Highlight next search term for the last searched text +--- +--- @param view_id? number Id of the view +--- +--- @example +--- ```lua +--- web.search.next() -- Next search result in current view +--- web.search.next(5) -- Next search result in view id 5 +--- ``` function web.search.next(view_id) return __internals.search_next(view_id) end --- TODO: Documentation please +--- Highlight previous search term for the last searched text +--- +--- @param view_id? number Id of the view +--- +--- @example +--- ```lua +--- web.search.previous() -- Previous search result in current view +--- web.search.previous(5) -- Previous search result in view id 5 +--- ``` function web.search.previous(view_id) return __internals.search_previous(view_id) end --- TODO: Documentation please -function web.view.open_devtools(view_id) return __internals.view_open_devtools(view_id) end - --- TODO: Documentation please -function web.keymap.set_mode(mode) return __internals.keymap_set_mode(mode) end - --- TODO: Documentation please -function web.keymap.get_mode() return __internals.keymap_get_mode() end - print("api loaded") diff --git a/lua/null-browser/extras/history.lua b/lua/null-browser/extras/history.lua index 4304dc0..f38bfa2 100644 --- a/lua/null-browser/extras/history.lua +++ b/lua/null-browser/extras/history.lua @@ -1,5 +1,5 @@ local history = { - path = '/tmp/.null-browser-history', -- TODO: Use proper path + path = web.get('app_data_dir') .. '/.null-browser-history', max_entires = 200, } diff --git a/src/Configuration.hpp b/src/Configuration.hpp index 3b491b2..1f861fa 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -1,5 +1,6 @@ #pragma once +#include "utils.hpp" #include <QtCore> #include <qwebengineprofile.h> #include <unordered_map> @@ -13,7 +14,9 @@ private: {"close_window_when_no_views", true}, {"user_agent", QWebEngineProfile::defaultProfile()->httpUserAgent()}, {"downloads_dir", QWebEngineProfile::defaultProfile()->downloadPath()}, + {"app_data_dir", QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)}, }; + QDir config_dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)); public: using QObject::QObject; @@ -24,6 +27,9 @@ public: } QVariant get_config(const QString &name, QVariant default_value = "") const { + if (name == "config_dir") // readonly + return config_dir.path(); + if (!config_map.contains(name)) return default_value; @@ -37,6 +43,12 @@ public: QString user_agent() const { return get_config("user_agent").toString(); } QString downloads_dir() const { return get_config("downloads_dir").toString(); } + DEFINE_GETTER(get_config_dir, config_dir) + DEFINE_SETTER(set_config_dir, config_dir) + + QString get_config_lua_init_file() const { return config_dir.filePath("init.lua"); } + QString get_config_lua_dir() const { return config_dir.filePath("lua"); } + private: void on_update(const QString &name, const QVariant &value) { if (name == "user_agent") diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index cb5a69a..b4c39a2 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -15,7 +15,7 @@ LuaRuntime::LuaRuntime() { state = luaL_newstate(); luaL_openlibs(state); - init_lua_package_path(); + init_builtins_package_path(); init_web_api(); } @@ -57,16 +57,9 @@ void LuaRuntime::init_web_api() { }); } -void LuaRuntime::init_lua_package_path() { - auto lua_path = QString(PROJECT_LUA_PATH); - preserve_top(state, { - lua_getglobal(state, "package"); - lua_getfield(state, -1, "path"); - auto pkg_path = QString(lua_tostring(state, -1)) + ";" + lua_path; - lua_pop(state, 1); - lua_pushstring(state, pkg_path.toStdString().c_str()); - lua_setfield(state, -2, "path"); - }); +void LuaRuntime::init_builtins_package_path() { + auto builtins_lua_path = QString(PROJECT_LUA_PATH); + append_package_path(builtins_lua_path); } void LuaRuntime::evaluate(const QString &code) { @@ -84,6 +77,17 @@ void LuaRuntime::evaluate(const QString &code) { }); } +void LuaRuntime::append_package_path(const QString &path) { + preserve_top(state, { + lua_getglobal(state, "package"); + lua_getfield(state, -1, "path"); + auto pkg_path = QString(lua_tostring(state, -1)) + ";" + path; + lua_pop(state, 1); + lua_pushstring(state, pkg_path.toStdString().c_str()); + lua_setfield(state, -2, "path"); + }); +} + void LuaRuntime::load_file_sync(const QString &path) { preserve_top(state, { if (luaL_dofile(state, path.toStdString().c_str()) != LUA_OK) { diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp index d7ec1ae..abefd86 100644 --- a/src/LuaRuntime.hpp +++ b/src/LuaRuntime.hpp @@ -34,6 +34,7 @@ public: void evaluate(const QString &code); void load_file_sync(const QString &path); + void append_package_path(const QString &path); void stop_event_loop(); void start_event_loop(); @@ -61,7 +62,7 @@ signals: protected: LuaRuntime(); ~LuaRuntime() override; - void init_lua_package_path(); + void init_builtins_package_path(); void init_web_api(); private: diff --git a/src/main.cpp b/src/main.cpp index dc51342..dc67c8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ QCommandLineParser *create_cli_parser() { parser->addPositionalArgument("url", "URL(s) to open", "[url]"); parser->addOptions({ {{"e", "expr"}, "Lua expression to execute", "lua"}, + {{"c", "config-dir"}, "Config directory for null browser", "dir"}, }); return parser; @@ -27,10 +28,15 @@ int main(int argc, char *argv[]) { auto urls = parser->positionalArguments(); auto lua_expr = parser->value("expr"); + auto config_dir = parser->value("config-dir"); + + Configuration configuration; + if (!config_dir.isEmpty()) + configuration.set_config_dir(config_dir); InstanceManager instance_manager; if (instance_manager.is_server()) { - auto *browser = new BrowserApp; + auto *browser = new BrowserApp(configuration); browser->create_window(urls); auto &lua = LuaRuntime::instance(); diff --git a/src/widgets/BrowserApp.cpp b/src/widgets/BrowserApp.cpp index 502d930..1362941 100644 --- a/src/widgets/BrowserApp.cpp +++ b/src/widgets/BrowserApp.cpp @@ -1,3 +1,4 @@ +#include <QDir> #include <QKeyEvent> #include <QtCore> @@ -6,7 +7,7 @@ #include "widgets/BrowserApp.hpp" #include "widgets/BrowserWindow.hpp" -BrowserApp::BrowserApp() { +BrowserApp::BrowserApp(Configuration &configuration) : configuration(configuration) { auto &lua = LuaRuntime::instance(); lua.start_event_loop(); @@ -17,8 +18,18 @@ BrowserApp::BrowserApp() { // Global event filter qApp->installEventFilter(this); - // NOTE: TMP - lua.load_file_sync("./config.lua"); + qDebug() << "Config dir:" << configuration.get_config_dir().path(); + + // Load lua directory into package path + lua.append_package_path(configuration.get_config_lua_dir()); + + // Load init.lua + auto lua_init_file = configuration.get_config_lua_init_file(); + if (QFile::exists(lua_init_file)) { + lua.load_file_sync(lua_init_file); + } else { + qWarning() << "Unable to find init.lua:" << lua_init_file; + } // Initializes profile for (auto *profile : profiles) { diff --git a/src/widgets/BrowserApp.hpp b/src/widgets/BrowserApp.hpp index 90d1ae7..c659763 100644 --- a/src/widgets/BrowserApp.hpp +++ b/src/widgets/BrowserApp.hpp @@ -1,5 +1,6 @@ #pragma once +#include "Configuration.hpp" #include "widgets/BrowserWindow.hpp" #include <qlist.h> @@ -7,7 +8,7 @@ class BrowserApp : public QObject { Q_OBJECT public: - BrowserApp(); + BrowserApp(Configuration &configuration); BrowserWindow *create_window(const QStringList &urls = {}); @@ -15,7 +16,7 @@ protected: bool eventFilter(QObject *target, QEvent *event) override; private: - Configuration configuration; + Configuration &configuration; QWebEngineProfile default_profile{"default"}; QList<QWebEngineProfile *> profiles{&default_profile}; }; |
