Sha256: 1bfb465d6d37e2be2c42d0320a0b6c6f0cd44bdb0b15a4da3d32bc32ed1b6b66

Contents?: true

Size: 1.89 KB

Versions: 47

Compression:

Stored size: 1.89 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)
          @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)
          @argstring = argstring
          @pattern   = pattern
        end

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

Version data entries

47 entries across 47 versions & 3 rubygems

Version Path
rfix-1.4.1 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.4.0.pre.201 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.4.0 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.3.0.pre.199 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.6.pre.198 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.6 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-1.0.5 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-1.0.4 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-1.0.3 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.2.pre.174 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.5 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.4 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.3.pre vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.2.pre vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.2.pre.172 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.2 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.1.0.pre.150 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.2.0 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.1.4.pre vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.1.1.pre vendor/shopify/cli-ui/lib/cli/ui/widgets.rb