Class: Bunch
Overview
Main Bunch CLI Class
Instance Attribute Summary collapse
-
#fragment ⇒ Object
writeonly
Sets the attribute fragment.
-
#show_url ⇒ Object
writeonly
Sets the attribute show_url.
- #url_method ⇒ Object
-
#variables ⇒ Object
writeonly
Sets the attribute variables.
Instance Method Summary collapse
- #bunch_dir ⇒ Object
- #bunch_list ⇒ Object
- #bunches ⇒ Object
- #find_bunch(str) ⇒ Object
-
#generate_bunch_list ⇒ Object
items.push(0).
- #get_cache ⇒ Object
- #human_action ⇒ Object
-
#initialize ⇒ Bunch
constructor
A new instance of Bunch.
- #launch_if_needed ⇒ Object
- #list_bunches ⇒ Object
- #list_preferences ⇒ Object
- #open(str) ⇒ Object
- #show(str) ⇒ Object
- #show_config(key = nil) ⇒ Object
- #update_cache ⇒ Object
- #url(bunch) ⇒ Object
- #variable_query ⇒ Object
Methods included from Util
Constructor Details
#initialize ⇒ Bunch
Returns a new instance of Bunch.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/bunch/bunchCLI.rb', line 8 def initialize @bunch_dir = nil @url_method = nil @bunches = nil @fragment = nil @variables = nil @success = nil @show_url = false get_cache end |
Instance Attribute Details
#fragment=(value) ⇒ Object (writeonly)
Sets the attribute fragment
6 7 8 |
# File 'lib/bunch/bunchCLI.rb', line 6 def fragment=(value) @fragment = value end |
#show_url=(value) ⇒ Object (writeonly)
Sets the attribute show_url
6 7 8 |
# File 'lib/bunch/bunchCLI.rb', line 6 def show_url=(value) @show_url = value end |
#url_method ⇒ Object
92 93 94 95 |
# File 'lib/bunch/bunchCLI.rb', line 92 def url_method @url_method ||= `osascript -e 'tell app "#{TARGET_APP}" to get preference "Toggle"'`.strip == '1' ? 'toggle' : 'open' # @url_method ||= `/usr/bin/defaults read #{ENV['HOME']}/Library/Preferences/com.brettterpstra.Bunch.plist toggleBunches`.strip == '1' ? 'toggle' : 'open' end |
#variables=(value) ⇒ Object (writeonly)
Sets the attribute variables
6 7 8 |
# File 'lib/bunch/bunchCLI.rb', line 6 def variables=(value) @variables = value end |
Instance Method Details
#bunch_dir ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/bunch/bunchCLI.rb', line 84 def bunch_dir @bunch_dir ||= begin dir = `osascript -e 'tell app "#{TARGET_APP}" to get preference "Folder"'`.strip # dir = `/usr/bin/defaults read #{ENV['HOME']}/Library/Preferences/com.brettterpstra.Bunch.plist configDir`.strip File.(dir) end end |
#bunch_list ⇒ Object
118 119 120 121 122 |
# File 'lib/bunch/bunchCLI.rb', line 118 def bunch_list list = [] bunches.each { |bunch| list.push(bunch[:title]) } list end |
#bunches ⇒ Object
97 98 99 |
# File 'lib/bunch/bunchCLI.rb', line 97 def bunches @bunches ||= generate_bunch_list end |
#find_bunch(str) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/bunch/bunchCLI.rb', line 128 def find_bunch(str) matches = [] bunches.each { |bunch| matches.push(bunch) if bunch[:title].downcase =~ /.*?#{str}.*?/i } matches.min_by(&:length) end |
#generate_bunch_list ⇒ Object
items.push(0)
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bunch/bunchCLI.rb', line 72 def generate_bunch_list items = [] `osascript -e 'tell app "#{TARGET_APP}" to list bunches'`.strip.split(/,/).each do |b| b.strip! items.push( path: File.join(bunch_dir, "#{b}.bunch"), title: b ) end items.sort_by { |b| b[:title].downcase } end |
#get_cache ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bunch/bunchCLI.rb', line 45 def get_cache target = File.(CACHE_FILE) if File.exist?(target) settings = YAML.load(IO.read(target)) now = Time.now.strftime('%s').to_i settings = update_cache if now - settings['updated'].to_i > CACHE_TIME else settings = update_cache end @bunch_dir = settings['bunchDir'] || bunch_dir @url_method = settings['method'] || url_method @bunches = settings['bunches'] || generate_bunch_list end |
#human_action ⇒ Object
135 136 137 |
# File 'lib/bunch/bunchCLI.rb', line 135 def human_action "#{url_method.gsub(/e$/, '')}ing".capitalize end |
#launch_if_needed ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/bunch/bunchCLI.rb', line 19 def launch_if_needed pid = `ps ax | grep 'MacOS/Bunch'|grep -v grep`.strip return unless pid == '' `open -a Bunch` sleep 2 end |
#list_bunches ⇒ Object
124 125 126 |
# File 'lib/bunch/bunchCLI.rb', line 124 def list_bunches $stdout.puts bunch_list.join("\n") end |
#list_preferences ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/bunch/bunchCLI.rb', line 139 def list_preferences puts <<~EOHELP toggleBunches=[0,1] Allow Bunches to be both opened and closed configDir=[path] Absolute path to Bunches folder singleBunchMode=[0,1] Close open Bunch when opening new one preserveOpenBunches=[0,1] Restore Open Bunches on Launch debugLevel=[0-4] Set the logging level for the Bunch Log EOHELP end |
#open(str) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/bunch/bunchCLI.rb', line 150 def open(str) launch_if_needed # get front app front_app = %x{osascript -e 'tell application "System Events" to return name of first application process whose frontmost is true'}.strip bid = bundle_id(front_app) || false @success = bid if bid case @url_method when /raw/ warn 'Running raw string' if @show_url $stdout.puts url(str) else `open '#{url(str)}'` end when /snippet/ this_url = url(str) params = [] params << "fragment=#{CGI.escape(@fragment)}" if @fragment params.concat(variable_query) if @variables this_url += "&#{params.join('&')}" if params.length.positive? if @show_url $stdout.puts this_url else warn 'Opening snippet' `open '#{this_url}'` end when /setPref/ if str =~ /^(\w+)=([^= ]+)$/ this_url = url(str) if @show_url $stdout.puts this_url else warn "Setting preference #{str}" `open '#{this_url}'` end else warn 'Invalid key=value pair' Process.exit 1 end else bunch = find_bunch(str) params = [] params << "fragment=#{CGI.escape(@fragment)}" if @fragment params.concat(variable_query) if @variables if bunch this_url = url(bunch[:title]) this_url += "&#{params.join('&')}" if params.length if @show_url $stdout.puts this_url else warn "#{human_action} #{bunch[:title]}" `open '#{this_url}'` end elsif File.exist?(str) @url_method = 'file' this_url = url(str) this_url += "&#{params.join('&')}" if params.length if @show_url $stdout.puts this_url else warn 'Opening file' `open '#{this_url}'` end else warn 'No matching Bunch found' Process.exit 1 end end # attempt to restore front app # %x{osascript -e 'delay 2' -e 'tell application "#{front_app}" to activate'} end |
#show(str) ⇒ Object
223 224 225 226 227 |
# File 'lib/bunch/bunchCLI.rb', line 223 def show(str) bunch = find_bunch(str) output = `cat "#{bunch[:path]}"`.strip puts output end |
#show_config(key = nil) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/bunch/bunchCLI.rb', line 229 def show_config(key = nil) case key when /(folder|dir)/ puts bunch_dir when /toggle/ puts url_method == 'toggle' ? 'true' : 'false' when /method/ puts url_method else puts "Bunches Folder: #{bunch_dir}" puts "Default URL Method: #{url_method}" puts 'Cached Bunches' bunches.each { |b| puts " - #{b[:title]}" } end end |
#update_cache ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bunch/bunchCLI.rb', line 27 def update_cache @bunch_dir = nil @url_method = nil @bunches = nil target = File.(CACHE_FILE) settings = { 'bunchDir' => bunch_dir, 'method' => url_method, 'bunches' => bunches, 'updated' => Time.now.strftime('%s').to_i } File.open(target, 'w') do |f| f.puts YAML.dump(settings) end settings end |
#url(bunch) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/bunch/bunchCLI.rb', line 101 def url(bunch) bunch = CGI.escape(bunch).gsub(/\+/, '%20') params = "&x-success=#{@success}" if @success case url_method when /file/ %(#{TARGET_URL}://raw?file=#{bunch}#{params}) when /raw/ %(#{TARGET_URL}://raw?txt=#{bunch}#{params}) when /snippet/ %(#{TARGET_URL}://snippet?file=#{bunch}#{params}) when /setPref/ %(#{TARGET_URL}://setPref?#{bunch}) else %(#{TARGET_URL}://#{url_method}?bunch=#{bunch}#{params}) end end |
#variable_query ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bunch/bunchCLI.rb', line 59 def variable_query vars = @variables.split(/,/).map(&:strip) query = [] vars.each do |v| parts = v.split(/=/).map(&:strip) k = parts[0] v = parts[1] query << "#{k}=#{CGI.escape(v)}" end query end |