#!/usr/bin/env ruby # frozen_string_literal: true require "game_of_life" require "thor" class CLI < Thor map %w[-v --version] => :version desc "--version, -v", "Prints the Game of Life version information" def version print "Game of Life version #{GameOfLife::VERSION}" end default_task :start desc "start [OPTIONS]", "Start the Game of Life simulations" class_option "seed", aliases: "-s", type: :numeric, banner: :SEED, desc: "Specify the seed number to use as an initial state (default to random)." class_option "input", aliases: "-i", type: :string, banner: :VALUE, desc: "Specify the path/URL for the file to use as an initial state. (used instead of seed)" class_option "width", type: :numeric, banner: :WIDTH, desc: "Specify the width of generated universe. (default to terminal width)" class_option "height", type: :numeric, banner: :HEIGHT, desc: "Specify the hight of generated universe. (default to terminal height)" class_option "dead-cell", type: :string, banner: :CHAR, default: "\s", desc: "Specify the dead-cell representation" class_option "live-cell", type: :string, banner: :CHAR, default: "\u2588", desc: "Specify the live-cell representation" class_option "delay", aliases: "-d", type: :numeric, banner: "Milli-Seconds", default: 50, desc: "Specify the introduced delay between each generation" def start universe = GameOfLife.generate(GameOfLife.parsed_options(options)) GameOfLife.run(universe) end class << self # Allow exit with status 1 on failure # Ref: https://github.com/erikhuda/thor/issues/244#issue-6116190 def exit_on_failure? true end end end begin CLI.start(ARGV) rescue Interrupt # Print Seed/File used in previous run when user exits with Interrupt (CMD/Ctrl + C) print "\e[?1049l" print "#{GameOfLife::BANNER}\n\e[0m" exit(0) rescue GameOfLife::Error => e print "#{e.message}\n\e[0m" exit(-1) end