JuliaDocs / LiveServer.jl

Simple development server with live-reload capability for Julia.
MIT License
143 stars 26 forks source link

Add fallback for Linux for `open_in_default_browser` #157

Closed kdheepak closed 1 year ago

kdheepak commented 1 year ago

For linux, in this package there's this line:

https://github.com/tlienart/LiveServer.jl/blob/36a7b208d5fc256283cfc484029a268ff2a5d4a6/src/server.jl#L23-L26

On StackOverflow, a user commented with this:

For linux, this works if the user already has xdg-open, but some people do not have it and need to apt install it.

Desktop.jl does this instead:

function browse_url(url::AbstractString)
    if Sys.iswindows()
        # https://github.com/LOLBAS-Project/LOLBAS/blob/master/Archive-Old-Version/OSLibraries/Url.dll.md
        return success(`rundll32.exe url.dll,OpenURL $url`)
    elseif Sys.isapple()
        # currently requests Safari explicitly, as e.g. Google Chrome
    # (if that's the default browser) fails to open the
    # Julia documentation index.html due to that file
    # commonly being installed with xattr com.apple.quarantine
        # https://github.com/JuliaLang/julia/issues/34275
        return success(`/usr/bin/open -a safari $url`)
    else
        for browser in [
            "/usr/bin/xdg-open",
            "/usr/bin/firefox",
            "/usr/bin/google-chrome",
        ]
            if isfile(browser)
                return success(`$browser $url`)
            end
        end
        @error "Cannot find a web browser to display $url"
    end
end

Specifically for Linux, it checks for other browsers too:

        for browser in [
            "/usr/bin/xdg-open",
            "/usr/bin/firefox",
            "/usr/bin/google-chrome",
        ]
            if isfile(browser)
                return success(`$browser $url`)
            end
        end

What are your thoughts about adding something similar into this package?

rikhuijzer commented 1 year ago

This looks like a not so good solution. xdg-open opens a file or URL in the user's preferred application. The list ["/usr/bin/firefox", "/usr/bin/google-chrome"] does not. Furthermore, binary paths are not very standardized across distributions in Linux. For example, firefox is located at /home/<user>/.nix-profile/bin/ at my system. If fallbacks for xdg-open would be implemented, then it would make more sense to check whether a program exists via which <program> than to hardcode full paths. For what it's worth, Pluto also uses xdg-open and I don't see any issues about it there.

kdheepak commented 1 year ago

Maybe then a warning if xdg-open is not installed? I don't daily drive a Linux machine, and my past experiences have been just ssh-ing into a cluster, so I'm not familiar with any GUI related Linux features.