Sha256: edf73240be03708c8170b2daf7b817af5e7cb625013e5235ce7c6a62d417e15e

Contents?: true

Size: 1.91 KB

Versions: 14

Compression:

Stored size: 1.91 KB

Contents

# encoding: UTF-8

require "airbrussh/colors"
require "delegate"
# rubocop:disable Style/AsciiComments

module Airbrussh
  # Decorates an SSHKit Command to add string output helpers and the
  # command's position within currently executing rake task:
  #
  # * position - zero-based position of this command in the list of
  #              all commands that have been run in the current rake task; in
  #              some cases this could be nil
  class CommandFormatter < SimpleDelegator
    include Airbrussh::Colors

    def initialize(command, position)
      super(command)
      @position = position
    end

    # Prefixes the line with the command number and removes the newline.
    #
    # format_output("hello\n") # => "01 hello"
    #
    def format_output(line)
      "#{number} #{line.chomp}"
    end

    # Returns the abbreviated command (in yellow) with the number prefix.
    #
    # start_message # => "01 echo hello"
    #
    def start_message
      "#{number} #{yellow(abbreviated)}"
    end

    # Returns a green (success) or red (failure) message depending on the
    # exit status.
    #
    # exit_message # => "✔ 01 user@host 0.084s"
    # exit_message # => "✘ 01 user@host 0.084s"
    #
    def exit_message
      message = if failure?
                  red(failure_message)
                else
                  green(success_message)
                end
      message << " #{runtime}"
    end

    private

    def user_at_host
      user_str = host.user || (host.ssh_options || {})[:user]
      host_str = host.hostname
      [user_str, host_str].compact.join("@")
    end

    def runtime
      format("%5.3fs", super)
    end

    def abbreviated
      to_s.sub(%r{^/usr/bin/env }, "")
    end

    def number
      format("%02d", @position.to_i + 1)
    end

    def success_message
      "✔ #{number} #{user_at_host}"
    end

    def failure_message
      "✘ #{number} #{user_at_host}"
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
airbrussh-1.5.3 lib/airbrussh/command_formatter.rb
airbrussh-1.5.2 lib/airbrussh/command_formatter.rb
airbrussh-1.5.1 lib/airbrussh/command_formatter.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/airbrussh-1.5.0/lib/airbrussh/command_formatter.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/airbrussh-1.5.0/lib/airbrussh/command_formatter.rb
airbrussh-1.5.0 lib/airbrussh/command_formatter.rb
airbrussh-1.4.2 lib/airbrussh/command_formatter.rb
airbrussh-1.4.1 lib/airbrussh/command_formatter.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/airbrussh-1.3.1/lib/airbrussh/command_formatter.rb
airbrussh-1.4.0 lib/airbrussh/command_formatter.rb
airbrussh-1.3.4 lib/airbrussh/command_formatter.rb
airbrussh-1.3.3 lib/airbrussh/command_formatter.rb
airbrussh-1.3.2 lib/airbrussh/command_formatter.rb
airbrussh-1.3.1 lib/airbrussh/command_formatter.rb