Sha256: d9fd15ec11f8d89d241f6e1d0dd05c7caf95a511c1b95a498853632e1a4fc570

Contents?: true

Size: 1.77 KB

Versions: 201

Compression:

Stored size: 1.77 KB

Contents

begin
  require "readline"
rescue LoadError
end

class Bundler::Thor
  module LineEditor
    class Readline < Basic
      def self.available?
        Object.const_defined?(:Readline)
      end

      def readline
        if echo?
          ::Readline.completion_append_character = nil
          # Ruby 1.8.7 does not allow Readline.completion_proc= to receive nil.
          if complete = completion_proc
            ::Readline.completion_proc = complete
          end
          ::Readline.readline(prompt, add_to_history?)
        else
          super
        end
      end

    private

      def add_to_history?
        options.fetch(:add_to_history, true)
      end

      def completion_proc
        if use_path_completion?
          proc { |text| PathCompletion.new(text).matches }
        elsif completion_options.any?
          proc do |text|
            completion_options.select { |option| option.start_with?(text) }
          end
        end
      end

      def completion_options
        options.fetch(:limited_to, [])
      end

      def use_path_completion?
        options.fetch(:path, false)
      end

      class PathCompletion
        attr_reader :text
        private :text

        def initialize(text)
          @text = text
        end

        def matches
          relative_matches
        end

      private

        def relative_matches
          absolute_matches.map { |path| path.sub(base_path, "") }
        end

        def absolute_matches
          Dir[glob_pattern].map do |path|
            if File.directory?(path)
              "#{path}/"
            else
              path
            end
          end
        end

        def glob_pattern
          "#{base_path}#{text}*"
        end

        def base_path
          "#{Dir.pwd}/"
        end
      end
    end
  end
end

Version data entries

201 entries across 173 versions & 8 rubygems

Version Path
rubygems-update-3.0.9 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-2.7.11 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.0.8 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.0.7 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.1.0.pre2 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.1.0.pre1 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.1.0.pre.2 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.1.0.pre.1 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.0.6 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.0.5 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.0.4 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-2.7.10 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.0.2 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-2.7.9 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.0.3 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rb2exe-0.3.1 bin/traveling-ruby-2.2.2/l64/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rb2exe-0.3.1 bin/traveling-ruby-2.2.2/osx/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rb2exe-0.3.1 bin/traveling-ruby-2.2.2/l32/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rb2exe-0.3.1 bin/traveling-ruby-2.2.2/win/lib/ruby/gems/2.2.0/gems/bundler-1.9.9/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.0.1 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb