Skip to content

Instantly share code, notes, and snippets.

@kjaymiller
Forked from ttscoff/README.md
Created May 22, 2019 16:20
Show Gist options
  • Save kjaymiller/db0bbf330becf8399a0561adbf30b1b2 to your computer and use it in GitHub Desktop.
Save kjaymiller/db0bbf330becf8399a0561adbf30b1b2 to your computer and use it in GitHub Desktop.

A CLI for Bunch.app by Brett Terpstra

Save the file below as bunch in your path. Run bunch -h for help.

CLI for Bunch.app
    -h, --help                       Display this screen
    -l, --list                       List available Bunches
    -o, --open                       Open Bunch ignoring "Toggle Bunches" preference
    -c, --close                      Close Bunch ignoring "Toggle Bunches" preference
    -t, --toggle                     Toggle Bunch ignoring "Toggle Bunches" preference
    -s, --show BUNCH                 Show contents of Bunch

Usage: bunch [options] BUNCH_NAME

Bunch names are case insensitive and will execute first match
#!/usr/bin/env ruby
# A CLI for [Bunch.app](https://brettterpstra.com/projects/bunch/) by Brett Terpstra
require 'optparse'
class Bunch
attr_reader :bunches
attr_accessor :url_method
def initialize
@bunch_dir = false
@url_method = false
@bunches = false
end
# items.push({title: 0})
def generate_bunch_list
items = []
Dir.glob(File.join(bunch_dir,'*.bunch')).each {|f|
item = {}
item[:path] = f
item[:title] = File.basename(f,'.bunch')
items.push(item)
}
return items
end
def bunch_dir
unless @bunch_dir
dir = %x{/usr/bin/defaults read com.brettterpstra.Bunch configDir}.strip
@bunch_dir = File.expand_path(dir)
end
@bunch_dir
end
def url_method
unless @url_method
@url_method = %x{/usr/bin/defaults read com.brettterpstra.Bunch toggleBunches}.strip == "1" ? 'toggle' : 'open'
end
@url_method
end
def bunches
unless @bunches
@bunches = generate_bunch_list
end
@bunches
end
def url(bunch)
%Q{x-bunch://#{url_method}?bunch=#{bunch[:title]}}
end
def list_bunches
bunches.each {|bunch|
$stdout.puts bunch[:title]
}
end
def find_bunch(str)
found_bunch = false
bunches.each {|bunch|
if bunch[:title].downcase =~ /.*?#{str}.*?/i
found_bunch = bunch
break
end
}
found_bunch
end
def open(str)
bunch = find_bunch(str)
unless bunch
$stderr.puts "No matching Bunch found"
Process.exit 1
end
$stderr.puts "#{url_method == 'toggle' ? "Toggling" : "Opening"} #{bunch[:title]}"
%x{open "#{url(bunch)}"}
end
def show(str)
bunch = find_bunch(str)
output = %x{cat "#{bunch[:path]}"}.strip
puts output
end
end
bunch = Bunch.new
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "CLI for Bunch.app"
opts.on( '-h', '--help', 'Display this screen' ) do |opt|
puts opts
puts "\nUsage: #{File.basename(__FILE__)} [options] BUNCH_NAME"
puts "\nBunch names are case insensitive and will execute first match"
exit
end
opts.on( '-l', '--list', 'List available Bunches' ) do|opt|
bunch.list_bunches
Process.exit 0
end
opts.on( '-o', '--open', 'Open Bunch ignoring "Toggle Bunches" preference' ) do |opt|
bunch.url_method = 'open'
end
opts.on( '-c', '--close', 'Close Bunch ignoring "Toggle Bunches" preference' ) do |opt|
bunch.url_method = 'close'
end
opts.on( '-t', '--toggle', 'Toggle Bunch ignoring "Toggle Bunches" preference' ) do |opt|
bunch.url_method = 'toggle'
end
opts.on( '-s', '--show BUNCH', 'Show contents of Bunch' ) do |opt|
bunch.show(opt)
Process.exit 0
end
end
optparse.parse!
if ARGV.length > 0
ARGV.each {|arg|
bunch.open(arg)
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment