class Pakyow::Support::CLI::Runner

Constants

ANSI_REGEX
FAILURE_MARK
FAILURE_MESSAGE
SPINNER
SUCCESS_MARK
SUCCESS_MESSAGE

Public Class Methods

new(message:) click to toggle source
# File lib/pakyow/support/cli/runner.rb, line 20
def initialize(message:)
  @spinner = TTY::Spinner.new(
    Support::CLI.style.bold(":spinner #{message}"),
    format: SPINNER,
    success_mark: SUCCESS_MARK,
    error_mark: FAILURE_MARK
  )

  @succeeded = @failed = false
end

Public Instance Methods

completed?() click to toggle source

Returns `true` if the command has completed.

# File lib/pakyow/support/cli/runner.rb, line 77
def completed?
  succeeded? || failed?
end
failed(output = "") click to toggle source

Called when the command fails.

# File lib/pakyow/support/cli/runner.rb, line 67
def failed(output = "")
  unless completed?
    @failed = true
    @spinner.error(Support::CLI.style.red(FAILURE_MESSAGE))
    puts indent_output(output) unless output.empty?
  end
end
failed?() click to toggle source

Returns `true` if the command has completed unsuccessfully.

# File lib/pakyow/support/cli/runner.rb, line 89
def failed?
  @failed == true
end
run(*command) { |self| ... } click to toggle source

Runs a command or block of code. If a value for `command` is passed with the block, the result will be yielded to the block on success.

# File lib/pakyow/support/cli/runner.rb, line 34
def run(*command)
  @spinner.auto_spin

  if command.empty? && block_given?
    yield self
    succeeded
  else
    result = TTY::Command.new(printer: :null, pty: true).run!(*command)

    if result.failure?
      failed(result.err)
    else
      succeeded

      if block_given?
        yield result
      end
    end
  end
end
succeeded(output = "") click to toggle source

Called when the command succeeds.

# File lib/pakyow/support/cli/runner.rb, line 57
def succeeded(output = "")
  unless completed?
    @succeeded = true
    @spinner.success(Support::CLI.style.green(SUCCESS_MESSAGE))
    puts indent_output(output) unless output.empty?
  end
end
succeeded?() click to toggle source

Returns `true` if the command has completed successfully.

# File lib/pakyow/support/cli/runner.rb, line 83
def succeeded?
  @succeeded == true
end

Private Instance Methods

indent_output(output) click to toggle source
# File lib/pakyow/support/cli/runner.rb, line 97
def indent_output(output)
  output.split("\n").map { |line|
    first_real_string = line.split(ANSI_REGEX).reject(&:empty?).first
    line.sub(first_real_string.to_s, "   #{first_real_string}")
  }.join("\n")
end