Sha256: e18287b1714712e0611cf81821ca06566e489b282c744613956be4a33f4cfcda

Contents?: true

Size: 1.8 KB

Versions: 317

Compression:

Stored size: 1.8 KB

Contents

class Bundler::Thor
  module LineEditor
    class Readline < Basic
      def self.available?
        begin
          require "readline"
        rescue LoadError
        end

        Object.const_defined?(:Readline)
      end

      def readline
        if echo?
          ::Readline.completion_append_character = nil
          # rb-readline 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

317 entries across 317 versions & 6 rubygems

Version Path
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.23 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.23 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.22 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.22 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.21 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.21 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.20 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.20 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.19 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.19 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.18 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.18 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.17 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.17 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.16 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.16 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.15 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb