summaryrefslogtreecommitdiffstats
path: root/CLI/ruby
diff options
context:
space:
mode:
authora14m <[email protected]>2020-12-10 22:08:18 +0100
committera14m <[email protected]>2020-12-10 22:08:18 +0100
commit26b2e1f4d805d852d455e0c55709a948df83c45d (patch)
treee276934bf3f603e84614bd0cae09936b617c4792 /CLI/ruby
parent114b36f8e9ea06e05bf5ea005599f2c31b0aff1b (diff)
Update the game-of-life binary to check max width/height
And raise an error if the requested width/height greater than the maximum provided by console.winsize (to avoid rendering multi-lines) Since the terminal cannot scroll with bigger width it's better to limit the max configurations to the max allowed without scrolling
Diffstat (limited to 'CLI/ruby')
-rwxr-xr-xCLI/ruby/bin/game-of-life21
1 files changed, 17 insertions, 4 deletions
diff --git a/CLI/ruby/bin/game-of-life b/CLI/ruby/bin/game-of-life
index 0230271..f49682e 100755
--- a/CLI/ruby/bin/game-of-life
+++ b/CLI/ruby/bin/game-of-life
@@ -20,10 +20,10 @@ class CLI < Thor
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", aliases: "-w", type: :numeric, banner: :WIDTH,
+ class_option "width", type: :numeric, banner: :WIDTH,
desc: "Specify the width of generated universe. (default to terminal width)"
- class_option "height", aliases: "-h", type: :numeric, banner: :HEIGHT,
+ 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",
@@ -34,18 +34,31 @@ class CLI < Thor
class_option "delay", aliases: "-d", type: :numeric, banner: "Milli-Seconds", default: 50,
desc: "Specify the introduced delay between each generation"
+ # rubocop:disable Metrics/AbcSize
+ # rubocop:disable Metrics/MethodLength
def start
+ max_height = IO.console.winsize[0] - 2
+ max_width = IO.console.winsize[1]
+ if options["width"]&.> max_width
+ fail GameOfLife::Error, "Invalid --width value, must not exceed current terminal width: #{max_width}"
+ end
+ if options["height"]&.> max_height
+ fail GameOfLife::Error, "Invalid --height value, must not exceed current terminal height: #{max_height}"
+ end
+
options_with_console_defaults = {
# Defaults the hight to less then the height of the terminal to allow banner info
# and an extra emtpy line to avoid triggering terminal scroll while flushing
- "height" => IO.console.winsize[0] - 3,
- "width" => IO.console.winsize[1],
+ "height" => max_height,
+ "width" => max_width,
"seed" => rand(100_000),
}.merge(options)
universe = GameOfLife.generate(options_with_console_defaults)
GameOfLife.run(universe)
end
+ # rubocop:enable Metrics/AbcSize
+ # rubocop:enable Metrics/MethodLength
class << self
# Allow exit with status 1 on failure