class Dictation::Typewriter

Attributes

words[RW]

Public Class Methods

new() click to toggle source
# File lib/dictation/typewriter.rb, line 5
def initialize
  @words = []
end

Public Instance Methods

collect() click to toggle source
# File lib/dictation/typewriter.rb, line 22
def collect
  paint_puts('Please type word and translation one by one, press Enter key to save.  When you finish, press Ctrl+C.', :pink)
  Signal.trap('SIGINT') do
    puts ''
    break
  end
  loop do
    record
  end
  paint_puts("=" * 80, :green)
  paint_puts("Input is done.", :green)
  @words
end

Private Instance Methods

get_input_if_not_nil() click to toggle source
# File lib/dictation/typewriter.rb, line 9
def get_input_if_not_nil
  input = gets
  input.nil? ? nil : input.chomp!
end
paint(text, color) click to toggle source
# File lib/dictation/typewriter.rb, line 36
def paint(text, color)
  case color
    when :green
      color_code = 32
    when :yellow
      color_code = 33
    when :blue
      color_code = 34
    when :pink
      color_code = 35
    else
      color_code = 0
  end
  "\e[#{color_code}m#{text}\e[0m"
end
paint_print(text, color) click to toggle source
# File lib/dictation/typewriter.rb, line 52
def paint_print(text, color)
  print(paint(text, color))
end
paint_puts(text, color) click to toggle source
# File lib/dictation/typewriter.rb, line 56
def paint_puts(text, color)
  puts(paint(text, color))
end
record() click to toggle source
# File lib/dictation/typewriter.rb, line 14
def record
  paint_print('Word:         ', :blue)
  value = get_input_if_not_nil
  paint_print('Translation:  ', :yellow)
  translation = get_input_if_not_nil
  @words.push(Word.new(value, translation)) if value && translation
end