Module: Helper

Defined in:
lib/helper.rb

Overview

雑多なお助けメソッド群

MacOSX 関連は確認してないので動作するか不明

Defined Under Namespace

Classes: AsyncCommand

Class Method Summary collapse

Class Method Details

.confirm(message) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/helper.rb', line 33

def self.confirm(message)
  confirm_msg = "#{message} (y/n)?: "
  STDOUT.print confirm_msg   # Logger でロギングされないように直接標準出力に表示
  while input = $stdin.gets
    case input[0].downcase
    when "y"
      return true
    when "n"
      return false
    else
      STDOUT.print confirm_msg
    end
  end
end

.determine_osObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/helper.rb', line 22

def self.determine_os
  case
  when os_windows?
    :windows
  when os_mac?
    :mac
  else
    :other
  end
end

.open_browser(url) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/helper.rb', line 70

def self.open_browser(url)
  case determine_os
  when :windows
    escaped_url = url.gsub("%", "%^").gsub("&", "^&")
    # MEMO: start の引数を "" で囲むと動かない
    `start #{escaped_url}`
  when :mac
    `open "#{url}"`
  else
    open_browser_linux(url, "ブラウザが見つかりませんでした")
  end
end

.open_browser_linux(address, error_message) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/helper.rb', line 48

def self.open_browser_linux(address, error_message)
  %w(xdg-open firefox w3m).each do |browser|
    system(%!#{browser} "#{address}"!)
    return if $?.exitstatus != 127
  end
  error error_message
end

.open_directory(path, confirm_message = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/helper.rb', line 56

def self.open_directory(path, confirm_message = nil)
  if confirm_message
    return unless confirm(confirm_message)
  end
  case determine_os
  when :windows
    `explorer "file:///#{path.encode(Encoding::Windows_31J)}"`
  when :mac
    `open "#{path}"`
  else
    open_browser_linux(path, "フォルダが開けませんでした")
  end
end

.os_mac?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/helper.rb', line 18

def self.os_mac?
  @@os_is_mac ||= RUBY_PLATFORM =~ /darwin/
end

.os_windows?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/helper.rb', line 14

def self.os_windows?
  @@os_is_windows ||= RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin|bccwin/i
end


83
84
85
# File 'lib/helper.rb', line 83

def self.print_horizontal_rule
  puts "" * 35
end

.replace_filename_special_chars(str) ⇒ Object



87
88
89
# File 'lib/helper.rb', line 87

def self.replace_filename_special_chars(str)
  str.tr("/:*?\"<>|.", "/:*?”〈〉|.").gsub("\\", "")
end