Class: Spellr::InteractiveReplacement

Inherits:
Object
  • Object
show all
Includes:
StringFormat
Defined in:
lib/spellr/interactive_replacement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringFormat

aqua, bold, green, key, lighten, normal, pluralize, red

Constructor Details

#initialize(token, reporter) ⇒ InteractiveReplacement

Returns a new instance of InteractiveReplacement.



14
15
16
17
18
19
20
# File 'lib/spellr/interactive_replacement.rb', line 14

def initialize(token, reporter)
  @token = token
  @token_highlight = red(token)
  @reporter = reporter
  Readline.input = reporter.output.stdin
  Readline.output = reporter.output.stdout
end

Instance Attribute Details

#globalObject Also known as: global?

Returns the value of attribute global.



11
12
13
# File 'lib/spellr/interactive_replacement.rb', line 11

def global
  @global
end

#reporterObject (readonly)

Returns the value of attribute reporter.



10
11
12
# File 'lib/spellr/interactive_replacement.rb', line 10

def reporter
  @reporter
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/spellr/interactive_replacement.rb', line 10

def token
  @token
end

#token_highlightObject (readonly)

Returns the value of attribute token_highlight.



10
11
12
# File 'lib/spellr/interactive_replacement.rb', line 10

def token_highlight
  @token_highlight
end

Instance Method Details

#ask_replacementObject



40
41
42
43
44
# File 'lib/spellr/interactive_replacement.rb', line 40

def ask_replacement
  puts ''
  puts "  #{lighten '[^C] to go back'}"
  prompt_replacement
end

#complete_replacement(replacement) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/spellr/interactive_replacement.rb', line 27

def complete_replacement(replacement)
  handle_global_replacement(replacement)
  token.replace(replacement)

  reporter.increment(:total_fixed)
  puts "\n\e[0mReplaced #{'all ' if global?}#{token_highlight} with #{green(replacement)}"
  throw :check_file_from, token
end

#global_replaceObject



22
23
24
25
# File 'lib/spellr/interactive_replacement.rb', line 22

def global_replace
  self.global = true
  replace
end

#handle_ctrl_cObject



71
72
73
74
75
# File 'lib/spellr/interactive_replacement.rb', line 71

def handle_ctrl_c
  print "\e[0m"
  reporter.clear_line(4)
  reporter.call(token, only_prompt: true)
end

#handle_global_replacement(replacement) ⇒ Object



36
37
38
# File 'lib/spellr/interactive_replacement.rb', line 36

def handle_global_replacement(replacement)
  reporter.global_replacements[token.to_s] = replacement if global?
end

#prompt_replacementObject



46
47
48
49
50
51
52
# File 'lib/spellr/interactive_replacement.rb', line 46

def prompt_replacement
  Readline.pre_input_hook = -> { pre_input_hook(token) }
  prompt = "  Replace #{'all ' if global?}#{token_highlight} with: \e[32m"
  Readline.readline(prompt)
rescue Interrupt
  handle_ctrl_c
end

#re_ask_replacementObject



54
55
56
57
58
# File 'lib/spellr/interactive_replacement.rb', line 54

def re_ask_replacement
  print "\e[0m\a\e[1T"

  try_replace(prompt_replacement)
end

#replaceObject



67
68
69
# File 'lib/spellr/interactive_replacement.rb', line 67

def replace
  try_replace(ask_replacement)
end

#try_replace(replacement) ⇒ Object



60
61
62
63
64
65
# File 'lib/spellr/interactive_replacement.rb', line 60

def try_replace(replacement)
  return re_ask_replacement if replacement == token
  return re_ask_replacement if replacement.empty?

  complete_replacement(replacement)
end