Sha256: 4ffb45bb6b675b031b6008b7bf5352d63af52a93309d74f67f7430764179b7b0

Contents?: true

Size: 1.92 KB

Versions: 92

Compression:

Stored size: 1.92 KB

Contents

require('cli/ui')

module CLI
  module UI
    # Widgets are formatter objects with more custom implementations than the
    # other features, which all center around formatting text with colours,
    # etc.
    #
    # If you want to extend CLI::UI with your own widgets, you may want to do
    # something like this:
    #
    #   require('cli/ui')
    #   class MyWidget < CLI::UI::Widgets::Base
    #     # ...
    #   end
    #   CLI::UI::Widgets.register('my-widget') { MyWidget }
    #   puts(CLI::UI.fmt("{{@widget/my-widget:args}}"))
    module Widgets
      MAP = {}

      autoload(:Base, 'cli/ui/widgets/base')

      def self.register(name, &cb)
        MAP[name] = cb
      end

      autoload(:Status, 'cli/ui/widgets/status')
      register('status') { Widgets::Status }

      # Looks up a widget by handle
      #
      # ==== Raises
      # Raises InvalidWidgetHandle if the widget is not available.
      #
      # ==== Returns
      # A callable widget, to be invoked like `.call(argstring)`
      #
      def self.lookup(handle)
        MAP.fetch(handle.to_s).call
      rescue KeyError, NameError
        raise(InvalidWidgetHandle, handle)
      end

      # All available widgets by name
      #
      def self.available
        MAP.keys
      end

      class InvalidWidgetHandle < ArgumentError
        def initialize(handle)
          super
          @handle = handle
        end

        def message
          keys = Widget.available.join(',')
          "invalid widget handle: #{@handle} " \
            "-- must be one of CLI::UI::Widgets.available (#{keys})"
        end
      end

      class InvalidWidgetArguments < ArgumentError
        def initialize(argstring, pattern)
          super
          @argstring = argstring
          @pattern   = pattern
        end

        def message
          "invalid widget arguments: #{@argstring} " \
            "-- must match pattern: #{@pattern.inspect}"
        end
      end
    end
  end
end

Version data entries

92 entries across 92 versions & 3 rubygems

Version Path
shopify-cli-2.36.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.35.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.34.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.33.1 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.33.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.32.1 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.32.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.31.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.30.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.29.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.28.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.27.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.26.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.25.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.24.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.23.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.22.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.21.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.20.1 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-2.20.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb