lib/boom/platform.rb in boom-0.2.0 vs lib/boom/platform.rb in boom-0.2.1

- old
+ new

@@ -8,47 +8,96 @@ # clipboard, pasteboard, or whatever they decide to call it. # module Boom class Platform class << self - # Public: tests if currently running on darwin. # # Returns true if running on darwin (MacOS X), else false def darwin? !!(RUBY_PLATFORM =~ /darwin/) end + # Public: tests if currently running on windows. + # + # Apparently Windows RUBY_PLATFORM can be 'win32' or 'mingw32' + # + # Returns true if running on windows (win32/mingw32), else false + def windows? + !!(RUBY_PLATFORM =~ /win|mingw/) + end + # Public: returns the command used to open a file or URL # for the current platform. # # Currently only supports MacOS X and Linux with `xdg-open`. # # Returns a String with the bin def open_command - darwin? ? 'open' : 'xdg-open' + if darwin? + 'open' + elsif windows? + 'start' + else + 'xdg-open' + end end # Public: opens a given Item's value in the browser. This # method is designed to handle multiple platforms. # - # Returns a String explaining what was done + # Returns a String of the Item value. def open(item) - `#{open_command} '#{item.url.gsub("\'","\\'")}'` + unless windows? + system("#{open_command} '#{item.url.gsub("\'","\\'")}'") + else + system("#{open_command} #{item.url.gsub("\'","\\'")}") + end - "Boom! We just opened #{item.value} for you." + item.value end + # Public: returns the command used to copy a given Item's value to the + # clipboard for the current platform. + # + # Returns a String with the bin + def copy_command + if darwin? + 'pbcopy' + elsif windows? + 'clip' + else + 'xclip -selection clipboard' + end + end + # Public: copies a given Item's value to the clipboard. This method is # designed to handle multiple platforms. # - # Returns a String explaining what was done + # Returns the String value of the Item. def copy(item) - copy_command = darwin? ? "pbcopy" : "xclip -selection clipboard" + unless windows? + system("printf '#{item.value.gsub("\'","\\'")}' | #{copy_command}") + else + system("echo #{item.value.gsub("\'","\\'")} | #{copy_command}") + end - Kernel.system("echo '#{item.value.gsub("\'","\\'")}' | tr -d \"\n\" | #{copy_command}") + item.value + end - "Boom! We just copied #{item.value} to your clipboard." + # Public: opens the JSON file in an editor for you to edit. Uses the + # $EDITOR environment variable, or %EDITOR% on Windows for editing. + # This method is designed to handle multiple platforms. + # + # Returns a String with a helpful message. + def edit(json_file) + unless windows? + system("`echo $EDITOR` #{json_file} &") + else + system("start %EDITOR% #{json_file}") + end + + "Make your edits, and do be sure to save." end end end end