Skip to content

Instantly share code, notes, and snippets.

@no6v
Created April 28, 2011 11:59
Show Gist options
  • Save no6v/946219 to your computer and use it in GitHub Desktop.
Save no6v/946219 to your computer and use it in GitHub Desktop.
module Earthquake::Input
def drafts
@drafts ||= []
end
end
dbfile = File.join(Earthquake.config[:plugin_dir], "draft.yml")
Earthquake.once do
draft = YAML.load(File.open(dbfile, "a+", &:read))
if Array === draft
drafts.replace(draft)
end
end
Earthquake.init do
d = drafts
subcommands = {
apply: ->(index){Readline::HISTORY.push(d.at(Integer(index)))},
clear: -> {d.clear if confirm("Are you sure?", :n)},
delete:->(index){d.delete_at(Integer(index))},
exec: ->(index){input(d.delete_at(Integer(index)))},
list: -> {ap d},
save: ->(text) {d << text.lstrip},
}.with_indifferent_access
command %r|^:draft (#{Regexp.union(subcommands.keys)})(\s+.*)?$|o, :as => :draft do |m|
subcommand, arg = m.captures
subcommands[subcommand][*[arg].compact]
File.open(dbfile, "w") do |db|
YAML.dump(d, db)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment