Sha256: d4211762734e7ec8ec25094c0a0a1a593263e20a777b87d7e45adace9dc0bb35

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require 'thor'

module Terminalwire
  module Thor
    class Shell < ::Thor::Shell::Basic
      extend Forwardable

      # Encapsulates all of the IO resources for a Terminalwire adapter.
      attr_reader :context, :session

      def_delegators :context,
        :stdin, :stdout, :stderr

      def initialize(context, *, **, &)
        @context = context
        @session = Terminalwire::Rails::Session.new(context:)
        super(*,**,&)
      end
    end

    def self.included(base)
      base.extend ClassMethods

      # I have to do this in a block to deal with some of Thor's DSL
      base.class_eval do
        extend Forwardable

        protected

        no_commands do
          def_delegators :shell,
            :context, :session
          def_delegators :context,
            :stdout, :stdin, :stderr, :browser
          def_delegators :stdout,
            :puts, :print
          def_delegators :stdin,
            :gets, :getpass

          # Prints text to the standard error stream.
          def warn(...)
            stderr.puts(...)
          end

          # Prints text to the standard error stream and exits the program.
          def fail(...)
            stderr.puts(...)
            context.exit 1
          ensure
            super
          end
          # Feels more naturual to call `client.files` etc. from
          # the serve since it's more apparent that it's a client.
          alias :client :context
        end
      end
    end

    module ClassMethods
      def start(given_args = ARGV, config = {})
        context = config.delete(:context)
        config[:shell] = Shell.new(context) if context
        super(given_args, config)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terminalwire-0.2.5 lib/terminalwire/thor.rb