#! /usr/bin/env ruby # Sortviz is a small terminal program written in Ruby and uses the Curses library # It lets you visualize sorting algorithms # # Author:: Hady Ahmed (mailto:me@hadyahmed.com) # Copyright:: Copyright (c) 2016 # License:: Distributes under MIT require 'optparse' require "sortviz" Options = Struct.new(:algorithm, :speed, :path) args = Options.new(:'bubble-sort', 0.05) @parser = OptionParser.new do |opts| opts.banner = 'Usage: sortviz [options] sorting_algorithm_name' opts.on('-sSPEED', '--sorting-speed=SPEED', OptionParser::DecimalNumeric, 'Set the sorting speed in seconds') do |speed| args.speed = speed end opts.on('-l', '--list', 'List of available sorting algorithms') do puts "Sortviz: List of available algorithms" puts "-------------------------------------" Sortviz::Algorithms.plugins.each do |plugin| print "#{plugin[:display_name]} \n" print "\t Use as: sortviz #{plugin[:name]} \n" print "\t Author: #{plugin[:author]} \n" end exit end opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end opts.on_tail('--version', 'Show version') do puts "Sortviz v#{Sortviz::VERSION}" exit end end def show_help(message = nil) print "#{message} \n\n" unless message.nil? puts @parser exit -1 end if ARGV.empty? show_help else begin @parser.parse!(ARGV) rescue OptionParser::MissingArgument => missing print "Error: #{missing.to_s} \n\n" show_help end algorithm = Sortviz.find_algorithm(ARGV.pop) show_help("Error: Please specify the algorithm name") if algorithm.nil? args.algorithm = algorithm Sortviz.init(args) end