Module: MJ::Path

Defined in:
lib/mj/path.rb

Overview

Utility functions to work with PATH

Class Method Summary collapse

Class Method Details

.open(path) ⇒ Object

Open the given path in a file browser with xdg-open.

Will print an error if xdg-open is not found.

Parameters:

  • Pathname (#to_s)

    to open.



16
17
18
19
20
21
22
23
# File 'lib/mj/path.rb', line 16

def Path.open( path )
    if Path.which( 'xdg-open' ).nil?
        logger.error( 'xdg-open is not available.' )
    end
    if system( 'xdg-open %s' % [ path ] ).nil?
        logger.error( 'xdg-open failed to start' )
    end
end

.which(cmd) ⇒ Object

Find cmd in environment variable $PATH



29
30
31
32
33
34
35
36
37
38
# File 'lib/mj/path.rb', line 29

def Path.which( cmd )
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    }
  end
  return nil
end