class AsciiParadise::Animation
Constants
- RUN_N_TIMES
#¶ ↑
RUN_N_TIMES
¶ ↑This can be overruled/set via the commandline. It just is a general default in that every animated ASCII component will be run 10 times, aka 10 iterations, by default.
#¶ ↑
Public Class Methods
Public Instance Methods
#¶ ↑
display¶ ↑
This method can display a frame of an animated ASCII class.
It is essentially just an output-method. The method will also optionally allow the user to use a specific colour. This colour must exist as part of the Colours namespace.
#¶ ↑
# File lib/ascii_paradise/animation/display.rb, line 25 def display(frame) i = frame.dup # Must work on a copy. if @use_colours # Only do any of the following if we do happen to use colours. if @use_rainbow_colours i = ::Colours::RainbowColours.returnln_plain(i) elsif @use_this_colour and ::Colours.respond_to?(@use_this_colour.to_sym) i = ::Colours.send(@use_this_colour, i) end end e i # Finally display the frame here. end
#¶ ↑
display_the_frame
¶ ↑
This method will display the individual “frame” of an animated ASCII component.
#¶ ↑
# File lib/ascii_paradise/animation/display.rb, line 44 def display_the_frame( frame = @frame ) # ======================================================================= # # Since since as of January 2020 we will also show the name of the # file at hand. # ======================================================================= # if debug? e steelblue('Filename: ')+sfile(guess_filename) end # ======================================================================= # # Display the particular frame at hand next: # ======================================================================= # display frame # ======================================================================= # # === If the user wants to see red numbers, we show them next # # The idea behind these red numbers is to make it easier to show # which frame may be mis-aligned and so forth. # ======================================================================= # if @wait_for_keypress_on_each_frame == :also_show_red_numbers e 1.upto(9) {|number| print swarn(number) } 1.upto(9) {|number| print swarn(number) } 1.upto(9) {|number| print swarn(number) } e end # ======================================================================= # # === Show the frame number if we are running in debug mode # ======================================================================= # if debug? e e steelblue('Frame number: ')+ royalblue((@index_position+1).to_s)+ mediumpurple( ' ('+return_filename(@index_position+1)+')' ) e # Display a newline too. end end
#¶ ↑
do_use_disco_inferno
¶ ↑
Enable the disco-inferno-mode, also called “disco-mode”.
The disco mode means that we will show a (one) random colour for every new frame. This is not very pretty for the eyes, but feature is feature after all. :)
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 147 def do_use_disco_inferno @use_disco_inferno = true end
#¶ ↑
do_use_half_colours
(half tag)¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 80 def do_use_half_colours # ======================================================================= # # First we must determine which two random colours we will use. # ======================================================================= # upper_colour = return_random_colour lower_colour = return_random_colour loop { # ===================================================================== # # Tiny "safeguard" to avoid using the same colour. # ===================================================================== # if upper_colour == lower_colour # lower_colour = return_random_colour end break unless upper_colour == lower_colour } # ======================================================================= # # Turn these into "real" colours - aka ANSII escape sequences next. # ======================================================================= # upper_colour = remove_trailing_ansci_escape_code( Colours.send(upper_colour) ) lower_colour = remove_trailing_ansci_escape_code( Colours.send(lower_colour) ) new_dataset = dataset? # Must turn it into an Array first. new_dataset.map! {|string| string.split("\n") } new_dataset.map! {|inner_array| inner_array[0..inner_array.size / 2] = inner_array[0..inner_array.size / 2].map {|entry| upper_colour+entry+rev } inner_array[(inner_array.size / 2)..-1] = inner_array[(inner_array.size / 2)..-1].map {|entry| lower_colour+entry+rev } inner_array } new_dataset.map! {|string| string.join("\n") } set_dataset( new_dataset ) end
#¶ ↑
do_use_rainbow_colours
¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 161 def do_use_rainbow_colours @use_rainbow_colours = true end
#¶ ↑
load_animated_ascii_files_from_this_directory
¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 57 def load_animated_ascii_files_from_this_directory(i) i = return_the_name_to_the_proper_animation_directory_of_this_animated_component(i) set_use_ascii_files_from_this_directory(i) load_ascii_files_and_determine_the_dataset end
#¶ ↑
load_ascii_files
¶ ↑
This will only load (and assign) the ascii files - it will NOT read these files into the dataset of the ASCII component at hand.
For the latter you will have to use set_dataset
() instead.
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 414 def load_ascii_files @ascii_files = sort_files( Dir[ @use_ascii_files_from_this_directory+'*.ascii' ] ) if @ascii_files.any? {|line| line.end_with? 'control.ascii' } # ===================================================================== # # Read in the data from the file control.ascii next. # ===================================================================== # dataset_from_the_file_control_ascii = File.readlines( @ascii_files.select {|inner_line| inner_line.end_with? 'control.ascii' }.first ) # ===================================================================== # # Parse the control.ascii file next. # ===================================================================== # parse_dataset_from_this_control_file(dataset_from_the_file_control_ascii) @ascii_files.reject! {|line| line.end_with? 'control.ascii' } end end
#¶ ↑
parse_dataset_from_this_control_file
¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 508 def parse_dataset_from_this_control_file(i) if i.is_a? Array i.each {|line| # =================================================================== # # === Handle the delay # =================================================================== # if line.include? 'delay' set_delay( line.split(':').last.strip ) end } end end
#¶ ↑
reset¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/reset.rb, line 14 def reset super() # ======================================================================= # # === @dataset # # @dataset will be nil on startup, but will then, lateron, become an # Array if the animated ASCII dataset has been found. # ======================================================================= # @dataset = nil # ======================================================================= # # Set a default delay on startup. # ======================================================================= # set_delay # ======================================================================= # # Set the default colour in use by this class. # ======================================================================= # set_use_this_colour # ======================================================================= # # Determine how many times to run by default. # ======================================================================= # set_run_n_times # ======================================================================= # # === @do_clear # # If the next instance variable is set to true, then we will run # "clear". By default this is the case. # ======================================================================= # @do_clear = true # ======================================================================= # # === @use_rainbow_colours # # Whether to use the rainbow-colours on every individual ASCII frame # of an animated ASCII "picture". # # By default this is not used; the user has to specifically select # that option if wanted. # ======================================================================= # @use_rainbow_colours = false # ======================================================================= # # === @use_disco_inferno # # Disco-inferno mode means to colourize every frame in a new colour. # ======================================================================= # @use_disco_inferno = false # ======================================================================= # # === @use_colour_spray # # If @use_colour_spray is set to true then the frames will show # a new, random colour AFTER every FULL iteration. This is different # to the disco-inferno mode, where colours are changed AFTER # every frame already. # ======================================================================= # @use_colour_spray = false end
#¶ ↑
return_filename
¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 544 def return_filename(use_this_position = 1) _ = "ascii_paradise/animations/"\ "#{File.basename(@use_ascii_files_from_this_directory)}/"\ "#{File.basename(@use_ascii_files_from_this_directory)}.ascii".dup index = _.index('.ascii') _[index, 0] = '_'+use_this_position.to_s.rjust(2, '0') return _ end
#¶ ↑
return_the_name_to_the_proper_animation_directory_of_this_animated_component
¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 48 def return_the_name_to_the_proper_animation_directory_of_this_animated_component(i) animation_directory?+ return_basename_of_this_file_without_the_extension(i)+ '/' end
#¶ ↑
run¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 245 def run run_with_default_dataset end
#¶ ↑
run_with_this_dataset
¶ ↑
This method can be used to “animate” the frames.
Note that this method has some aliases, such as .animate().
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 446 def run_with_this_dataset( dataset_holding_all_the_frames = dataset?, run_n_times = @run_n_times ) @index_position = 0 # ======================================================================= # # We have two options next: # # - either we will wait for a keypress event from the user # - or we will simply sleep for some time before continuing anyway # # ======================================================================= # if @wait_for_keypress_on_each_frame run_with_this_dataset_while_waiting_for_keypress_events( dataset_holding_all_the_frames ) else run_n_times.times {|iteration_number| dataset_holding_all_the_frames.each {|this_frame| # =================================================================== # # Clear the screen, before doing anything else, if @do_clear # is true. # =================================================================== # clear_screen if @do_clear # =================================================================== # # === Handle colour-usage on every iteration next # =================================================================== # if @use_disco_inferno # Invoke like so: earth --disco do_use_random_colour end @frame = this_frame # =================================================================== # # Next display the frame: # =================================================================== # display_the_frame # =================================================================== # # We next sleep for the default delay - but not if we operate # in the keypress-mode. # =================================================================== # sleep_with_the_default_delay @index_position += 1 if @index_position >= dataset_holding_all_the_frames.size @index_position = 0 end } } # ===================================================================== # # The colour-spray mode is different in that the colour will be # changed after every FULL iteration. # ===================================================================== # if @use_colour_spray do_use_random_colour end end end
#¶ ↑
run_with_this_dataset_while_waiting_for_keypress_events
¶ ↑
To invoke this method, try:
volcano --wait-for-keypress
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 296 def run_with_this_dataset_while_waiting_for_keypress_events( dataset_holding_all_the_frames = dataset? ) loop { can_we_exit = false # ===================================================================== # # Clear the screen, before doing anything else, if @do_clear is true. # ===================================================================== # clear_screen if @do_clear # ===================================================================== # # Keep track of which frame we are working on right now in the # dataset holding all the frames. # ===================================================================== # @frame = dataset_holding_all_the_frames[@index_position] display_the_frame # ===================================================================== # # Show to the user how to operate this keypress-interface: # ===================================================================== # show_to_the_user_how_to_operate_the_keypress_interface # ===================================================================== # # Show that this is the last frame, if it is. # ===================================================================== # if @index_position == (dataset_holding_all_the_frames.size - 1) e "\nThis is the last frame." end user_input = STDIN.getch case user_input # case tag # ===================================================================== # # === previous # ===================================================================== # when 'p','previous' e @index_position -= 1 @index_position = 0 if @index_position < 0 # Don't go below 0. # ===================================================================== # # === next # ===================================================================== # when 'n','next' @index_position += 1 if @index_position >= dataset_holding_all_the_frames.size # Don't go above the max frames. @index_position = dataset_holding_all_the_frames.size - 1 end # ===================================================================== # # === quit # ===================================================================== # when 'q' e can_we_exit = true end if can_we_exit break end } end
#¶ ↑
set_dataset
¶ ↑
This method will assign to the dataset of this animated ASCII class.
The dataset is obtained from @ascii_files, and simply reading in the content of these .ascii files.
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 391 def set_dataset( i = @ascii_files.map {|entry| File.read(entry) } ) @dataset = [i].flatten.compact end
#¶ ↑
set_delay
¶ ↑
The default delay is typically set to 0.145.
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 254 def set_delay( i = SLEEP_FOR_N_SECONDS ) case i when :default i = SLEEP_FOR_N_SECONDS when :slightly_faster # This will take the value from SLEEP_FOR_N_SECONDS and deduct a bit # from it, to a 60% increase in speed. i = (SLEEP_FOR_N_SECONDS * 0.6) when :quite_a_bit_faster i = (SLEEP_FOR_N_SECONDS * 0.4) end i = i.to_f if i.is_a? String @delay = i end
#¶ ↑
set_run_n_times
¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 223 def set_run_n_times( i = RUN_N_TIMES ) case i when :default i = RUN_N_TIMES end @run_n_times = i.to_i end
#¶ ↑
show_frame_at_this_position
¶ ↑
This method can be used to show one individual frame at the given position. For example, if you pass in 3 then we will show frame 3.
The reason as to why this method exists is largely because we may sometimes wish to show just an individual frame on the commandline, perhaps if we wish to colourize it.
See these usage examples:
tetris --show-frame=5 | rainbow_colours table_tennis --show-frame=4 | rainbow_colours table_tennis --show-frame=5 | rainbow_colours table_tennis --show-frames=5-8 table_tennis --show-frames=5-8 | rainbowc volcano --show-frames=5-8
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 193 def show_frame_at_this_position(i) dataset_holding_all_frames = dataset? i.delete!('(') if i.include? '(' i.delete!(')') if i.include? ')' if i.include? '-' # If we have a more complex input, enter here. splitted = i.split('-') position = (splitted.first.to_i - 1)..(splitted.last.to_i - 1) # position = position.split('..').map {|entry| Integer(entry) } # ===================================================================== # # Must be turned into a Range object if it is a String entry. # ===================================================================== # else # Else assume a simpler input. position = (i.to_i - 1) # -1 because Arrays in ruby start at 0. if position > dataset_holding_all_frames.size position = (dataset_holding_all_frames.size - 1) end end # ======================================================================= # # At this point, the variable "position" must be proper. # ======================================================================= # these_frames = dataset_holding_all_frames[position] these_frames = [these_frames] unless these_frames.is_a? Array these_frames.each {|this_frame| e this_frame } end
#¶ ↑
show_to_the_user_how_to_operate_the_keypress_interface
¶ ↑
#¶ ↑
# File lib/ascii_paradise/animation/animation.rb, line 274 def show_to_the_user_how_to_operate_the_keypress_interface e 'Press '+ slategrey('"')+ simp('n')+ slategrey('"')+ ' if you wish to continue and see '\ 'the next frame, '+slategrey('"')+ simp('p')+ slategrey('"')+' for' ee 'the previous frame and '+slategrey('"')+ simp('q')+ slategrey('"')+' if you wish to exit.' end