Open searches from command bar
This commit is contained in:
Generated
+1
@@ -2209,6 +2209,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"ely_domain",
|
"ely_domain",
|
||||||
"thiserror 2.0.18",
|
"thiserror 2.0.18",
|
||||||
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ rust-version.workspace = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
ely_domain = { path = "../ely_domain" }
|
ely_domain = { path = "../ely_domain" }
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
|
url.workspace = true
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ use ely_domain::{
|
|||||||
BrowserTab, CommandIntent, CommandScope, DomainError, Profile, ProfileId, ProfileKind, Space,
|
BrowserTab, CommandIntent, CommandScope, DomainError, Profile, ProfileId, ProfileKind, Space,
|
||||||
SpaceId, TabId, UrlText,
|
SpaceId, TabId, UrlText,
|
||||||
};
|
};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
use crate::CoreError;
|
use crate::CoreError;
|
||||||
|
|
||||||
|
const DEFAULT_SEARCH_URL: &str = "https://duckduckgo.com/";
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct InitialBrowserConfig {
|
pub struct InitialBrowserConfig {
|
||||||
pub space_name: String,
|
pub space_name: String,
|
||||||
@@ -157,6 +160,11 @@ impl BrowserCore {
|
|||||||
self.open_tab(url.clone());
|
self.open_tab(url.clone());
|
||||||
self.command_query.clear();
|
self.command_query.clear();
|
||||||
}
|
}
|
||||||
|
CommandIntent::Search(query) => {
|
||||||
|
let url = search_url(query)?;
|
||||||
|
self.open_tab(url);
|
||||||
|
self.command_query.clear();
|
||||||
|
}
|
||||||
CommandIntent::ScopedSearch { scope: CommandScope::Tabs, query } => {
|
CommandIntent::ScopedSearch { scope: CommandScope::Tabs, query } => {
|
||||||
if let Some(tab_id) = self.find_tab_match(query) {
|
if let Some(tab_id) = self.find_tab_match(query) {
|
||||||
self.select_tab(&tab_id)?;
|
self.select_tab(&tab_id)?;
|
||||||
@@ -247,6 +255,13 @@ fn tab_matches_query(tab: &BrowserTab, normalized_query: &str) -> bool {
|
|||||||
|| tab.display_url().to_lowercase().contains(normalized_query)
|
|| tab.display_url().to_lowercase().contains(normalized_query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn search_url(query: &str) -> Result<UrlText, CoreError> {
|
||||||
|
let mut url = Url::parse(DEFAULT_SEARCH_URL)
|
||||||
|
.map_err(|_| DomainError::InvalidUrl { value: DEFAULT_SEARCH_URL.to_string() })?;
|
||||||
|
url.query_pairs_mut().append_pair("q", query);
|
||||||
|
UrlText::parse(url.to_string()).map_err(CoreError::from)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
@@ -374,4 +389,18 @@ mod tests {
|
|||||||
assert_eq!(snapshot.command_query, "@tabs absent");
|
assert_eq!(snapshot.command_query, "@tabs absent");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn search_command_opens_default_search_url() -> Result<(), Box<dyn Error>> {
|
||||||
|
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
|
||||||
|
|
||||||
|
core.set_command_query("? rust async book");
|
||||||
|
let intent = core.submit_command()?;
|
||||||
|
|
||||||
|
let active_tab = core.active_tab()?;
|
||||||
|
assert_eq!(intent, Some(CommandIntent::Search("rust async book".to_string())));
|
||||||
|
assert_eq!(active_tab.url().as_str(), "https://duckduckgo.com/?q=rust+async+book");
|
||||||
|
assert_eq!(core.command_query(), "");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user