Module: Hub::Context::System

Included in:
Hub::Context, Hub::Context
Defined in:
lib/hub/context.rb

Instance Method Summary collapse

Instance Method Details

#browser_launcherObject

Cross-platform web browser command; respects the value set in $BROWSER.

Returns an array, e.g.: [‘open’]



502
503
504
505
506
507
508
509
510
# File 'lib/hub/context.rb', line 502

def browser_launcher
  browser = ENV['BROWSER'] || (
    osx? ? 'open' : windows? ? %w[cmd /c start] :
    %w[xdg-open cygstart x-www-browser firefox opera mozilla netscape].find { |comm| which comm }
  )

  abort "Please set $BROWSER to a web launcher to use this command." unless browser
  Array(browser)
end

#command?(name) ⇒ Boolean

Checks whether a command exists on this system in the $PATH.

name - The String name of the command to check for.

Returns a Boolean.

Returns:

  • (Boolean)


546
547
548
# File 'lib/hub/context.rb', line 546

def command?(name)
  !which(name).nil?
end

#osx?Boolean

Returns:

  • (Boolean)


512
513
514
515
# File 'lib/hub/context.rb', line 512

def osx?
  require 'rbconfig'
  RbConfig::CONFIG['host_os'].to_s.include?('darwin')
end

#terminal_widthObject



554
555
556
557
558
559
560
561
562
# File 'lib/hub/context.rb', line 554

def terminal_width
  if unix?
    width = %x{stty size 2>#{NULL}}.split[1].to_i
    width = %x{tput cols 2>#{NULL}}.to_i if width.zero?
  else
    width = 0
  end
  width < 10 ? 78 : width
end

#tmp_dirObject



550
551
552
# File 'lib/hub/context.rb', line 550

def tmp_dir
  ENV['TMPDIR'] || ENV['TEMP'] || '/tmp'
end

#unix?Boolean

Returns:

  • (Boolean)


522
523
524
525
# File 'lib/hub/context.rb', line 522

def unix?
  require 'rbconfig'
  RbConfig::CONFIG['host_os'] =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
end

#which(cmd) ⇒ Object

Cross-platform way of finding an executable in the $PATH.

which('ruby') #=> /usr/bin/ruby


530
531
532
533
534
535
536
537
538
539
# File 'lib/hub/context.rb', line 530

def 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

#windows?Boolean

Returns:

  • (Boolean)


517
518
519
520
# File 'lib/hub/context.rb', line 517

def windows?
  require 'rbconfig'
  RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw|windows/
end