class Sijka::Smoke

Constants

DEFAULT_IMG_FILE
MOVE_SLEEP_TIME
SLEEP_TIME_IN_THE_END

Attributes

img_file_name[R]
smoker_name[R]

Public Class Methods

new(smoker_name, file_name) click to toggle source
# File lib/sijka/smoke.rb, line 9
def initialize(smoker_name, file_name)
  @smoker_name = smoker_name
  @img_file_name = validated_img_file_name(file_name&.downcase)
  load_standarted_img
  CursesInitializer.call
  @movement_range = Curses.cols - @img_length
end

Public Instance Methods

smoke() click to toggle source
# File lib/sijka/smoke.rb, line 17
def smoke
  right_move
  reverse_img
  left_move
  write_end_message
end

Private Instance Methods

clear_terminal() click to toggle source
# File lib/sijka/smoke.rb, line 26
def clear_terminal
  Curses.refresh
  Curses.clear
end
draw_image(start_position:) click to toggle source
# File lib/sijka/smoke.rb, line 36
def draw_image(start_position:)
  @img.each_with_index { |line, y_position| place_string(y_pos: y_position, x_pos: start_position, line: line) }
end
left_move() click to toggle source
# File lib/sijka/smoke.rb, line 48
def left_move
  @movement_range.times do |i|
    draw_image(start_position: @movement_range - i)
    sleep(MOVE_SLEEP_TIME)
    clear_terminal
  end
end
load_standarted_img() click to toggle source
# File lib/sijka/smoke.rb, line 71
def load_standarted_img
  file_path = "#{$LOAD_PATH.first}/characters/#{img_file_name}"
  @img = File.open(file_path) { |file| file.read.split("\n") }

  @img_length = @img.max_by(&:length).length
  @img.map { |line| line << ' ' * (@img_length - line.length) }
end
message_with_name() click to toggle source
# File lib/sijka/smoke.rb, line 60
def message_with_name
  message = Translator.new.smoken_with_locale(img_file_name)
  smoker_name.to_s.empty? ? "#{message}!" : "#{message}, #{smoker_name}!"
end
place_string(y_pos:, x_pos:, line:) click to toggle source
# File lib/sijka/smoke.rb, line 31
def place_string(y_pos:, x_pos:, line:)
  Curses.setpos(y_pos, x_pos)
  Curses.addstr(line)
end
reverse_img() click to toggle source
# File lib/sijka/smoke.rb, line 56
def reverse_img
  @img.map!(&:reverse)
end
right_move() click to toggle source
# File lib/sijka/smoke.rb, line 40
def right_move
  @movement_range.times do |i|
    draw_image(start_position: i)
    sleep(MOVE_SLEEP_TIME)
    clear_terminal
  end
end
validated_img_file_name(file_name) click to toggle source
# File lib/sijka/smoke.rb, line 79
def validated_img_file_name(file_name)
  file_name && Sijka::FILE_LIST.include?(file_name) ? file_name : DEFAULT_IMG_FILE
end
write_end_message() click to toggle source
# File lib/sijka/smoke.rb, line 65
def write_end_message
  place_string(y_pos: 0, x_pos: 0, line: message_with_name)
  clear_terminal
  sleep(SLEEP_TIME_IN_THE_END)
end