mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:38:11 +00:00
LibWebView: Add an API to format a search query for UI display
This will create a string of the form: Search DuckDuckGo for "Ladybird is awesome!" If the provided query URL is unknown, the engine name is excluded (e.g. for custom search URLs).
This commit is contained in:
parent
c8c3d00615
commit
e221b3afeb
2 changed files with 34 additions and 0 deletions
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Find.h>
|
#include <AK/Find.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <LibWebView/SearchEngine.h>
|
#include <LibWebView/SearchEngine.h>
|
||||||
|
|
||||||
namespace WebView {
|
namespace WebView {
|
||||||
|
@ -46,4 +47,35 @@ Optional<SearchEngine const&> find_search_engine_by_name(StringView name)
|
||||||
return *it;
|
return *it;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_url)
|
||||||
|
{
|
||||||
|
auto it = AK::find_if(builtin_search_engines.begin(), builtin_search_engines.end(),
|
||||||
|
[&](auto const& engine) {
|
||||||
|
return engine.query_url == query_url;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (it == builtin_search_engines.end())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return *it;
|
||||||
|
}
|
||||||
|
|
||||||
|
String format_search_query_for_display(StringView query_url, StringView query)
|
||||||
|
{
|
||||||
|
static constexpr auto MAX_SEARCH_STRING_LENGTH = 32;
|
||||||
|
|
||||||
|
if (auto search_engine = find_search_engine_by_query_url(query_url); search_engine.has_value()) {
|
||||||
|
return MUST(String::formatted("Search {} for \"{:.{}}{}\"",
|
||||||
|
search_engine->name,
|
||||||
|
query,
|
||||||
|
MAX_SEARCH_STRING_LENGTH,
|
||||||
|
query.length() > MAX_SEARCH_STRING_LENGTH ? "..."sv : ""sv));
|
||||||
|
}
|
||||||
|
|
||||||
|
return MUST(String::formatted("Search for \"{:.{}}{}\"",
|
||||||
|
query,
|
||||||
|
MAX_SEARCH_STRING_LENGTH,
|
||||||
|
query.length() > MAX_SEARCH_STRING_LENGTH ? "..."sv : ""sv));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,5 +19,7 @@ struct SearchEngine {
|
||||||
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);
|
||||||
|
String format_search_query_for_display(StringView query_url, StringView query);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue