Skip to content

Commit 9c36fbb

Browse files
authored
Add 'open-browser' function. (#2)
1 parent 09002bb commit 9c36fbb

File tree

8 files changed

+108
-6
lines changed

8 files changed

+108
-6
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ jobs:
2727
os: [ ubuntu-latest, windows-latest, macos-latest ]
2828
# The versions should contain (at least) the lowest requirement
2929
# and a version that is more up to date.
30-
toit-version: [ v2.0.0-alpha.120, latest ]
30+
toit-version: [ v2.0.0-alpha.144, latest ]
3131
include:
32-
- toit-version: v2.0.0-alpha.120
32+
- toit-version: v2.0.0-alpha.144
3333
version-name: old
3434
- toit-version: latest
3535
version-name: new

examples/EXAMPLES_LICENSE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Zero-Clause BSD License
2+
3+
Copyright (C) 2024 Toitware ApS
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
10+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14+
PERFORMANCE OF THIS SOFTWARE.

examples/browser.toit

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (C) 2024 Toitware ApS.
2+
// Use of this source code is governed by a Zero-Clause BSD license that can
3+
// be found in the tests/TESTS_LICENSE file.
4+
5+
import desktop
6+
7+
main:
8+
desktop.open-browser "https://toitlang.org"

examples/package.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sdk: ^2.0.0-alpha.144
2+
prefixes:
3+
desktop: ..
4+
packages:
5+
..:
6+
path: ..
7+
prefixes:
8+
host: pkg-host
9+
pkg-host:
10+
url: github.com/toitlang/pkg-host
11+
name: host
12+
version: 1.15.3
13+
hash: 62393e8522b77eafbafe60b9817935266117daf6

examples/package.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies:
2+
desktop:
3+
path: ..

package.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
sdk: ^2.0.0-alpha.120
1+
sdk: ^2.0.0-alpha.144
22
prefixes:
33
host: pkg-host
44
packages:
55
pkg-host:
66
url: github.com/toitlang/pkg-host
77
name: host
8-
version: 1.11.0
9-
hash: 7e7df6ac70d98a02f232185add81a06cec0d77e8
8+
version: 1.15.3
9+
hash: 62393e8522b77eafbafe60b9817935266117daf6

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ environment:
55
dependencies:
66
host:
77
url: github.com/toitlang/pkg-host
8-
version: ^1.11.0
8+
version: ^1.15.3

src/desktop.toit

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the package's LICENSE file.
44
55
import host.os
6+
import host.pipe
67
import system
78

89
/**
@@ -96,3 +97,66 @@ The base directory relative to which user-specific non-essential (cached) data
9697
*/
9798
cache-home -> string?:
9899
return from-env_ "XDG_CACHE_HOME" --fallback=".cache"
100+
101+
/**
102+
Opens the given URL in the default browser.
103+
104+
Typically, opening the browser doesn't take long, so the function will wait for
105+
at most $timeout-ms milliseconds. If the command hasn't returned in that time
106+
it will be killed.
107+
*/
108+
open-browser url/string --timeout-ms/int=20_000:
109+
catch:
110+
command/string? := null
111+
args/List? := null
112+
platform := system.platform
113+
if platform == system.PLATFORM-LINUX:
114+
command = "xdg-open"
115+
args = [ url ]
116+
else if platform == system.PLATFORM-MACOS:
117+
command = "open"
118+
args = [ url ]
119+
else if platform == system.PLATFORM-WINDOWS:
120+
command = "cmd"
121+
escaped-url := url.replace "&" "^&"
122+
args = [ "/c", "start", escaped-url ]
123+
else:
124+
throw "Unsupported platform"
125+
126+
if command != null:
127+
fork-data := pipe.fork
128+
true // Use path.
129+
pipe.PIPE-CREATED // Stdin.
130+
pipe.PIPE-CREATED // Stdout.
131+
pipe.PIPE-CREATED // Stderr.
132+
command
133+
[ command ] + args
134+
stdin := fork-data[0]
135+
stdout/pipe.OpenPipe := fork-data[1]
136+
stderr/pipe.OpenPipe := fork-data[2]
137+
pid := fork-data[3]
138+
stdin.out.close
139+
task --background:: catch: stdout.in.drain
140+
task --background:: catch: stderr.in.drain
141+
task --background::
142+
// The 'open' command should finish in almost no time.
143+
// Even if it doesn't, then the CLI almost always terminates
144+
// shortly after calling 'open'.
145+
// However, if we modify the CLI, so it becomes long-running (for
146+
// example inside a server), we need to make sure we don't keep
147+
// spawned processes around.
148+
exception := catch: with-timeout --ms=timeout-ms:
149+
pipe.wait-for pid
150+
if exception == DEADLINE-EXCEEDED-ERROR:
151+
killed := false
152+
if platform != system.PLATFORM-WINDOWS:
153+
// Try a gentle kill first.
154+
SIGTERM ::= 15
155+
catch:
156+
pipe.kill_ pid SIGTERM
157+
with-timeout --ms=1_000:
158+
pipe.wait-for pid
159+
killed = true
160+
if not killed:
161+
SIGKILL ::= 9
162+
catch: pipe.kill_ pid SIGKILL

0 commit comments

Comments
 (0)