Method: Doing::WWID#guess_section

Defined in:
lib/doing/wwid/guess.rb

#guess_section(frag, guessed: false, suggest: false) ⇒ Object

Attempt to match a string with an existing section

Parameters:

  • frag (String)

    The user-provided string

  • guessed (Boolean) (defaults to: false)

    already guessed and failed



11
12
13
14
15
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
42
43
44
45
46
# File 'lib/doing/wwid/guess.rb', line 11

def guess_section(frag, guessed: false, suggest: false)
  return 'All' if frag =~ /^all$/i

  frag ||= Doing.setting('current_section')

  return frag.cap_first if @content.section?(frag)

  found = @content.guess_section(frag, distance: 2)

  section = found ? found.title : nil

  return section if suggest

  unless section || guessed
    alt = guess_view(frag, guessed: true, suggest: true)
    if alt
      prompt = Color.template("{bw}Did you mean `{xy}doing {by}view {xy}#{alt}`{bw}?{x}")
      meant_view = Prompt.yn(prompt, default_response: 'n')

      msg = format('%<y>srun with `%<w>sdoing view %<alt>s%<y>s`', w: boldwhite, y: yellow, alt: alt)
      raise Errors::WrongCommand.new(msg, topic: 'Try again:') if meant_view

    end

    res = Prompt.yn("#{boldwhite}Section #{frag.yellow}#{boldwhite} not found, create it", default_response: 'n')

    if res
      @content.add_section(frag.cap_first, log: true)
      write(@doing_file)
      return frag.cap_first
    end

    raise Errors::InvalidSection.new("unknown section #{frag.bold.white}", topic: 'Missing:')
  end
  section ? section.cap_first : guessed
end