This commit is contained in:
Saksham Goyal 2024-11-20 20:01:46 -05:00 committed by GitHub
commit 2f2194cee3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 53 additions and 15 deletions

View file

@ -11,19 +11,19 @@
namespace WebView { namespace WebView {
static constexpr auto builtin_search_engines = Array { static constexpr auto builtin_search_engines = Array {
SearchEngine { "Bing"sv, "https://www.bing.com/search?q={}"sv }, SearchEngine { "Bing"sv, "https://www.bing.com/search?q={}"sv, "!b"sv },
SearchEngine { "Brave"sv, "https://search.brave.com/search?q={}"sv }, SearchEngine { "Brave"sv, "https://search.brave.com/search?q={}"sv, "!brave"sv },
SearchEngine { "DuckDuckGo"sv, "https://duckduckgo.com/?q={}"sv }, SearchEngine { "DuckDuckGo"sv, "https://duckduckgo.com/?q={}"sv, "!ddg"sv },
SearchEngine { "Ecosia"sv, "https://ecosia.org/search?q={}"sv }, SearchEngine { "Ecosia"sv, "https://ecosia.org/search?q={}"sv, "!ec"sv },
SearchEngine { "GitHub"sv, "https://github.com/search?q={}"sv }, SearchEngine { "GitHub"sv, "https://github.com/search?q={}"sv, "!gh"sv },
SearchEngine { "Google"sv, "https://www.google.com/search?q={}"sv }, SearchEngine { "Google"sv, "https://www.google.com/search?q={}"sv, "!g"sv },
SearchEngine { "GoogleScholar"sv, "https://scholar.google.com/scholar?q={}"sv }, SearchEngine { "GoogleScholar"sv, "https://scholar.google.com/scholar?q={}"sv, "!gscholar"sv },
SearchEngine { "Kagi"sv, "https://kagi.com/search?q={}"sv }, SearchEngine { "Kagi"sv, "https://kagi.com/search?q={}"sv, "!kagi"sv },
SearchEngine { "Mojeek"sv, "https://www.mojeek.com/search?q={}"sv }, SearchEngine { "Mojeek"sv, "https://www.mojeek.com/search?q={}"sv, "!mojeek"sv },
SearchEngine { "Startpage"sv, "https://startpage.com/search?q={}"sv }, SearchEngine { "Startpage"sv, "https://startpage.com/search?q={}"sv, "!startpage"sv },
SearchEngine { "Wikipedia"sv, "https://en.wikipedia.org/w/index.php?title=Special:Search&search={}"sv }, SearchEngine { "Wikipedia"sv, "https://en.wikipedia.org/w/index.php?title=Special:Search&search={}"sv, "!w"sv },
SearchEngine { "Yahoo"sv, "https://search.yahoo.com/search?p={}"sv }, SearchEngine { "Yahoo"sv, "https://search.yahoo.com/search?p={}"sv, "!y"sv },
SearchEngine { "Yandex"sv, "https://yandex.com/search/?text={}"sv }, SearchEngine { "Yandex"sv, "https://yandex.com/search/?text={}"sv, "!ya"sv },
}; };
ReadonlySpan<SearchEngine> search_engines() ReadonlySpan<SearchEngine> search_engines()
@ -65,6 +65,19 @@ Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_u
return *it; return *it;
} }
Optional<SearchEngine const&> find_search_engine_by_bang(StringView bang)
{
auto it = AK::find_if(builtin_search_engines.begin(), builtin_search_engines.end(),
[&](auto const& engine) {
return engine.bang == bang;
});
if (it == builtin_search_engines.end())
return {};
return *it;
}
String format_search_query_for_display(StringView query_url, StringView query) String format_search_query_for_display(StringView query_url, StringView query)
{ {
static constexpr auto MAX_SEARCH_STRING_LENGTH = 32; static constexpr auto MAX_SEARCH_STRING_LENGTH = 32;

View file

@ -14,12 +14,14 @@ namespace WebView {
struct SearchEngine { struct SearchEngine {
StringView name; StringView name;
StringView query_url; StringView query_url;
StringView bang;
}; };
ReadonlySpan<SearchEngine> search_engines(); ReadonlySpan<SearchEngine> search_engines();
SearchEngine const& default_search_engine(); SearchEngine const& default_search_engine();
Optional<SearchEngine const&> find_search_engine_by_name(StringView name); Optional<SearchEngine const&> find_search_engine_by_name(StringView name);
Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_url); Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_url);
Optional<SearchEngine const&> find_search_engine_by_bang(StringView bang);
String format_search_query_for_display(StringView query_url, StringView query); String format_search_query_for_display(StringView query_url, StringView query);
} }

View file

@ -37,11 +37,34 @@ LocationEdit::LocationEdit(QWidget* parent)
clearFocus(); clearFocus();
auto query = ak_string_from_qstring(text());
auto const bang_char = '!'; // should this be a setting?
Optional<StringView> search_engine_url; Optional<StringView> search_engine_url;
if (Settings::the()->enable_search()) if (Settings::the()->enable_search()) {
search_engine_url = Settings::the()->search_engine().query_url; search_engine_url = Settings::the()->search_engine().query_url;
auto query = ak_string_from_qstring(text()); auto const splits = query.split(' ');
if (!splits.is_error()) {
auto const first = splits.value().first();
if (first.starts_with(bang_char)) {
auto exist = WebView::find_search_engine_by_bang(first);
if (exist.has_value()) {
search_engine_url = exist->query_url;
query = MUST(query.substring_from_byte_offset(first.bytes().size() + 1));
}
}
auto const last = splits.value().last();
if (last.starts_with(bang_char)) {
auto exist = WebView::find_search_engine_by_bang(last);
if (exist.has_value()) {
search_engine_url = exist->query_url;
query = MUST(query.substring_from_byte_offset(0, query.bytes().size() - last.bytes().size()));
}
}
}
}
if (auto url = WebView::sanitize_url(query, search_engine_url); url.has_value()) if (auto url = WebView::sanitize_url(query, search_engine_url); url.has_value())
set_url(url.release_value()); set_url(url.release_value());