Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! /usr/bin/env ruby
require 'optparse'
require 'readline'
require 'yaml'
defname = 'lonst_session.yml'
Readline.vi_editing_mode
OptionParser.new do |opt|
opt.banner = "#{__FILE__} [option]"
opt.separator ' '
opt.on('-f', '--file=FILENAME', 'Set using data file name') {|name| fname = name }
opt.on('-v', 'Set vim input mode [default]') {|vim| vim and Readline.vi_editing_mode }
opt.on('-e', 'Set emacs input mode') {|emacs| emacs and Readline.emacs_editing_mode }
opt.on('--delete_session', "Delete original session file (#{defname})") do |delete|
if delete && File.exist?(defname)
File.delete(defname)
end
end
opt.on('-h', '--help', 'You\'re looking at it') { puts opt; exit }
opt.parse!
end
fname ||= defname
source = Readline::HISTORY
ask = lambda do |q|
idea = Readline.readline(q, true)
source.pop if source[-1].empty? || source.count(idea) == 2
end
if File.exist?(fname)
File.open(fname) do |f|
YAML.load(f.read).each do |idea|
source << idea
end
end
end
ask['seed: '] while source.size < 2
begin
loop do
idea, flavor = source.to_a.shuffle.take(2)
unless idea == flavor
puts
puts "#{idea} x #{flavor}"
ask['> ']
end
end
rescue Interrupt
File.open(fname, 'wb') do |f|
f.puts YAML.dump(source.to_a)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment