Giter VIP home page Giter VIP logo

wsl_open_browser's Introduction

wsl_open_browser

Inside WSL2 opens a html file in the browser that is in Win10. The local file path is transformed between Linux and Win10 notation.
version: 2021.822.842 date: 2021-08-22 author: bestia.dev repository: GitHub

Lines in Rust code Lines in Doc comments Lines in Comments Lines in examples Lines in tests

crates.io Documentation crev reviews Lib.rs License Rust Hits

Hashtags: #rustlang #tutorial

Try it

Install it from crates.io and add 2 symbolic links and one env variable:

cargo install wsl_open_browser
ln -s "/mnt/c/Program Files/Google/Chrome/Application/chrome.exe" /usr/bin/chrome_in_win
export BROWSER='/usr/bin/wsl_open_browser'
# I like to use the short command `www` to open a browser
ln -s "/usr/bin/wsl_open_browser" www

# go to a directory that contains a subdirectory with index.html
cd ~/rustprojects/wsl_open_browser
www docs/index.html
# or
www docs
# or go inside the directory that contains index.html
cd docs
www .
# or
www
# if you want to open an URL
www google.com

Congratulations! You have just opened a windows browser from WSL2.

Motivation

Inside WSL2 I want to open a html file in the browser. But the browser is in Win10.
This is useful in Rust for the documentation:

cargo doc --open

or for simply open a file in a browser from bash:

www index.html

Open URL in Win10 Browser (first attempt)

I prepared a symbolic link to the chrome.exe.

ln -s "/mnt/c/Program Files/Google/Chrome/Application/chrome.exe" /usr/bin/chrome_in_win
# now I can open a page like this
chrome_in_win http://github.com
# I want to open it with xdg-open
# xdg-open is the "Open any file" for linux
export BROWSER='/usr/bin/chrome_in_win'
# now I can use
xdg-open http://google.com

This works great for URL, but it does not work for local files, because Linux and Windows see the same file with different paths.
The command ln -s is permanent and persistent. It makes a file that stays there forever.
But export BROWSER= is NOT persistent. You need to add this command to ~/.bashrc that runs it on every start of terminal.

Open local file in Win10 Browser

For example:
Linux: /home/luciano/index.html
Win10: \\wsl$\Debian\home\luciano\index.html
OR:
Linux: /mnt/c/Users/Luciano/Downloads\index.html
Win10: c:\Users\Luciano\Downloads\index.html

I need a way to transform the path prior to call the browser.
Let's make a Rust CLI for that.

Development

This is a simple binary. For good habit I separated the lib from the bin.
Use cargo auto to run automation tasks: build, release, doc, copy_to_usr_bin,....
After cargo auto copy_to_usr_bin we can now open the browser like this:

wsl_open_browser http://google.com
wsl_open_browser /home/luciano/index.html
wsl_open_browser /mnt/c/Users/Luciano/Downloads/index.html
wsl_open_browser docs/index.html

Open with xdg-open

But I want to use xdg-open.
xdg-open is the "Open any file" for linux.
For that I need to set the environment variable BROWSER.
To make it persistent, copy this line to ~/.bashrc.

export BROWSER='/usr/bin/wsl_open_browser'

Finally we can open the browser like this:

xdg-open http://google.com
xdg-open /home/luciano/index.html
xdg-open /mnt/c/Users/Luciano/Downloads/index.html

and the Rust documentation works fine:

cargo doc --open

simply: www docs or www .

I am still not satisfied.
I want to open the browser from Debian bash terminal with this simple syntax:

www docs
www .
www google.com

I will make another alias:

ln -s "/usr/bin/wsl_open_browser" www

The command ln -s is permanent and persistent. It makes a file that stays there forever.

wsl_open_browser's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.