summaryrefslogtreecommitdiffstats
path: root/CLI
diff options
context:
space:
mode:
Diffstat (limited to 'CLI')
-rwxr-xr-xCLI/ruby/bin/game-of-life8
-rw-r--r--CLI/ruby/lib/game_of_life.rb10
-rw-r--r--CLI/ruby/spec/game_of_life_spec.rb4
3 files changed, 14 insertions, 8 deletions
diff --git a/CLI/ruby/bin/game-of-life b/CLI/ruby/bin/game-of-life
index 16dc0fa..08dbeca 100755
--- a/CLI/ruby/bin/game-of-life
+++ b/CLI/ruby/bin/game-of-life
@@ -8,7 +8,7 @@ class CLI < Thor
map %w[-v --version] => :version
desc "--version, -v", "Prints the Game of Life version information"
def version
- puts "Game of Life version #{GameOfLife::VERSION}"
+ print "Game of Life version #{GameOfLife::VERSION}"
end
default_task :start
@@ -50,8 +50,10 @@ end
begin
CLI.start(ARGV)
rescue SystemExit, Interrupt
- "Do nothing when user exits with Interrupt (CMD/Ctrl + C)"
+ # Print Seed/File used in previous run when user exits with Interrupt (CMD/Ctrl + C)
+ print "\e[?1049l"
+ print "#{GameOfLife::BANNER}\n\e[0m"
rescue NotImplementedError, GameOfLife::Error => e
- puts e.message
+ print e.message
exit(-1)
end
diff --git a/CLI/ruby/lib/game_of_life.rb b/CLI/ruby/lib/game_of_life.rb
index 2ff548c..b1a95f1 100644
--- a/CLI/ruby/lib/game_of_life.rb
+++ b/CLI/ruby/lib/game_of_life.rb
@@ -106,9 +106,13 @@ module GameOfLife
# and rendering of the current universe/banner info
# based on the delay/cell configured by the user
def render(universe)
- puts "\33c\e[3J" # Clears the screen and scrollback buffer.
- puts universe.to_s
- puts BANNER
+ # Use Alternate Screen (\e[?1049h) and move cursor to top left (\e[H)
+ # Ref: https://github.com/ruby/ruby/blob/ruby_2_7/lib/irb/easter-egg.rb#L112-L131
+ # Ref: https://stackoverflow.com/questions/11023929/using-the-alternate-screen-in-a-bash-script
+ # Ref: https://superuser.com/questions/122911/what-commands-can-i-use-to-reset-and-clear-my-terminal
+ print "\e[?1049h\e[H"
+ print universe.to_s
+ print BANNER
sleep(DELAY / 1000.0)
end
end
diff --git a/CLI/ruby/spec/game_of_life_spec.rb b/CLI/ruby/spec/game_of_life_spec.rb
index 89a0c87..a0013a6 100644
--- a/CLI/ruby/spec/game_of_life_spec.rb
+++ b/CLI/ruby/spec/game_of_life_spec.rb
@@ -181,8 +181,8 @@ RSpec.describe GameOfLife do
it "renders the game of life presentation" do
stub_const("BANNER", "")
stub_const("DELAY", 100.0)
- expect_any_instance_of(Kernel).to receive(:puts).with("\33c\e[3J")
- expect_any_instance_of(Kernel).to receive(:puts).twice
+ expect_any_instance_of(Kernel).to receive(:print).with("\e[?1049h\e[H")
+ expect_any_instance_of(Kernel).to receive(:print).twice
expect(universe).to receive(:to_s)
expect_any_instance_of(Kernel).to receive(:sleep)