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.1.0.pre.149 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.1.0.pre.147 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-1.0.2 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-1.0.1 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-1.0.0 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
shopify-cli-0.9.3 vendor/deps/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.15.pre.116 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.15 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.8.pre.109 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.8.pre.108 vendor/shopify/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.7.pre.68 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.7.pre.67 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.7.pre.66 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.7.pre.65 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.7.pre.61 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.6.pre.60 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.7 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.5.pre.58 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.6 vendor/cli-ui/lib/cli/ui/widgets.rb
rfix-1.0.5.pre.pre.57 vendor/cli-ui/lib/cli/ui/widgets.rb