Module: WordSmith::CommandRunner::Translation

Extended by:
T::Sig
Defined in:
lib/command_runner/translation.rb

Class Method Summary collapse

Class Method Details

.run(input_text, options) ⇒ Object



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
# File 'lib/command_runner/translation.rb', line 16

def run(input_text, options)
  is_contextual_translation = input_text.match?(%r{/[a-zA-Z]+/})

  if is_contextual_translation
    sentence = input_text.gsub(%r{/([a-zA-Z]+)/}, '\1')

    literal_word_match = input_text.match(%r{/([a-zA-Z]+)/})
    raise "Invalid word: #{input_text}" if literal_word_match.nil?

    word = T.cast(literal_word_match.captures[0], String)

    result = find_or_create_contextual_translation(word, sentence, **options)

    puts '', "In context of \"#{Rainbow(sentence).blue.bold}\":"

    print_common_parts(result)

    return
  end

  word = input_text.chomp

  result = find_or_create_word_translation(word, **options)

  print_common_parts(result)
end