From 913412e0c540a0d9f119e5575613162f2095d00c Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sat, 13 Aug 2022 14:48:31 +0100 Subject: [PATCH] Browser+Base: Allow opening multiple URLs at once from command line This lets you run `br example.com wikipedia.org some/local/file.html` in one go and have them all opened as tabs. --- Base/usr/share/man/man1/Browser.md | 7 +++---- Userland/Applications/Browser/main.cpp | 29 +++++++++++++++----------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Base/usr/share/man/man1/Browser.md b/Base/usr/share/man/man1/Browser.md index a4c97f9ca98..f5a6a95c4eb 100644 --- a/Base/usr/share/man/man1/Browser.md +++ b/Base/usr/share/man/man1/Browser.md @@ -7,9 +7,7 @@ ## Synopsis ```**sh -$ Browser -$ Browser [options] -$ Browser [url] +$ Browser [options] [urls] ``` ## Description @@ -23,7 +21,7 @@ $ Browser [url] ## Arguments -* `url`: URL to open +* `urls`: A list of urls to open, one per tab. If none are specified, then the homepage will be opened instead. ## Examples @@ -31,4 +29,5 @@ $ Browser [url] $ Browser $ Browser --help $ Browser https://serenityos.org/ +$ Browser https://serenityos.org/ /res/html/misc/welcome.html github.com/serenityos/serenity ``` diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp index 04a7ea46135..10990f6c4e4 100644 --- a/Userland/Applications/Browser/main.cpp +++ b/Userland/Applications/Browser/main.cpp @@ -1,12 +1,11 @@ /* * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ -#include "AK/IterationDecision.h" -#include "LibCore/FileWatcher.h" -#include +#include #include #include #include @@ -15,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -63,10 +63,10 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::pledge("stdio recvfd sendfd unix cpath rpath wpath proc exec")); - char const* specified_url = nullptr; + Vector specified_urls; Core::ArgsParser args_parser; - args_parser.add_positional_argument(specified_url, "URL to open", "url", Core::ArgsParser::Required::No); + args_parser.add_positional_argument(specified_urls, "URLs to open", "url", Core::ArgsParser::Required::No); args_parser.parse(arguments); auto app = TRY(GUI::Application::try_create(arguments)); @@ -118,14 +118,16 @@ ErrorOr serenity_main(Main::Arguments arguments) } } - URL first_url = Browser::url_from_user_input(Browser::g_home_url); - if (specified_url) { - if (Core::File::exists(specified_url)) { - first_url = URL::create_with_file_protocol(Core::File::real_path_for(specified_url)); - } else { - first_url = Browser::url_from_user_input(specified_url); + auto url_from_argument_string = [](String const& string) -> URL { + if (Core::File::exists(string)) { + return URL::create_with_file_protocol(Core::File::real_path_for(string)); } - } + return Browser::url_from_user_input(string); + }; + + URL first_url = Browser::url_from_user_input(Browser::g_home_url); + if (!specified_urls.is_empty()) + first_url = url_from_argument_string(specified_urls.first()); Browser::CookieJar cookie_jar; auto window = Browser::BrowserWindow::construct(cookie_jar, first_url); @@ -160,6 +162,9 @@ ErrorOr serenity_main(Main::Arguments arguments) } }; + for (size_t i = 1; i < specified_urls.size(); ++i) + window->create_new_tab(url_from_argument_string(specified_urls[i]), false); + window->show(); return app->exec();