Class: Kotoshu::Cli::AutoSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/cli/auto_setup.rb

Overview

Interactive prompt that wraps the strict two-stage setup/resolve flow for the human-facing CLI.

The library API (‘Kotoshu.correct?`, `Kotoshu.suggest`) still raises `ResourceNotSetupError` strictly — no surprise downloads on metered networks. This class catches that error in the CLI dispatcher, asks the user once, and retries the original command. Programmatic users never see it.

Non-TTY contexts (pipes, CI) and offline mode never prompt. The caller decides how to surface a nil result — the CLI dispatcher raises Errors::ResourceUnavailable so scripts see stable exit codes.

Instance Method Summary collapse

Constructor Details

#initialize(input: $stdin, output: $stderr) ⇒ AutoSetup

Returns a new instance of AutoSetup.

Parameters:

  • input (IO) (defaults to: $stdin)

    Stdin (or override for tests)

  • output (IO) (defaults to: $stderr)

    Stderr (or override for tests)



22
23
24
25
# File 'lib/kotoshu/cli/auto_setup.rb', line 22

def initialize(input: $stdin, output: $stderr)
  @input = input
  @output = output
end

Instance Method Details

#call(error, want: %i[spelling])) ⇒ String?

Prompt the user to set up the missing language.

Parameters:

  • error (Kotoshu::ResourceNotSetupError)

    The error raised by resolve

  • want (Array<Symbol>) (defaults to: %i[spelling]))

    Resource types to fetch (default [:spelling])

Returns:

  • (String, nil)

    Language code on success; nil when non-TTY, offline, or user declined.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kotoshu/cli/auto_setup.rb', line 33

def call(error, want: %i[spelling])
  language = error.language
  return nil if skip_prompt?

  @output.puts prompt_message(language, error.resource_type, want)
  answer = @input.gets&.strip&.downcase
  return nil unless affirmative?(answer)

  Kotoshu.setup(language, want: want)
  language
end