Class: AsciiParadise::Animation

Inherits:
Base
  • Object
show all
Defined in:
lib/ascii_paradise/animation/reset.rb,
lib/ascii_paradise/animation/display.rb,
lib/ascii_paradise/animation/animation.rb

Overview

AsciiParadise::Animation

Constant Summary collapse

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.

#
10

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

animation_dir?, #animation_directory?, #clear_screen, #colour_parse_this_string, #debug?, #do_not_run_already, #do_not_use_clear, #do_use_random_colour, #do_wait_for_keypress_event, #e, e, #enable_debug, #initialize, #menu, #project_base_dir?, #register_sigint, #remove_trailing_ansci_escape_code, #report_how_many_animated_components_exist, #return_basename_of_this_file_without_the_extension, #return_random_colour, #rev, #royalblue, #set_use_this_colour, #sfancy, #sfile, #show_available_components, #show_help, #simp, #slategrey, #sort_files, #static_dir?, #steelblue, #swarn, #use_colours?

Constructor Details

This class inherits a constructor from AsciiParadise::Base

Class Method Details

.is_animated?Boolean

#

AsciiParadise::Animation.is_animated?

This will always be true when people subclass from this class.

#

Returns:

  • (Boolean)


36
37
38
# File 'lib/ascii_paradise/animation/animation.rb', line 36

def self.is_animated?
  true
end

.runObject

#

AsciiParadise::Animation.run

#


558
559
560
# File 'lib/ascii_paradise/animation/animation.rb', line 558

def self.run
  new
end

Instance Method Details

#ascii_files?Boolean

#

ascii_files?

#

Returns:

  • (Boolean)


68
69
70
# File 'lib/ascii_paradise/animation/animation.rb', line 68

def ascii_files?
  @ascii_files
end

#dataset?Boolean

#

dataset?

#

Returns:

  • (Boolean)


372
373
374
# File 'lib/ascii_paradise/animation/animation.rb', line 372

def dataset?
  @dataset
end

#delay?Boolean Also known as: sleep_for_n_seconds?

#

delay?

#

Returns:

  • (Boolean)


75
76
77
# File 'lib/ascii_paradise/animation/animation.rb', line 75

def delay?
  @delay
end

#display(frame) ⇒ Object Also known as: display_this_frame

#

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.

#


25
26
27
28
29
30
31
32
33
34
35
36
# 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(frame = @frame) ⇒ Object

#

display_the_frame

This method will display the individual “frame” of an animated ASCII component.

