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
bundler-2.5.15 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.14 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.14 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.13 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.13 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.12 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.12 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.10 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.10 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.9 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.9 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.8 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.8 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
rubygems-update-3.5.7 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.7 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
direct7-0.0.13 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.6 bundler/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
bundler-2.5.6 lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb