forthebadge forthebadge Gem Version

History of this project

In the past I used separate projects / gems, such as OpenInEditor or OpenInBrowser, to open files via my favourite editor, or to open remote URLs via my favourite browser.

Additionally I also had more code aggregated in roebe/classes/open/.

This slightly confusing situation accrued over the years, from 2010 to 2021 or so.

In early September 2021 I reconsidered the old approach.

I wanted to not only clean up the old code base, making it more flexible as well in the process, but add a new API such as:

Open.in_editor('/home/foobar.md')
Open.in_editor(this_file: '/home/foobar.md')

As well as:

Open.in_browser(url: 'google.de', browser: 'firefox')

That way we can 'think' of "Open" first, and then simply call the method name that we want to use. On the commandline we can still do:

open_in_editor foobar.rb
open_in_browser google.de

Layout of the project's structure

This subsection will not include all details, but may serve as a slight introduction to how the project is layout.

Most of the constants will reside under open/constants/constants.rb.

Toplevel-methods can be found under open/toplevel_code/ for the most part. This may be ignored by some classes if they add toplevel-methods; in that case the toplevel may be modified by such a class, and can subsequently be found in that .rb file where the class is defined (or the directory, if this class is split up into several different .rb files).

So, for instance, class Open::Browser may define a toplevel method called Open.in_browser(). You can also grep (on Linux) for where the source code resides specifically, of course.

Optional dependencies in this project

Officially there are no hardcoded dependencies in this project.

However had, you are encouraged to install various gems, such as:

  • colours
  • convert_global_env
  • beautiful_url
  • opn

These may enhance the ruby code used in this project.

I highly recommend to install the colours gem, but ultimately the code in this project is written deliberately in a manner to make all these add-ons optional, so this is entirely up to you in the end.

class Open::InEditor

This class can be used for opening a (local) file via the editor.

To open a local file via the open-gem, do:

require 'open'

Open.in_editor('FILE_PATH_GOES_IN_HERE')
Open.in_editor('/home/x/programming/ruby/src/open/open.gemspec')

class Open::InBrowser

This class can be used for opening a remote URL via your favourite browser.

Upon requiring the main class, the method called:

open_in_browser()

will be defined as well, to simplify working with the class.

To specify a particular port, use this variant:

Open.open_in_browser(port: 8080)

This is equivalent to this variant here:

PORT_TO_USE = 8080
Open.in_browser(with_localhost: PORT_TO_USE)

So different keywords can be used, however your brain prefers this or that variant.

In February 2022 I discovered the launchy gem. This gem can be used to open a remote URL via your favourite browser too. Since that is a rather neat functionality to have, I decided to integrate this as an optional API into the open gem as well.

Open.open_in_browser(port: 8080, use_launchy: true)
# or this variant
Open.open_in_browser(port: 8080) { :use_launchy }

Note that in order for this to work, e. g. to use :use_launchy such as shown by the last example, you need to have the launchy gem installed, logically.

On windows you can open a remote URL via firefox in the following manner:

start firefox website_here
start firefox blog.fefe.de
start firefox www.howtogeek.com
start firefox -new-tab www.howtogeek.com

In particular the latter variant can be used as-is via cmd.exe. You can also pass the /k flag to spawn a new cmd.exe, on windows. Before knowing that I used the system() approach passing the path to firefox.exe, but that did not work. Via start, though, it does indeed work.

class Open::LastUrl

This small class can be used to specifically open the content of the xorg-buffer via the browser.

class Open::WithDelay

You can open files via a delay, through class Open::WithDelay.

You can specify a delay via:

openwithdelay 3

Or via the method:

.set_use_this_delay()
.set_use_this_delay(3)
.set_use_this_delay(0.5)

Changing to a different default editor and browser

In November 2021 I realized that we need a way to more easily switch between the default main editor and the default main browser. For instance, I abandoned palemoon, so I needed to use firefox quickly.

The old code had hardcoded calls to palemoon, which was not very elegant, and made changes to the code harder.

Thus two new yaml files were added:

use_this_browser.yml
use_this_editor.yml

Now you can modify these yaml files and use a different browser or editor. You can even specify the full path to these browsers and it should work fine.

Finding out the current editor and browser in use

Use the following two methods if you wish to find out which is the current main editor and the current main browser in use.

puts Open.use_which_editor?
puts Open.use_which_browser?

class Open::Base

This subsection contains just a few pointers to class Open::Base.

The method .this_file_was_not_found() can be used to designate that a certain file (the input argument to that method) could not be found.

Contact information

If your creative mind has ideas and specific suggestions to make this gem more useful in general, feel free to drop me an email at any time, via:

shevy@inbox.lt

Before that email I used an email account at Google gmail, but in 2021 I decided to slowly abandon gmail for various reasons. In part this is because the UI annoys me (on non-chrome browser loading takes too long), but also because of Google's attempt to establish mass surveillance via its federated cohorts sniffing (FLoC). I do not know what happened at Google, but enough is enough - there is only so much you can take while supporting greed. When it comes to data mining done by private groups, ultimately the user became the product.

Do keep in mind that responding to emails may take some time, depending on the amount of work I may have at that moment, due to reallife time constraints. I will, however had, read feedback eventually. Patches and code changes are welcome too, of course, as long as they are in the spirit of the project at hand, e. g. fitting to the general theme. For this I may make use of github as a discussion site, but this has a low priority right now.