Skip to content

Commit 5c18d5e

Browse files
committed
tweak url alike checking
1 parent b83124a commit 5c18d5e

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

src/app/tile/update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::config::Config;
3535
use crate::unit_conversion;
3636
use crate::utils::get_installed_apps;
3737

38-
use crate::utils::is_valid_url;
38+
use crate::utils::is_url_like;
3939
#[cfg(target_os = "macos")]
4040
use crate::{
4141
cross_platform::macos::focus_this_app,
@@ -468,7 +468,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
468468
}
469469
})
470470
.collect();
471-
} else if tile.results.is_empty() && is_valid_url(&tile.query) {
471+
} else if tile.results.is_empty() && is_url_like(&tile.query) {
472472
tile.results.push(App {
473473
open_command: AppCommand::Function(Function::OpenWebsite(tile.query.clone())),
474474
desc: "Web Browsing".to_string(),

src/commands.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This handles all the different commands that rustcast can perform, such as opening apps,
22
//! copying to clipboard, etc.
33
use std::process::Command;
4-
#[cfg(any(target_os = "macos", target_os = "linux"))]
4+
#[cfg(target_os = "macos")]
55
use std::thread;
66

77
use arboard::Clipboard;
@@ -59,25 +59,6 @@ impl Function {
5959
let query = query.strip_suffix("?").unwrap_or(&query).to_string();
6060

6161
cross_platform::open_url(&query);
62-
63-
#[cfg(target_os = "macos")]
64-
NSWorkspace::new().openURL(
65-
&NSURL::URLWithString_relativeToURL(
66-
&objc2_foundation::NSString::from_str(&query),
67-
None,
68-
)
69-
.unwrap(),
70-
);
71-
72-
#[cfg(target_os = "linux")]
73-
thread::spawn(move || {
74-
Command::new("xdg-open")
75-
.arg(query)
76-
.spawn()
77-
.unwrap()
78-
.wait()
79-
.unwrap();
80-
});
8162
}
8263

8364
Function::OpenWebsite(url) => {

src/utils.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,30 @@ pub fn get_installed_apps(config: &Config) -> Vec<App> {
218218
}
219219
}
220220

221-
/// Check if the provided string is a valid url
222-
pub fn is_valid_url(s: &str) -> bool {
223-
s.ends_with(".com")
224-
|| s.ends_with(".net")
225-
|| s.ends_with(".org")
226-
|| s.ends_with(".edu")
227-
|| s.ends_with(".gov")
228-
|| s.ends_with(".io")
229-
|| s.ends_with(".co")
230-
|| s.ends_with(".me")
231-
|| s.ends_with(".app")
232-
|| s.ends_with(".dev")
221+
/// Check if the provided string looks like a valid url
222+
pub fn is_url_like(s: &str) -> bool {
223+
if s.starts_with("http://") || s.starts_with("https://") {
224+
return true;
225+
}
226+
if !s.contains('.') {
227+
return false;
228+
}
229+
let mut parts = s.split('.');
230+
231+
let tld = match parts.next_back() {
232+
Some(p) => p,
233+
None => return false,
234+
};
235+
236+
if tld.is_empty() || tld.len() > 63 || !tld.chars().all(|c| c.is_ascii_alphabetic()) {
237+
return false;
238+
}
239+
240+
parts.all(|label| {
241+
!label.is_empty()
242+
&& label.len() <= 63
243+
&& label.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
244+
&& !label.starts_with('-')
245+
&& !label.ends_with('-')
246+
})
233247
}

0 commit comments

Comments
 (0)