Skip to content

Commit 16b24aa

Browse files
author
Rogerluo
authored
support launching browser (#109)
1 parent 3bf1b77 commit 16b24aa

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/server.jl

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
# NOTE: this is copied from Pluto.
2+
"""
3+
open_in_default_browser(url)
4+
5+
Open a URL in system default browser.
6+
"""
7+
function open_in_default_browser(url::AbstractString)::Bool
8+
try
9+
if Sys.isapple()
10+
Base.run(`open $url`)
11+
true
12+
elseif Sys.iswindows() || detectwsl()
13+
Base.run(`cmd.exe /s /c start "" /b $url`)
14+
true
15+
elseif Sys.islinux()
16+
Base.run(`xdg-open $url`)
17+
true
18+
else
19+
false
20+
end
21+
catch ex
22+
false
23+
end
24+
end
25+
126
"""
227
update_and_close_viewers!(wss::Vector{HTTP.WebSockets.WebSocket})
328
@@ -206,7 +231,7 @@ end
206231

207232

208233
"""
209-
serve(filewatcher; host="127.0.0.1", port=8000, dir="", verbose=false, coreloopfun=(c,fw)->nothing, inject_browser_reload_script::Bool = true)
234+
serve(filewatcher; host="127.0.0.1", port=8000, dir="", verbose=false, coreloopfun=(c,fw)->nothing, inject_browser_reload_script::Bool = true, launch_browser::Bool = false)
210235
211236
Main function to start a server at `http://host:port` and render what is in the current
212237
directory. (See also [`example`](@ref) for an example folder).
@@ -216,6 +241,7 @@ directory. (See also [`example`](@ref) for an example folder).
216241
* `dir` specifies where to launch the server if not the current working directory.
217242
* `verbose` is a boolean switch to make the server print information about file changes and connections.
218243
* `coreloopfun` specifies a function which can be run every 0.1 second while the liveserver is going; it takes two arguments: the cycle counter and the filewatcher. By default the coreloop does nothing.
244+
* `launch_browser=false` is a boolean switch to choose launch the browser at host url.
219245
220246
# Example
221247
@@ -231,7 +257,8 @@ page and show the changes.
231257
function serve(fw::FileWatcher=SimpleWatcher(file_changed_callback);
232258
host::String="127.0.0.1", port::Int=8000, dir::AbstractString="", verbose::Bool=false,
233259
coreloopfun::Function=(c,fw)->nothing,
234-
inject_browser_reload_script::Bool = true)
260+
inject_browser_reload_script::Bool = true,
261+
launch_browser::Bool = false)
235262

236263
8000 port 9000 || throw(ArgumentError("The port must be between 8000 and 9000."))
237264
setverbose(verbose)
@@ -247,7 +274,8 @@ function serve(fw::FileWatcher=SimpleWatcher(file_changed_callback);
247274
req_handler = HTTP.RequestHandlerFunction(req -> serve_file(fw, req; inject_browser_reload_script = inject_browser_reload_script))
248275

249276
server = Sockets.listen(parse(IPAddr, host), port)
250-
println("✓ LiveServer listening on http://$(host == string(Sockets.localhost) ? "localhost" : host):$port/ ...\n (use CTRL+C to shut down)")
277+
url = "http://$(host == string(Sockets.localhost) ? "localhost" : host):$port"
278+
println("✓ LiveServer listening on $url/ ...\n (use CTRL+C to shut down)")
251279
@async HTTP.listen(host, port;
252280
server=server, readtimeout=0, reuse_limit=0) do http::HTTP.Stream
253281
# reuse_limit=0 ensures that there won't be an error if killing and restarting the server.
@@ -262,6 +290,7 @@ function serve(fw::FileWatcher=SimpleWatcher(file_changed_callback);
262290
end
263291
end
264292

293+
launch_browser && open_in_default_browser(url)
265294
# wait until user interrupts the LiveServer (using CTRL+C).
266295
try
267296
counter = 1

src/utils.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ assumed that they are in `docs/src`.
122122
* `skip_dirs=[]` same as `skip_dir` but for a vector of such dirs. Takes precedence over `skip_dir`.
123123
* `foldername="docs"` specify the name of the content folder if different than "docs".
124124
* `buildfoldername="build"` specify the name of the build folder if different than "build".
125+
* `launch_browser=false` is a boolean switch to choose launch the browser at host url.
125126
"""
126127
function servedocs(; verbose::Bool=false, literate::String="", doc_env::Bool=false,
127128
skip_dir::String="", skip_dirs::Vector{String}=String[],
128-
foldername::String="docs", buildfoldername::String="build")
129+
foldername::String="docs", buildfoldername::String="build", launch_browser::Bool = false)
129130
# Custom file watcher: it's the standard `SimpleWatcher` but with a custom callback.
130131
docwatcher = SimpleWatcher()
131132

@@ -147,7 +148,7 @@ function servedocs(; verbose::Bool=false, literate::String="", doc_env::Bool=fal
147148

148149
# note the `docs/build` exists here given that if we're here it means the documenter
149150
# pass did not error and therefore that a docs/build has been generated.
150-
serve(docwatcher, dir=joinpath(foldername, buildfoldername), verbose=verbose)
151+
serve(docwatcher, dir=joinpath(foldername, buildfoldername), verbose=verbose, launch_browser=launch_browser)
151152
if doc_env
152153
Pkg.activate()
153154
end

0 commit comments

Comments
 (0)