crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.32k stars 1.61k forks source link

Automatically launch browser window when running `crystal play`. #7493

Open joshuapinter opened 5 years ago

joshuapinter commented 5 years ago

Preamble

I'm new to Crystal but I'm absolutely loving it as I've been a Ruby and Rails dev/contributor for over a decade.

This is a feature request that I'm willing to write a PR for but as this is my first intro to the Crystal Github community, I wanted to gauge your reaction first before writing a PR.

Feature Request

crystal play is incredible, especially when learning. However, it's a bit of a pain to have to manually open up a browser window and put in the correct address and port where the Crystal Playground is being served.

I would much prefer to just type crystal play and have it launch the server as well as the default browser window to the correct IP address and port. Something as simple as open 127.0.0.1:8080.

If for whatever reason you think that this should not be the default behaviour, we could optionally keep it tucked behind a command line option on play, like -o or --open.

Anyway, thanks for all your work on this impressive and exciting language, and let me know your feelings on this. If you like it, either as the default or as an option, I'll write up a PR against it this weekend.

Thanks!

Versions

$ crystal --version
Crystal 0.27.2 (2019-02-05)

LLVM: 6.0.1
Default target: x86_64-apple-macosx
wooster0 commented 5 years ago

That's a good idea! I think the browser should be launched by default whenever it's possible. Sometimes it won't be possible like on linux for me when I try sensible-browser:

$ sensible-browser
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.

Or xdg-open:

$ xdg-open
-bash: xdg-open: command not found

In that case the tab will have to be manually opened.

Sija commented 5 years ago

I think that would be pretty useful, although:

If for whatever reason you think that this should not be the default behaviour, we could optionally keep it tucked behind a command line option on play, like -o or --open.

I'd use flag for that.

bcardiff commented 5 years ago

I'm not sure what is the most portable way to do that. Also, note that port and binding might change, but the HTTP server API should help to return the actual values used for that.

The changes to make this happen should be at https://github.com/crystal-lang/crystal/blob/master/src/compiler/crystal/command/playground.cr

wooster0 commented 5 years ago

I'd use flag for that.

Or the user is asked if he wants to open it. I would find that more convenient than having to manually add a flag which is a bit similar to manually opening the tab:

$ crystal play
Do you want to open http://127.0.0.1:8080 in your browser?
straight-shoota commented 5 years ago

This seems like a delicate issue, because it needs to be portable and customizable.

crystal play prints the server address as an HTTP URL. Many terminals provide some way of opening such an URL from a commands output. This is already a pretty neat workflow, portable and fully customizable by the user/environment. Adding this directly into crystal play saves opening the link, but likely brings some headache with its implementation.

If you even want to spare that interaction, I'd suggest a simple script which pipes the output from crystal play and opens the URL read from the first output line.

IMO this is not a problem that needs to be solved in the compiler binary.

ysbaddaden commented 5 years ago

It doesn't have to work anywhere. It doesn't even have to be configurable —system preferences are already doing that.

There is usually a documented and standard, way to open a URL in the default browser for each operating system.

If for some reason this fails to execute, then we silently fail and let the user copy/paste the link into their favourite browser.

No need to be fancy.

refi64 commented 5 years ago

FWIW on Linux, this would be xdg-open url-goes-here if you want to keep things simple.

joshuapinter commented 5 years ago

Wow, great response everyone! 👏

I'll start looking into it but my plan of attack is:

  1. By default, open up the default browser to the correct IP address and port, supporting a few of the most popular operating systems (I'm on a Mac and have access to Windows but not Unix). Fail silently if this cannot be done.

  2. Continue discussion about whether it should be default, behind a flag or prompted to the user.

IMO this is not a problem that needs to be solved in the compiler binary.

If the Playground was in another repo, I'd agree! But as the Playground is likely one of the first interactions that a new user (read: potential contributor) has with Crystal, and is something they will likely use frequently as they pickup the language, I think it's important to make it as streamlined as possible and give the best first impressions.

I realize this is small beans compared to what you guys are working on but I think it's a great first PR for a newcomer, and is something I think new users, like myself, would appreciate. 👍

joshuapinter commented 5 years ago

Initial Work in Progress (WIP) Pull Request created here.

Only tested on macOS 10.14 but wanted to get the ball rolling here to get initial feedback, etc.

I've got Ubuntu 18.04 installed on a VM that I'll test against next as well.