#


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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 tomato(number) }
    1.upto(9) {|number| print tomato(number) }
    1.upto(9) {|number| print tomato(number) }
    1.upto(9) {|number| print tomato(number) }
    1.upto(9) {|number| print tomato(number) }
    1.upto(9) {|number| print tomato(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_clear?Boolean

#

do_clear?

#

Returns:

  • (Boolean)


170
171
172
# File 'lib/ascii_paradise/animation/animation.rb', line 170

def do_clear?
  @do_clear
end

#do_use_colour_sprayObject

#

do_use_colour_spray

The colour-spray mode means that after every iteration, a new general colour will be shown for all ASCII frames in that iteration run.

#


136
137
138
# File 'lib/ascii_paradise/animation/animation.rb', line 136

def do_use_colour_spray
  @use_colour_spray = true
end

#do_use_disco_infernoObject

#

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. :)

#


149
150
151
# File 'lib/ascii_paradise/animation/animation.rb', line 149

def do_use_disco_inferno
  @use_disco_inferno = true
end

#do_use_half_coloursObject

#

do_use_half_colours (half tag)

#


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ascii_paradise/animation/animation.rb', line 82

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_coloursObject Also known as: do_use_rainbow_lines

#

do_use_rainbow_colours

#


163
164
165
# File 'lib/ascii_paradise/animation/animation.rb', line 163

def do_use_rainbow_colours
  @use_rainbow_colours = true
end

#guess_filenameObject

#

guess_filename

This method will, as its name may imply, attempt to guess the filename.

#


531
532
533
534
# File 'lib/ascii_paradise/animation/animation.rb', line 531

def guess_filename
  "ascii_paradise/animations/"\
  "#{File.basename(@use_ascii_files_from_this_directory)}.rb"
end

#is_animated?Boolean

#

is_animated?

#

Returns:

  • (Boolean)


43
44
45
# File 'lib/ascii_paradise/animation/animation.rb', line 43

def is_animated?
  ::AsciiParadise::Animation.is_animated?
end

#load_animated_ascii_files_from_this_directory(i) ⇒ Object

#

load_animated_ascii_files_from_this_directory

#


59
60
61
62
63
# File 'lib/ascii_paradise/animation/animation.rb', line 59

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_filesObject

#

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.

#


416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/ascii_paradise/animation/animation.rb', line 416

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

#load_ascii_files_and_determine_the_datasetObject

#

load_ascii_files_and_determine_the_dataset

#


402
403
404
405
406
# File 'lib/ascii_paradise/animation/animation.rb', line 402

def load_ascii_files_and_determine_the_dataset
  load_ascii_files
  load_dataset
  set_dataset
end

#load_datasetObject

#

load_dataset

#


379
380
381
382
383
# File 'lib/ascii_paradise/animation/animation.rb', line 379

def load_dataset
  set_dataset(
    @ascii_files.map {|entry| File.read(entry) }
  )
end

#mediumpurple(i = '') ⇒ Object

#

mediumpurple

#


539
540
541
# File 'lib/ascii_paradise/animation/animation.rb', line 539

def mediumpurple(i = '')
  AsciiParadise.mediumpurple(i)
end

#parse_dataset_from_this_control_file(i) ⇒ Object

#

parse_dataset_from_this_control_file

#


510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/ascii_paradise/animation/animation.rb', line 510

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

#resetObject

#

reset

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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(use_this_position = 1) ⇒ Object

#

return_filename

#


546
547
548
549
550
551
552
553
# File 'lib/ascii_paradise/animation/animation.rb', line 546

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(i) ⇒ Object

#

return_the_name_to_the_proper_animation_directory_of_this_animated_component

#


50
51
52
53
54
# File 'lib/ascii_paradise/animation/animation.rb', line 50

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

#runObject

#

run

#


247
248
249
# File 'lib/ascii_paradise/animation/animation.rb', line 247

def run
  run_with_default_dataset
end

#run_n_times?Boolean

#

run_n_times?

How many times we will run the looped-animation.

#

Returns:

  • (Boolean)


240
241
242
# File 'lib/ascii_paradise/animation/animation.rb', line 240

def run_n_times?
  @run_n_times
end

#run_with_this_dataset(dataset_holding_all_the_frames = dataset?, , run_n_times = @run_n_times) ⇒ Object Also known as: run_with_default_dataset, start_the_animation, do_animate, animate

#

run_with_this_dataset

This method can be used to “animate” the frames.

Note that this method has some aliases, such as .animate().

#


448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/ascii_paradise/animation/animation.rb', line 448

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(dataset_holding_all_the_frames = dataset? ) ⇒ Object

#

run_with_this_dataset_while_waiting_for_keypress_events

To invoke this method, try:

volcano --wait-for-keypress
#


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/ascii_paradise/animation/animation.rb', line 298

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(i = @ascii_files.map {|entry| File.read(entry) }) ⇒ Object

#

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.

#


393
394
395
396
397
# File 'lib/ascii_paradise/animation/animation.rb', line 393

def set_dataset(
    i = @ascii_files.map {|entry| File.read(entry) }
  )
  @dataset = [i].flatten.compact
end

#set_delay(i = SLEEP_FOR_N_SECONDS) ⇒ Object Also known as: set_sleep_for_n_seconds

#

set_delay

The default delay is typically set to 0.145.

#


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/ascii_paradise/animation/animation.rb', line 256

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(i = RUN_N_TIMES) ⇒ Object Also known as: run_n_times

#

set_run_n_times

#


225
226
227
228
229
230
231
232
233
# File 'lib/ascii_paradise/animation/animation.rb', line 225

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

#set_use_ascii_files_from_this_directory(i) ⇒ Object Also known as: set_animation_dir, set_which_dir, set_main_dir

#

set_use_ascii_files_from_this_directory

#


356
357
358
# File 'lib/ascii_paradise/animation/animation.rb', line 356

def set_use_ascii_files_from_this_directory(i)
  @use_ascii_files_from_this_directory = i
end

#show_frame_at_this_position(i) ⇒ Object

#

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
#


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ascii_paradise/animation/animation.rb', line 195

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_interfaceObject

#

show_to_the_user_how_to_operate_the_keypress_interface

#


276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/ascii_paradise/animation/animation.rb', line 276

def show_to_the_user_how_to_operate_the_keypress_interface
  e  "#{rev}Press "+
     slategrey('"')+
     simp('n')+
     slategrey('"')+
     rev+' if you wish to continue and see '\
     'the next frame, '+slategrey('"')+
     simp('p')+
     slategrey('"')
  ee "#{rev}for the previous frame and "+
     slategrey('"')+simp('q')+slategrey('"')+
     "#{rev} if you wish to exit."
end

#sleep_with_the_default_delayObject

#

sleep_with_the_default_delay

#


365
366
367
# File 'lib/ascii_paradise/animation/animation.rb', line 365

def sleep_with_the_default_delay
  sleep @delay
end

#use_disco_inferno?Boolean

#

use_disco_inferno?

#

Returns:

  • (Boolean)


156
157
158
# File 'lib/ascii_paradise/animation/animation.rb', line 156

def use_disco_inferno?
  @use_disco_inferno
end