Skip to content

Instantly share code, notes, and snippets.

@schoblaska
Last active November 29, 2019 23:35
Show Gist options
  • Save schoblaska/eb3ef2a60f5ef31d1fc561ce41d0e353 to your computer and use it in GitHub Desktop.
Save schoblaska/eb3ef2a60f5ef31d1fc561ce41d0e353 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "rubygems"
require "fuzzy_match"
def find_match(str, opts = {})
notes = {}
Dir["#{ENV["HOME"]}/Dropbox/notes/**/*"].select { |n| n =~ /\./ }.each do |path|
key = path.split("Dropbox/notes/")[1]
notes[key] = path
end
matches = FuzzyMatch.new(notes.keys, opts).find(str)
if opts[:find_all_with_score]
matches.map do |match|
[notes[match[0]], match[1], match[2]]
end
else
notes[matches]
end
end
if ARGV.empty?
puts "Usage:"
puts "- note [name] | open note in vim"
puts "- note -s [name] | show matching notes"
puts "- note -c [name] | copy full path to clipboard"
puts "- note -p [name] | full path of best match (for use in scripts / pipes)"
puts "- note -t [name] | open note in typo"
elsif ARGV[0] == "-s"
puts
matches = find_match(ARGV[1..-1].join(" "), find_all_with_score: true)
matches.first(5).each do |match|
short = match[0].gsub("#{ENV["HOME"]}/Dropbox/notes/", "")
score = match[1].to_s[0, 5].ljust(5, " ")
puts "#{score} : #{short}"
end
puts
elsif ARGV[0] == "-t"
match = find_match(ARGV[1..-1].join(" "))
`typo "#{match}"`
elsif ARGV[0] == "-c"
match = find_match(ARGV[1..-1].join(" "))
exec("echo \"#{match.gsub(" ", "\\ ")}\" | tr -d '\n' | pbcopy")
elsif ARGV[0] == "-p"
match = find_match(ARGV[1..-1].join(" "))
exec("echo \"#{match.gsub(" ", "\\ ")}\" | tr -d '\n'")
else
match = find_match(ARGV.join(" "))
macvim = "/usr/local/opt/vim@7.4/bin/vim"
vim = File.exists?(macvim) ? macvim : "vim"
exec("#{vim} -c \"ProseMode\" \"#{match}\"")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment