Skip to content

Commit

Permalink
feat(utils): add open_url function
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Jan 27, 2025
1 parent 854ab77 commit 0e11661
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ web-sys = { version = "0.3.76", features = [
'KeyboardEvent',
'CanvasRenderingContext2d',
'HtmlCanvasElement',
'Location',
] }
ratatui = { version = "0.29", default-features = false }
console_error_panic_hook = "0.1.7"
Expand Down
13 changes: 13 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ pub fn set_document_title(title: &str) -> Result<(), Error> {
.set_title(title);
Ok(())
}

/// Open a URL in a new tab or the current tab.
pub fn open_url(url: &str, new_tab: bool) -> Result<(), Error> {
let window = web_sys::window().ok_or(Error::UnableToRetrieveWindow)?;
if new_tab {
window.open_with_url(url)?;
} else {
let location = window.location();
location.set_href(url)?;
location.replace(url)?;
}
Ok(())
}

0 comments on commit 0e11661

Please sign in to comment.