#!/usr/local/bin/ruby -w # Dan Richert 2006 # dan.richert@gmail.com require 'like_class.rb' #---------startup------ if ARGV.length < 3 puts "failure: wrong arguments. like \n" exit end $num_of_objects = ARGV[0].to_i $lookahead = ARGV[1].to_i $all_texts = Dir.glob("#{ARGV[2]}*") if $all_texts.length == 0 puts "\nfailure: no files in specified directory.\n" exit end if ARGV[3]: $output_file = File.open(ARGV[3], 'w') end #---------functions------ #overlapping printing def inplace(msg) STDOUT.flush() STDOUT.write(msg) STDOUT.write("\010"*msg.length) end def scramble_list(li) new_list = [] li.length.times do ndx = rand(li.length) new_list.push(li[ndx]) li.delete_at(ndx) end return new_list end #----------main---------- loop do $all_texts = scramble_list($all_texts) $all_texts.each do |f| text = File.open(f, 'r').read #seed according to average of bytes in text total = 0 text.each_byte {|b| total += b} avg = total/text.length srand(avg) likes = [] $num_of_objects.times do |i| #start in random positions, 3rd argument is @lookahead likes.push(Like.new(text, rand(text.length), $lookahead)) end likes.each {|obj| obj.others = likes} print "\n"*300 #effectively clear screen ndx = 0 loop do movement_check = 0 threads = [] likes.each do |obj| threads << Thread.new do loop do obj._read inplace(obj.s) end end end threads.each { |th| th.kill } #tallies moving objects likes.each {|obj| if obj.can_move: movement_check += 1 end} if movement_check = likes.length system('clear') final = "#{f}\nFINAL READING:\n\n" likes.each do |obj| final += "#{obj.s}\n" end final += "\n\n" #puts final if $output_file: $output_file.print(final) end #sleep(5) break end if movement_check==0: break end end end end if $output_file: $output_file.close end