2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2020-01-24 10:45:29 -03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-04-23 12:19:17 -03:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
Help: Add support for launching with a section and page, like man
I found it strange that `man` and `Help` did not accept the same command
line arguments since they are so similar. So... now they do. :^)
This means you can now open for example the `tar` man page in Help with
`Help tar`, or `Help 1 tar` if you want to disambiguate between pages in
different sections.
If the result is not found, it falls back to the previous behavior,
treating the input as a search query.
Initially I had this written as two optional positional arguments, but
when told to parse `[optional int] [optional string]`, and then given a
string input, ArgsParser forwards it to the [optional int], which then
fails to parse. Ideally it would pass it to the second, [optional
string] arg instead, but that looks like a fairly big change to make to
ArgsParser's internals, and risk breaking things. Maybe this ugly hack
will be an incentive to fix it. :^)
2021-12-19 12:35:21 -03:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2022-02-28 13:05:35 -03:00
|
|
|
#include "MainWidget.h"
|
2022-04-20 16:35:15 -03:00
|
|
|
#include <AK/URL.h>
|
2020-07-22 12:34:27 -03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-11-23 12:03:01 -03:00
|
|
|
#include <LibCore/System.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Application.h>
|
2022-02-28 13:05:35 -03:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Window.h>
|
2021-11-23 12:03:01 -03:00
|
|
|
#include <LibMain/Main.h>
|
2022-02-28 13:05:35 -03:00
|
|
|
|
|
|
|
|
using namespace Help;
|
2019-09-20 18:47:31 -03:00
|
|
|
|
2022-07-11 16:53:29 -03:00
|
|
|
static String parse_input(StringView input)
|
2022-04-20 16:35:15 -03:00
|
|
|
{
|
|
|
|
|
AK::URL url(input);
|
|
|
|
|
if (url.is_valid())
|
|
|
|
|
return url.basename();
|
|
|
|
|
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 12:03:01 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-09-20 18:47:31 -03:00
|
|
|
{
|
2021-12-29 15:10:12 -03:00
|
|
|
TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
|
2021-11-23 12:03:01 -03:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2020-01-11 22:23:55 -03:00
|
|
|
|
2021-11-23 12:03:01 -03:00
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
|
|
|
|
TRY(Core::System::unveil("/usr/share/man", "r"));
|
2022-07-24 10:28:42 -03:00
|
|
|
TRY(Core::System::unveil("/tmp/user/%uid/portal/filesystemaccess", "rw"));
|
2022-08-07 13:19:42 -03:00
|
|
|
TRY(Core::System::unveil("/tmp/user/%uid/portal/launch", "rw"));
|
2022-07-24 10:12:04 -03:00
|
|
|
TRY(Core::System::unveil("/tmp/user/%uid/portal/webcontent", "rw"));
|
2021-11-23 12:03:01 -03:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-01-21 18:09:12 -03:00
|
|
|
|
2022-04-20 16:35:15 -03:00
|
|
|
String start_page;
|
|
|
|
|
u32 section = 0;
|
2020-07-22 12:34:27 -03:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
Help: Add support for launching with a section and page, like man
I found it strange that `man` and `Help` did not accept the same command
line arguments since they are so similar. So... now they do. :^)
This means you can now open for example the `tar` man page in Help with
`Help tar`, or `Help 1 tar` if you want to disambiguate between pages in
different sections.
If the result is not found, it falls back to the previous behavior,
treating the input as a search query.
Initially I had this written as two optional positional arguments, but
when told to parse `[optional int] [optional string]`, and then given a
string input, ArgsParser forwards it to the [optional int], which then
fails to parse. Ideally it would pass it to the second, [optional
string] arg instead, but that looks like a fairly big change to make to
ArgsParser's internals, and risk breaking things. Maybe this ugly hack
will be an incentive to fix it. :^)
2021-12-19 12:35:21 -03:00
|
|
|
// FIXME: These custom Args are a hack. What we want to do is have an optional int arg, then an optional string.
|
|
|
|
|
// However, when only a string is provided, it gets forwarded to the int argument since that is first, and
|
|
|
|
|
// parsing fails. This hack instead forwards it to the start_page in that case.
|
|
|
|
|
args_parser.add_positional_argument(Core::ArgsParser::Arg {
|
|
|
|
|
.help_string = "Section of the man page",
|
|
|
|
|
.name = "section",
|
|
|
|
|
.min_values = 0,
|
|
|
|
|
.max_values = 1,
|
2022-07-11 16:53:29 -03:00
|
|
|
.accept_value = [&](char const* input_ptr) {
|
|
|
|
|
StringView input { input_ptr, strlen(input_ptr) };
|
Help: Add support for launching with a section and page, like man
I found it strange that `man` and `Help` did not accept the same command
line arguments since they are so similar. So... now they do. :^)
This means you can now open for example the `tar` man page in Help with
`Help tar`, or `Help 1 tar` if you want to disambiguate between pages in
different sections.
If the result is not found, it falls back to the previous behavior,
treating the input as a search query.
Initially I had this written as two optional positional arguments, but
when told to parse `[optional int] [optional string]`, and then given a
string input, ArgsParser forwards it to the [optional int], which then
fails to parse. Ideally it would pass it to the second, [optional
string] arg instead, but that looks like a fairly big change to make to
ArgsParser's internals, and risk breaking things. Maybe this ugly hack
will be an incentive to fix it. :^)
2021-12-19 12:35:21 -03:00
|
|
|
// If it's a number, use it as the section
|
2022-07-11 16:53:29 -03:00
|
|
|
if (auto number = input.to_int(); number.has_value()) {
|
Help: Add support for launching with a section and page, like man
I found it strange that `man` and `Help` did not accept the same command
line arguments since they are so similar. So... now they do. :^)
This means you can now open for example the `tar` man page in Help with
`Help tar`, or `Help 1 tar` if you want to disambiguate between pages in
different sections.
If the result is not found, it falls back to the previous behavior,
treating the input as a search query.
Initially I had this written as two optional positional arguments, but
when told to parse `[optional int] [optional string]`, and then given a
string input, ArgsParser forwards it to the [optional int], which then
fails to parse. Ideally it would pass it to the second, [optional
string] arg instead, but that looks like a fairly big change to make to
ArgsParser's internals, and risk breaking things. Maybe this ugly hack
will be an incentive to fix it. :^)
2021-12-19 12:35:21 -03:00
|
|
|
section = number.value();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, use it as the start_page
|
2022-04-20 16:35:15 -03:00
|
|
|
start_page = parse_input(input);
|
Help: Add support for launching with a section and page, like man
I found it strange that `man` and `Help` did not accept the same command
line arguments since they are so similar. So... now they do. :^)
This means you can now open for example the `tar` man page in Help with
`Help tar`, or `Help 1 tar` if you want to disambiguate between pages in
different sections.
If the result is not found, it falls back to the previous behavior,
treating the input as a search query.
Initially I had this written as two optional positional arguments, but
when told to parse `[optional int] [optional string]`, and then given a
string input, ArgsParser forwards it to the [optional int], which then
fails to parse. Ideally it would pass it to the second, [optional
string] arg instead, but that looks like a fairly big change to make to
ArgsParser's internals, and risk breaking things. Maybe this ugly hack
will be an incentive to fix it. :^)
2021-12-19 12:35:21 -03:00
|
|
|
return true;
|
|
|
|
|
} });
|
|
|
|
|
args_parser.add_positional_argument(Core::ArgsParser::Arg {
|
|
|
|
|
.help_string = "Help page to open. Either an absolute path to the markdown file, or a search query",
|
|
|
|
|
.name = "page",
|
|
|
|
|
.min_values = 0,
|
|
|
|
|
.max_values = 1,
|
2022-07-11 16:53:29 -03:00
|
|
|
.accept_value = [&](char const* input_ptr) {
|
|
|
|
|
StringView input { input_ptr, strlen(input_ptr) };
|
Help: Add support for launching with a section and page, like man
I found it strange that `man` and `Help` did not accept the same command
line arguments since they are so similar. So... now they do. :^)
This means you can now open for example the `tar` man page in Help with
`Help tar`, or `Help 1 tar` if you want to disambiguate between pages in
different sections.
If the result is not found, it falls back to the previous behavior,
treating the input as a search query.
Initially I had this written as two optional positional arguments, but
when told to parse `[optional int] [optional string]`, and then given a
string input, ArgsParser forwards it to the [optional int], which then
fails to parse. Ideally it would pass it to the second, [optional
string] arg instead, but that looks like a fairly big change to make to
ArgsParser's internals, and risk breaking things. Maybe this ugly hack
will be an incentive to fix it. :^)
2021-12-19 12:35:21 -03:00
|
|
|
// If start_page was already set by our section arg, then it can't be set again
|
2022-04-20 16:35:15 -03:00
|
|
|
if (start_page.is_empty())
|
Help: Add support for launching with a section and page, like man
I found it strange that `man` and `Help` did not accept the same command
line arguments since they are so similar. So... now they do. :^)
This means you can now open for example the `tar` man page in Help with
`Help tar`, or `Help 1 tar` if you want to disambiguate between pages in
different sections.
If the result is not found, it falls back to the previous behavior,
treating the input as a search query.
Initially I had this written as two optional positional arguments, but
when told to parse `[optional int] [optional string]`, and then given a
string input, ArgsParser forwards it to the [optional int], which then
fails to parse. Ideally it would pass it to the second, [optional
string] arg instead, but that looks like a fairly big change to make to
ArgsParser's internals, and risk breaking things. Maybe this ugly hack
will be an incentive to fix it. :^)
2021-12-19 12:35:21 -03:00
|
|
|
return false;
|
2022-04-20 16:35:15 -03:00
|
|
|
start_page = parse_input(input);
|
Help: Add support for launching with a section and page, like man
I found it strange that `man` and `Help` did not accept the same command
line arguments since they are so similar. So... now they do. :^)
This means you can now open for example the `tar` man page in Help with
`Help tar`, or `Help 1 tar` if you want to disambiguate between pages in
different sections.
If the result is not found, it falls back to the previous behavior,
treating the input as a search query.
Initially I had this written as two optional positional arguments, but
when told to parse `[optional int] [optional string]`, and then given a
string input, ArgsParser forwards it to the [optional int], which then
fails to parse. Ideally it would pass it to the second, [optional
string] arg instead, but that looks like a fairly big change to make to
ArgsParser's internals, and risk breaking things. Maybe this ugly hack
will be an incentive to fix it. :^)
2021-12-19 12:35:21 -03:00
|
|
|
return true;
|
|
|
|
|
} });
|
2021-11-23 12:03:01 -03:00
|
|
|
args_parser.parse(arguments);
|
2020-07-22 12:34:27 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-help"sv);
|
2020-07-07 08:04:06 -03:00
|
|
|
|
2021-11-23 12:03:01 -03:00
|
|
|
auto window = TRY(GUI::Window::try_create());
|
2020-07-07 08:04:06 -03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2019-09-20 18:47:31 -03:00
|
|
|
window->set_title("Help");
|
2020-07-31 19:12:11 -03:00
|
|
|
window->resize(570, 500);
|
2019-09-20 18:47:31 -03:00
|
|
|
|
2022-02-28 13:05:35 -03:00
|
|
|
auto main_widget = TRY(window->try_set_main_widget<MainWidget>());
|
|
|
|
|
TRY(main_widget->initialize_fallibles(window));
|
|
|
|
|
main_widget->set_start_page(start_page, section);
|
2021-04-23 12:19:17 -03:00
|
|
|
|
2019-09-20 18:47:31 -03:00
|
|
|
window->show();
|
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
return app->exec();
|
2019-09-20 18:47:31 -03:00
|
|
|
}
|