Class: Legion::TTY::Components::DigitalRain

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/tty/components/digital_rain.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

FADE_SHADES =

rubocop:disable Naming/VariableNumber

%i[
  purple_12 purple_11 purple_10 purple_9 purple_8
  purple_7 purple_6 purple_5 purple_4 purple_3
  purple_2 purple_1
].freeze
HEAD_COLOR =
:purple_17
FALLBACK_NAMES =

rubocop:enable Naming/VariableNumber

%w[
  hippocampus amygdala prefrontal-cortex cerebellum thalamus hypothalamus
  brainstem synapse apollo tasker scheduler node health telemetry
  conditioner transformer memory dream cortex glia neuron dendrite axon receptor
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:, extensions: nil, density: 0.7) ⇒ DigitalRain

Returns a new instance of DigitalRain.



28
29
30
31
32
33
34
35
36
# File 'lib/legion/tty/components/digital_rain.rb', line 28

def initialize(width:, height:, extensions: nil, density: 0.7)
  @width = width
  @height = height
  @density = density
  @extensions = extensions || self.class.extension_names
  @max_frames = 200
  @frame = 0
  @columns = build_columns
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



26
27
28
# File 'lib/legion/tty/components/digital_rain.rb', line 26

def columns
  @columns
end

#heightObject (readonly)

Returns the value of attribute height.



26
27
28
# File 'lib/legion/tty/components/digital_rain.rb', line 26

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



26
27
28
# File 'lib/legion/tty/components/digital_rain.rb', line 26

def width
  @width
end

Class Method Details

.extension_namesObject



38
39
40
41
42
43
44
# File 'lib/legion/tty/components/digital_rain.rb', line 38

def self.extension_names
  gems = Gem::Specification.select { |s| s.name.start_with?('lex-') }
                           .map { |s| s.name.sub(/^lex-/, '') }
  gems.empty? ? FALLBACK_NAMES : gems
rescue StandardError
  FALLBACK_NAMES
end

Instance Method Details

#done?Boolean

rubocop:enable Metrics/AbcSize

Returns:

  • (Boolean)


78
79
80
# File 'lib/legion/tty/components/digital_rain.rb', line 78

def done?
  @frame >= @max_frames
end

#render_frameObject



54
55
56
57
58
# File 'lib/legion/tty/components/digital_rain.rb', line 54

def render_frame
  grid = Array.new(@height) { Array.new(@width) { { char: ' ', color: nil } } }
  paint_columns(grid)
  render_grid(grid)
end

#run(duration_seconds: 7, fps: 18, output: $stdout) ⇒ Object

rubocop:disable Metrics/AbcSize



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/legion/tty/components/digital_rain.rb', line 61

def run(duration_seconds: 7, fps: 18, output: $stdout)
  require 'tty-cursor'
  cursor = ::TTY::Cursor
  frame_delay = 1.0 / fps
  output.print cursor.hide
  output.print cursor.save
  (duration_seconds * fps).times do
    output.print cursor.restore
    render_frame.each { |line| output.puts line }
    tick
    sleep frame_delay
  end
ensure
  output.print cursor.show
end

#tickObject



46
47
48
49
50
51
52
# File 'lib/legion/tty/components/digital_rain.rb', line 46

def tick
  @frame += 1
  @columns.each do |col|
    col[:y] += col[:speed]
    reset_column(col) if col[:y] - col[:length] > @height
  end
end