From df356d42954107688b7484b595ff1e33cd70291c Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Sun, 22 Apr 2018 16:37:42 +0200 Subject: Move TUI to commands to seperate file --- maze_generator.rb | 19 +++++++++---------- maze_solver.rb | 16 +++++++++------- tui.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 tui.rb diff --git a/maze_generator.rb b/maze_generator.rb index 5261f87..5022c89 100644 --- a/maze_generator.rb +++ b/maze_generator.rb @@ -22,31 +22,30 @@ class MazeGenerator @stack = [start_pos] if ENV["DEBUG"] == "visual" - print "\e[?1049h" # Save the state of the terminal - print "\e[2J" # Clear the screen - print "\e[0;0H" # Move the cursor to 0, 0 - print "\n [ \e[1;35mGenerating maze...\e[0m ]\n\n" # Print some nice graphics - print "\e[s" # Save the cursor position + TUI::Screen.save + TUI::Screen.reset + print "\n [ #{TUI::Color.purple}Generating maze...#{TUI::Color.reset} ]\n\n" + TUI::Cursor.save end while !@stack.empty? step() if ENV["DEBUG"] == "visual" - print "\e[u" # Restore the cursor position + TUI::Cursor.restore print @maze.to_s(" ") print "\n\n" - puts " Stack size: #{@stack.length}/#{@stack_threshold}" + puts " Stack size: #{@stack.length}" if @stack.length < @stack_threshold - puts " Current algorithm: Depth-first-search" + puts " Current algorithm: Depth-first search" else - puts " Current algorithm: Breath-first-search" + puts " Current algorithm: Breath-first search" end end end if ENV["DEBUG"] == "visual" - print "\e[?1049l" # Restore the state of the terminal + TUI::Screen.restore end end diff --git a/maze_solver.rb b/maze_solver.rb index e5fd87e..5633515 100644 --- a/maze_solver.rb +++ b/maze_solver.rb @@ -1,5 +1,6 @@ require_relative 'pos' require_relative 'maze' +require_relative 'tui' class MazeSolver attr_reader :maze @@ -15,26 +16,27 @@ class MazeSolver def solve!() if ENV["DEBUG"] == "visual" - print "\e[?1049h" # Save the state of the terminal - print "\e[2J" # Clear the screen - print "\e[0;0H" # Move the cursor to 0, 0 - print "\n [ \e[1;35mSolving maze...\e[0m ]\n\n" # Print some nice graphics - print "\e[s" # Save the cursor position + TUI::Screen.save + TUI::Screen.reset + print "\n [ #{TUI::Color.purple}Solving maze...#{TUI::Color.reset} ]\n\n" + TUI::Cursor.save + end while @stack.last != @end_pos step() if ENV["DEBUG"] == "visual" - print "\e[u" # Restore the cursor position + TUI::Cursor.restore print @maze.to_s(" ", @stack) print "\n\n" puts " Stack size: #{@stack.length}" + puts " Current algorithm: Depth-first search" end end if ENV["DEBUG"] == "visual" - print "\e[?1049l" # Restore the state of the terminal + TUI::Screen.restore end end diff --git a/tui.rb b/tui.rb new file mode 100644 index 0000000..c1ab434 --- /dev/null +++ b/tui.rb @@ -0,0 +1,40 @@ +module TUI + class Screen + def self.save() + print "\e[?1049h" + end + + def self.reset() + print "\e[2J" + print "\e[0;0H" + end + + def self.restore() + print "\e[?1049l" + end + end + + class Cursor + def self.save() + print "\e[s" + end + + def self.restore() + print "\e[u" + end + end + + class Color + def self.red + "\e[0;31;1m" + end + + def self.purple + "\e[1;35m" + end + + def self.reset + "\e[0m" + end + end +end -- cgit v1.1