Module: Helper
Overview
雑多なお助けメソッド群
MacOSX 関連は確認してないので動作するか不明
Defined Under Namespace
Classes: AsyncCommand
Constant Summary collapse
- ENTITIES =
{ quot: '"', amp: "&", nbsp: " ", lt: "<", gt: ">", copy: "(c)" }
Instance Method Summary collapse
- #confirm(message) ⇒ Object
-
#convert_to_windows_path(path) ⇒ Object
CYGWINのパスからwindowsのパスへと変換(cygpathを呼び出すだけ).
- #determine_os ⇒ Object
- #open_browser(url) ⇒ Object
- #open_browser_linux(address, error_message) ⇒ Object
- #open_directory(path, confirm_message = nil) ⇒ Object
- #os_cygwin? ⇒ Boolean
- #os_mac? ⇒ Boolean
- #os_windows? ⇒ Boolean
-
#pretreatment_source(src, encoding = Encoding::UTF_8) ⇒ Object
ダウンロードしてきたデータを使いやすいように処理.
- #print_horizontal_rule ⇒ Object
- #replace_filename_special_chars(str, invalid_replace = false) ⇒ Object
-
#restor_entity(str) ⇒ Object
エンティティ復号.
Instance Method Details
#confirm(message) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/helper.rb', line 41 def confirm() confirm_msg = "#{} (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 |
#convert_to_windows_path(path) ⇒ Object
CYGWINのパスからwindowsのパスへと変換(cygpathを呼び出すだけ)
131 132 133 |
# File 'lib/helper.rb', line 131 def convert_to_windows_path(path) `cygpath -aw \"#{path}\"`.strip end |
#determine_os ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/helper.rb', line 28 def determine_os case when os_windows? :windows when os_cygwin? :cygwin when os_mac? :mac else :other end end |
#open_browser(url) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/helper.rb', line 80 def open_browser(url) case determine_os when :windows escaped_url = url.gsub("%", "%^").gsub("&", "^&") # MEMO: start の引数を "" で囲むと動かない `start #{escaped_url}` when :cygwin `cygstart #{url}` when :mac `open "#{url}"` else open_browser_linux(url, "ブラウザが見つかりませんでした") end end |
#open_browser_linux(address, error_message) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/helper.rb', line 56 def open_browser_linux(address, ) %w(xdg-open firefox w3m).each do |browser| system(%!#{browser} "#{address}"!) return if $?.exitstatus != 127 end error end |
#open_directory(path, confirm_message = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/helper.rb', line 64 def open_directory(path, = nil) if return unless confirm() end case determine_os when :windows `explorer "file:///#{path.encode(Encoding::Windows_31J)}"` when :cygwin `cygstart "#{path}"` when :mac `open "#{path}"` else open_browser_linux(path, "フォルダが開けませんでした") end end |
#os_cygwin? ⇒ Boolean
20 21 22 |
# File 'lib/helper.rb', line 20 def os_cygwin? @@os_is_cygwin ||= RUBY_PLATFORM =~ /cygwin/i end |
#os_mac? ⇒ Boolean
24 25 26 |
# File 'lib/helper.rb', line 24 def os_mac? @@os_is_mac ||= RUBY_PLATFORM =~ /darwin/ end |
#os_windows? ⇒ Boolean
16 17 18 |
# File 'lib/helper.rb', line 16 def os_windows? @@os_is_windows ||= RUBY_PLATFORM =~ /mswin(?!ce)|mingw|bccwin/i end |
#pretreatment_source(src, encoding = Encoding::UTF_8) ⇒ Object
ダウンロードしてきたデータを使いやすいように処理
112 113 114 |
# File 'lib/helper.rb', line 112 def pretreatment_source(src, encoding = Encoding::UTF_8) restor_entity(src.force_encoding(encoding)).gsub("\r", "") end |
#print_horizontal_rule ⇒ Object
95 96 97 |
# File 'lib/helper.rb', line 95 def print_horizontal_rule puts "―" * 35 end |
#replace_filename_special_chars(str, invalid_replace = false) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/helper.rb', line 99 def replace_filename_special_chars(str, invalid_replace = false) result = str.tr("/:*?\"<>|.", "/:*?”〈〉|.").gsub("\\", "¥").gsub("\t", "") if invalid_replace org_encoding = result.encoding result = result.encode(Encoding::Windows_31J, invalid: :replace, undef: :replace, replace: "_") .encode(org_encoding) end result end |
#restor_entity(str) ⇒ Object
エンティティ復号
120 121 122 123 124 125 126 |
# File 'lib/helper.rb', line 120 def restor_entity(str) result = str.encode("UTF-16BE", "UTF-8", :invalid => :replace, :undef => :replace, :replace => "?").encode("UTF-8") ENTITIES.each do |key, value| result.gsub!("&#{key};", value) end result end |