Skip to content

Commit

Permalink
Add 'open-browser' function. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Nov 8, 2024
1 parent 09002bb commit 9c36fbb
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ jobs:
os: [ ubuntu-latest, windows-latest, macos-latest ]
# The versions should contain (at least) the lowest requirement
# and a version that is more up to date.
toit-version: [ v2.0.0-alpha.120, latest ]
toit-version: [ v2.0.0-alpha.144, latest ]
include:
- toit-version: v2.0.0-alpha.120
- toit-version: v2.0.0-alpha.144
version-name: old
- toit-version: latest
version-name: new
Expand Down
14 changes: 14 additions & 0 deletions examples/EXAMPLES_LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Zero-Clause BSD License

Copyright (C) 2024 Toitware ApS

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
8 changes: 8 additions & 0 deletions examples/browser.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (C) 2024 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the tests/TESTS_LICENSE file.
import desktop

main:
desktop.open-browser "https://toitlang.org"
13 changes: 13 additions & 0 deletions examples/package.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sdk: ^2.0.0-alpha.144
prefixes:
desktop: ..
packages:
..:
path: ..
prefixes:
host: pkg-host
pkg-host:
url: github.com/toitlang/pkg-host
name: host
version: 1.15.3
hash: 62393e8522b77eafbafe60b9817935266117daf6
3 changes: 3 additions & 0 deletions examples/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies:
desktop:
path: ..
6 changes: 3 additions & 3 deletions package.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
sdk: ^2.0.0-alpha.120
sdk: ^2.0.0-alpha.144
prefixes:
host: pkg-host
packages:
pkg-host:
url: github.com/toitlang/pkg-host
name: host
version: 1.11.0
hash: 7e7df6ac70d98a02f232185add81a06cec0d77e8
version: 1.15.3
hash: 62393e8522b77eafbafe60b9817935266117daf6
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ environment:
dependencies:
host:
url: github.com/toitlang/pkg-host
version: ^1.11.0
version: ^1.15.3
64 changes: 64 additions & 0 deletions src/desktop.toit
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the package's LICENSE file.
import host.os
import host.pipe
import system

/**
Expand Down Expand Up @@ -96,3 +97,66 @@ The base directory relative to which user-specific non-essential (cached) data
*/
cache-home -> string?:
return from-env_ "XDG_CACHE_HOME" --fallback=".cache"

/**
Opens the given URL in the default browser.
Typically, opening the browser doesn't take long, so the function will wait for
at most $timeout-ms milliseconds. If the command hasn't returned in that time
it will be killed.
*/
open-browser url/string --timeout-ms/int=20_000:
catch:
command/string? := null
args/List? := null
platform := system.platform
if platform == system.PLATFORM-LINUX:
command = "xdg-open"
args = [ url ]
else if platform == system.PLATFORM-MACOS:
command = "open"
args = [ url ]
else if platform == system.PLATFORM-WINDOWS:
command = "cmd"
escaped-url := url.replace "&" "^&"
args = [ "/c", "start", escaped-url ]
else:
throw "Unsupported platform"

if command != null:
fork-data := pipe.fork
true // Use path.
pipe.PIPE-CREATED // Stdin.
pipe.PIPE-CREATED // Stdout.
pipe.PIPE-CREATED // Stderr.
command
[ command ] + args
stdin := fork-data[0]
stdout/pipe.OpenPipe := fork-data[1]
stderr/pipe.OpenPipe := fork-data[2]
pid := fork-data[3]
stdin.out.close
task --background:: catch: stdout.in.drain
task --background:: catch: stderr.in.drain
task --background::
// The 'open' command should finish in almost no time.
// Even if it doesn't, then the CLI almost always terminates
// shortly after calling 'open'.
// However, if we modify the CLI, so it becomes long-running (for
// example inside a server), we need to make sure we don't keep
// spawned processes around.
exception := catch: with-timeout --ms=timeout-ms:
pipe.wait-for pid
if exception == DEADLINE-EXCEEDED-ERROR:
killed := false
if platform != system.PLATFORM-WINDOWS:
// Try a gentle kill first.
SIGTERM ::= 15
catch:
pipe.kill_ pid SIGTERM
with-timeout --ms=1_000:
pipe.wait-for pid
killed = true
if not killed:
SIGKILL ::= 9
catch: pipe.kill_ pid SIGKILL

0 comments on commit 9c36fbb

Please sign in to comment.