Sha256: 3bf0c2912b46cd4bdbf32d588b05a1651d96ba49eaac413844ade064eb2baa1e

Contents?: true

Size: 1.34 KB

Versions: 7

Compression:

Stored size: 1.34 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

          # 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

7 entries across 7 versions & 1 rubygems

Version Path
terminalwire-0.1.10 lib/terminalwire/thor.rb
terminalwire-0.1.9 lib/terminalwire/thor.rb
terminalwire-0.1.8 lib/terminalwire/thor.rb
terminalwire-0.1.7 lib/terminalwire/thor.rb
terminalwire-0.1.6 lib/terminalwire/thor.rb
terminalwire-0.1.5 lib/terminalwire/thor.rb
terminalwire-0.1.4 lib/terminalwire/thor.rb