Sha256: 9e7cb888211f4bb53ca684c1634c7d2a3242b004c3d8de5f85b39b85764fb726

Contents?: true

Size: 1.81 KB

Versions: 82

Compression:

Stored size: 1.81 KB

Contents

require 'cli/ui'

module CLI
  module UI
    class Printer
      # Print a message to a stream with common utilities.
      # Allows overriding the color, encoding, and target stream.
      # By default, it formats the string using CLI:UI and rescues common stream errors.
      #
      # ==== Attributes
      #
      # * +msg+ - (required) the string to output. Can be frozen.
      #
      # ==== Options
      #
      # * +:frame_color+ - Override the frame color. Defaults to nil.
      # * +:to+ - Target stream, like $stdout or $stderr. Can be anything with a puts method. Defaults to $stdout.
      # * +:encoding+ - Force the output to be in a certain encoding. Defaults to UTF-8.
      # * +:format+ - Whether to format the string using CLI::UI.fmt. Defaults to true.
      # * +:graceful+ - Whether to gracefully ignore common I/O errors. Defaults to true.
      # * +:wrap+ - Whether to wrap text at word boundaries to terminal width. Defaults to true.
      #
      # ==== Returns
      # Returns whether the message was successfully printed,
      # which can be useful if +:graceful+ is set to true.
      #
      # ==== Example
      #
      #   CLI::UI::Printer.puts('{{x}} Ouch', to: $stderr)
      #
      def self.puts(
        msg,
        frame_color:
        nil,
        to:
        $stdout,
        encoding: Encoding::UTF_8,
        format: true,
        graceful: true,
        wrap: true
      )
        msg = (+msg).force_encoding(encoding) if encoding
        msg = CLI::UI.fmt(msg) if format
        msg = CLI::UI.wrap(msg) if wrap

        if frame_color
          CLI::UI::Frame.with_frame_color_override(frame_color) { to.puts(msg) }
        else
          to.puts(msg)
        end

        true
      rescue Errno::EIO, Errno::EPIPE, IOError => e
        raise(e) unless graceful
        false
      end
    end
  end
end

Version data entries

82 entries across 82 versions & 3 rubygems

Version Path
shopify-cli-2.10.1 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.10.0 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.9.0 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.8.0 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.7.4 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.7.3 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.7.2 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.7.1 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.7.0 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.6.6 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.6.5 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.6.4 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.6.3 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.6.2 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.6.1 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.6.0 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.5.0 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.4.0 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.3.0 vendor/deps/cli-ui/lib/cli/ui/printer.rb
shopify-cli-2.2.2 vendor/deps/cli-ui/lib/cli/ui/printer.rb