File tree Expand file tree Collapse file tree 3 files changed +29
-34
lines changed
Expand file tree Collapse file tree 3 files changed +29
-34
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ use crate::config::Config;
3535use crate :: unit_conversion;
3636use 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" ) ]
4040use 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 ( ) ,
Original file line number Diff line number Diff line change 11//! This handles all the different commands that rustcast can perform, such as opening apps,
22//! copying to clipboard, etc.
33use std:: process:: Command ;
4- #[ cfg( any ( target_os = "macos" , target_os = "linux" ) ) ]
4+ #[ cfg( target_os = "macos" ) ]
55use std:: thread;
66
77use 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) => {
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments