forthebadge forthebadge Gem Version

This gem was last updated on the 16.05.2023 (dd.mm.yyyy notation), at 15:26:58 o'clock.

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.

If you want to find out what the currently used browser is, simply issue the following command:

open --browser?

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)

Why may this be necessary? My old editor (called bluefish) has had problems batch-opening many files, so I needed a delay. With a delay in place the problem I saw went away. So this was mostly 'workaround' code.

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 naturally.

Thus two new yaml files were added to the gem:

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.

In July 2022 I decided to extend this functionality.

First, you can now automatically set a new browser via the toplevel method Open.permanently_use_this_browser().

Simply pass the new browser as name to the method, such as:

Open.permanently_use_this_browser('thorium') # if you want to use the thorium browser

The second argument is optional and allows the user to specify another .yml file to use. The default value should be fine for most users, though, in perhaps 99.9% of the use cases.

Note that the save_file gem is required for this functionality.

Note that you can also place a file called use_this_browser.yml in your home directory - the home directory can be queried via Dir.home. This .yml file, should it exist, will be checked first. That way you can simply assign a new default value without having to modify the internal .yml files of this project.

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? # Or the shorter alias: Open.browser?

Both values can be changed for your own use case. For instance, if you would like to change the browser to be used, use either of the following two methods:

Open.set_use_this_browser
Open.use_this_browser=

Currently, as of July 2022, the following browsers have been tested and "integrated":

firefox
palemoon
chromium
thorium

More may be added in the future. Note that "integrated" mostly means tested on my home system. Each of these may use a slightly different way to open a remote URL in a tab, so the syntax has to be adjusted to each browser differently.

class Open::Base

This subsection contains just a few pointers to the class called 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.

Determining which browser is in use

You can use this method to query which browser has been assigned to be used for Open:

Open.use_which_browser?

Opening local files when the symbol : is used

You can use e. g.

open_in_editor :gamebooks

To open all files that belong to gamebooks. This is catered to my own use cases. You may have to change the .yml file to suit your own use cases. I needed this functionality to batch-open a "project", based on individual files, so this functionality was added in March 2023.

Contact information and mandatory 2FA coming up in 2022

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 order to limit the explanation here, allow me to just briefly state that I do not feel as if I want to promote any Google service anymore when the user becomes the product (such as via data collection by upstream services). I feel this is a hugely flawed business model.

Do keep in mind that responding to emails may take some time, depending on the amount of work I may have at that moment.

In 2022 rubygems.org, or rather the corporate overlords who control the rubygems.org infrastructure these days, decided to make 2FA mandatory for every gem owner eventually: see https://blog.rubygems.org/2022/06/13/making-packages-more-secure.html

Mandatory 2FA will eventually be extended to all rubygems.org developers and maintainers. As I can not use 2FA, for reasons I will skip explaining here, this means that my projects will eventually be taken over by shopify (or, correspondingly, whoever effectively controls the rubygems.org ecosystem). At that point, I no longer have any control what is done to my projects since shopify (respectively those controlling the gems ecosystem) took away control here. Not sure at which point ruby became corporate-controlled - that was not the case several years ago.

Ruby also only allows 2FA users to participate on the issue tracker these days:

https://bugs.ruby-lang.org/issues/18800

(Note that this was changed a few months ago, so the last part is no longer valid - it is possible to register again without mandating 2FA. I will retain the above notice for a bit longer, though, as I feel we should not restrict communication via mandatory authentification in general. Fighting spam is a noble goal, but when it also means you lock out real human people then this is definitely NOT good.)